View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional information regarding
4    * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License. You may obtain a
6    * copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software distributed under the License
11   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing permissions and limitations under
13   * the License.
14   */
15  
16  package org.apache.maven.lifecycle.internal.stub;
17  
18  import org.apache.maven.execution.MavenSession;
19  import org.apache.maven.lifecycle.LifecycleNotFoundException;
20  import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
21  import org.apache.maven.lifecycle.MavenExecutionPlan;
22  import org.apache.maven.lifecycle.internal.ExecutionPlanItem;
23  import org.apache.maven.lifecycle.internal.LifecycleExecutionPlanCalculator;
24  import org.apache.maven.lifecycle.internal.ProjectBuildList;
25  import org.apache.maven.lifecycle.internal.ProjectSegment;
26  import org.apache.maven.model.Plugin;
27  import org.apache.maven.plugin.InvalidPluginDescriptorException;
28  import org.apache.maven.plugin.MojoExecution;
29  import org.apache.maven.plugin.MojoNotFoundException;
30  import org.apache.maven.plugin.PluginDescriptorParsingException;
31  import org.apache.maven.plugin.PluginNotFoundException;
32  import org.apache.maven.plugin.PluginResolutionException;
33  import org.apache.maven.plugin.descriptor.MojoDescriptor;
34  import org.apache.maven.plugin.descriptor.PluginDescriptor;
35  import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
36  import org.apache.maven.plugin.version.PluginVersionResolutionException;
37  import org.apache.maven.project.MavenProject;
38  import org.codehaus.plexus.util.xml.Xpp3Dom;
39  
40  import java.util.ArrayList;
41  import java.util.Arrays;
42  import java.util.HashSet;
43  import java.util.List;
44  import java.util.Set;
45  
46  /**
47   * @author Kristian Rosenvold
48   */
49  public class LifecycleExecutionPlanCalculatorStub
50      implements LifecycleExecutionPlanCalculator
51  {
52      // clean
53  
54      public final static MojoDescriptor PRE_CLEAN = createMojoDescriptor( "pre-clean" );
55  
56      public final static MojoDescriptor CLEAN = createMojoDescriptor( "clean" );
57  
58      public final static MojoDescriptor POST_CLEAN = createMojoDescriptor( "post-clean" );
59  
60      // default (or at least some of them)
61  
62      public final static MojoDescriptor VALIDATE = createMojoDescriptor( "validate" );
63  
64      public final static MojoDescriptor INITIALIZE = createMojoDescriptor( "initialize" );
65  
66      public final static MojoDescriptor TEST_COMPILE = createMojoDescriptor( "test-compile" );
67  
68      public final static MojoDescriptor PROCESS_TEST_RESOURCES = createMojoDescriptor( "process-test-resources" );
69  
70      public final static MojoDescriptor PROCESS_RESOURCES = createMojoDescriptor( "process-resources" );
71  
72      public final static MojoDescriptor COMPILE = createMojoDescriptor( "compile", true );
73  
74      public final static MojoDescriptor TEST = createMojoDescriptor( "test" );
75  
76      public final static MojoDescriptor PACKAGE = createMojoDescriptor( "package" );
77  
78      public final static MojoDescriptor INSTALL = createMojoDescriptor( "install" );
79  
80      // site
81  
82      public final static MojoDescriptor PRE_SITE = createMojoDescriptor( "pre-site" );
83  
84      public final static MojoDescriptor SITE = createMojoDescriptor( "site" );
85  
86      public final static MojoDescriptor POST_SITE = createMojoDescriptor( "post-site" );
87  
88      public final static MojoDescriptor SITE_DEPLOY = createMojoDescriptor( "site-deploy" );
89  
90      /**
91       * @deprecated instead use {@link #getNumberOfExecutions(ProjectBuildList)}
92       */
93      @Deprecated
94      public int getNumberOfExceutions( ProjectBuildList projectBuildList )
95          throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
96          NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
97          LifecyclePhaseNotFoundException, LifecycleNotFoundException
98      {
99          int result = 0;
100         for ( ProjectSegment projectBuild : projectBuildList )
101         {
102             MavenExecutionPlan plan = calculateExecutionPlan( projectBuild.getSession(), projectBuild.getProject(),
103                                                               projectBuild.getTaskSegment().getTasks() );
104             result += plan.size();
105         }
106         return result;
107     }
108 
109     public int getNumberOfExecutions( ProjectBuildList projectBuildList )
110         throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
111         NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
112         LifecyclePhaseNotFoundException, LifecycleNotFoundException
113     {
114         return getNumberOfExceutions( projectBuildList );
115     }
116 
117     public void calculateForkedExecutions( MojoExecution mojoExecution, MavenSession session )
118         throws MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
119         PluginDescriptorParsingException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
120         LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException
121     {
122         // Maybe do something ?
123     }
124 
125     public MavenExecutionPlan calculateExecutionPlan( MavenSession session, MavenProject project, List<Object> tasks,
126                                                       boolean setup )
127         throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
128         PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
129         NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
130     {
131         if ( project.equals( ProjectDependencyGraphStub.A ) )
132         {
133             return getProjectAExceutionPlan();
134         }
135         if ( project.equals( ProjectDependencyGraphStub.B ) )
136         {
137             return getProjectBExecutionPlan();
138         }
139         // The remaining are basically "for future expansion"
140         List<MojoExecution> me = new ArrayList<>();
141         me.add( createMojoExecution( "resources", "default-resources", PROCESS_RESOURCES ) );
142         me.add( createMojoExecution( "compile", "default-compile", COMPILE ) );
143         return createExecutionPlan( project, me );
144     }
145 
146     public MavenExecutionPlan calculateExecutionPlan( MavenSession session, MavenProject project, List<Object> tasks )
147         throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
148         PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
149         NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
150     {
151         return calculateExecutionPlan( session, project, tasks, true );
152     }
153 
154     public void setupMojoExecution( MavenSession session, MavenProject project, MojoExecution mojoExecution )
155         throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
156         MojoNotFoundException, InvalidPluginDescriptorException, NoPluginFoundForPrefixException,
157         LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException
158     {
159     }
160 
161     public static MavenExecutionPlan getProjectAExceutionPlan()
162         throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
163         PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
164         NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
165     {
166         List<MojoExecution> me = new ArrayList<>();
167         me.add( createMojoExecution( "initialize", "default-initialize", INITIALIZE ) );
168         me.add( createMojoExecution( "resources", "default-resources", PROCESS_RESOURCES ) );
169         me.add( createMojoExecution( "compile", "default-compile", COMPILE ) );
170         me.add( createMojoExecution( "testResources", "default-testResources", PROCESS_TEST_RESOURCES ) );
171         me.add( createMojoExecution( "testCompile", "default-testCompile", TEST_COMPILE ) );
172         me.add( createMojoExecution( "test", "default-test", TEST ) );
173         me.add( createMojoExecution( "war", "default-war", PACKAGE ) );
174         me.add( createMojoExecution( "install", "default-install", INSTALL ) );
175         return createExecutionPlan( ProjectDependencyGraphStub.A.getExecutionProject(), me );
176     }
177 
178     public static MavenExecutionPlan getProjectBExecutionPlan()
179         throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
180         PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
181         NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
182     {
183         List<MojoExecution> me = new ArrayList<>();
184         me.add( createMojoExecution( "enforce", "enforce-versions", VALIDATE ) );
185         me.add( createMojoExecution( "resources", "default-resources", PROCESS_RESOURCES ) );
186         me.add( createMojoExecution( "compile", "default-compile", COMPILE ) );
187         me.add( createMojoExecution( "testResources", "default-testResources", PROCESS_TEST_RESOURCES ) );
188         me.add( createMojoExecution( "testCompile", "default-testCompile", TEST_COMPILE ) );
189         me.add( createMojoExecution( "test", "default-test", TEST ) );
190         return createExecutionPlan( ProjectDependencyGraphStub.B.getExecutionProject(), me );
191     }
192 
193 
194     private static MavenExecutionPlan createExecutionPlan( MavenProject project, List<MojoExecution> mojoExecutions )
195         throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
196         NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
197         LifecyclePhaseNotFoundException, LifecycleNotFoundException
198     {
199         final List<ExecutionPlanItem> planItemList =
200             ExecutionPlanItem.createExecutionPlanItems( project, mojoExecutions );
201         return new MavenExecutionPlan( planItemList, DefaultLifecyclesStub.createDefaultLifecycles() );
202     }
203 
204     private static MojoExecution createMojoExecution( String goal, String executionId, MojoDescriptor mojoDescriptor )
205     {
206         final Plugin plugin = mojoDescriptor.getPluginDescriptor().getPlugin();
207         MojoExecution result = new MojoExecution( plugin, goal, executionId );
208         result.setConfiguration( new Xpp3Dom( executionId + "-" + goal ) );
209         result.setMojoDescriptor( mojoDescriptor );
210         result.setLifecyclePhase( mojoDescriptor.getPhase() );
211 
212         return result;
213 
214     }
215 
216     public static MojoDescriptor createMojoDescriptor( String phaseName )
217     {
218         return createMojoDescriptor( phaseName, false );
219     }
220 
221     public static MojoDescriptor createMojoDescriptor( String phaseName, boolean threadSafe )
222     {
223         final MojoDescriptor mojoDescriptor = new MojoDescriptor();
224         mojoDescriptor.setPhase( phaseName );
225         final PluginDescriptor descriptor = new PluginDescriptor();
226         Plugin plugin = new Plugin();
227         plugin.setArtifactId( "org.apache.maven.test.MavenExecutionPlan" );
228         plugin.setGroupId( "stub-plugin-" + phaseName );
229         descriptor.setPlugin( plugin );
230         descriptor.setArtifactId( "artifact." + phaseName );
231         mojoDescriptor.setPluginDescriptor( descriptor );
232         mojoDescriptor.setThreadSafe( threadSafe );
233         return mojoDescriptor;
234     }
235 
236 
237     public static Set<String> getScopes()
238     {
239         return new HashSet<>( Arrays.asList( "compile" ) );
240     }
241 
242 }