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.internal.xml.Xpp3Dom;
20  import org.apache.maven.lifecycle.DefaultLifecycles;
21  import org.apache.maven.lifecycle.LifecycleNotFoundException;
22  import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
23  import org.apache.maven.lifecycle.MavenExecutionPlan;
24  import org.apache.maven.lifecycle.internal.DefaultLifecyclePluginAnalyzer;
25  import org.apache.maven.lifecycle.internal.ExecutionPlanItem;
26  import org.apache.maven.lifecycle.internal.LifecycleExecutionPlanCalculator;
27  import org.apache.maven.lifecycle.internal.ProjectBuildList;
28  import org.apache.maven.lifecycle.internal.ProjectSegment;
29  import org.apache.maven.model.InputLocation;
30  import org.apache.maven.model.InputSource;
31  import org.apache.maven.model.Plugin;
32  import org.apache.maven.plugin.InvalidPluginDescriptorException;
33  import org.apache.maven.plugin.MojoExecution;
34  import org.apache.maven.plugin.MojoNotFoundException;
35  import org.apache.maven.plugin.PluginDescriptorParsingException;
36  import org.apache.maven.plugin.PluginNotFoundException;
37  import org.apache.maven.plugin.PluginResolutionException;
38  import org.apache.maven.plugin.descriptor.MojoDescriptor;
39  import org.apache.maven.plugin.descriptor.PluginDescriptor;
40  import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
41  import org.apache.maven.plugin.version.PluginVersionResolutionException;
42  import org.apache.maven.project.MavenProject;
43  import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
44  
45  import java.util.ArrayList;
46  import java.util.Arrays;
47  import java.util.HashSet;
48  import java.util.List;
49  import java.util.Set;
50  
51  /**
52   * @author Kristian Rosenvold
53   */
54  public class LifecycleExecutionPlanCalculatorStub
55      implements LifecycleExecutionPlanCalculator
56  {
57      // clean
58  
59      public final static MojoDescriptor PRE_CLEAN = createMojoDescriptor( "pre-clean" );
60  
61      public final static MojoDescriptor CLEAN = createMojoDescriptor( "clean" );
62  
63      public final static MojoDescriptor POST_CLEAN = createMojoDescriptor( "post-clean" );
64  
65      // default (or at least some of them)
66  
67      public final static MojoDescriptor VALIDATE = createMojoDescriptor( "validate" );
68  
69      public final static MojoDescriptor INITIALIZE = createMojoDescriptor( "initialize" );
70  
71      public final static MojoDescriptor TEST_COMPILE = createMojoDescriptor( "test-compile" );
72  
73      public final static MojoDescriptor PROCESS_TEST_RESOURCES = createMojoDescriptor( "process-test-resources" );
74  
75      public final static MojoDescriptor PROCESS_RESOURCES = createMojoDescriptor( "process-resources" );
76  
77      public final static MojoDescriptor COMPILE = createMojoDescriptor( "compile", true );
78  
79      public final static MojoDescriptor TEST = createMojoDescriptor( "test" );
80  
81      public final static MojoDescriptor PACKAGE = createMojoDescriptor( "package" );
82  
83      public final static MojoDescriptor INSTALL = createMojoDescriptor( "install" );
84  
85      // site
86  
87      public final static MojoDescriptor PRE_SITE = createMojoDescriptor( "pre-site" );
88  
89      public final static MojoDescriptor SITE = createMojoDescriptor( "site" );
90  
91      public final static MojoDescriptor POST_SITE = createMojoDescriptor( "post-site" );
92  
93      public final static MojoDescriptor SITE_DEPLOY = createMojoDescriptor( "site-deploy" );
94  
95      // wrapper
96  
97      public final static MojoDescriptor WRAPPER = createMojoDescriptor( "wrapper" );
98  
99      /**
100      * @deprecated instead use {@link #getNumberOfExecutions(ProjectBuildList)}
101      */
102     @Deprecated
103     public int getNumberOfExceutions( ProjectBuildList projectBuildList )
104         throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
105         NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
106         LifecyclePhaseNotFoundException, LifecycleNotFoundException
107     {
108         int result = 0;
109         for ( ProjectSegment projectBuild : projectBuildList )
110         {
111             MavenExecutionPlan plan = calculateExecutionPlan( projectBuild.getSession(), projectBuild.getProject(),
112                                                               projectBuild.getTaskSegment().getTasks() );
113             result += plan.size();
114         }
115         return result;
116     }
117 
118     public int getNumberOfExecutions( ProjectBuildList projectBuildList )
119         throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
120         NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
121         LifecyclePhaseNotFoundException, LifecycleNotFoundException
122     {
123         return getNumberOfExceutions( projectBuildList );
124     }
125 
126     public void calculateForkedExecutions( MojoExecution mojoExecution, MavenSession session )
127         throws MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
128         PluginDescriptorParsingException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
129         LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException
130     {
131         // Maybe do something ?
132     }
133 
134     public MavenExecutionPlan calculateExecutionPlan( MavenSession session, MavenProject project, List<Object> tasks,
135                                                       boolean setup )
136         throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
137         PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
138         NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
139     {
140         if ( project.equals( ProjectDependencyGraphStub.A ) )
141         {
142             return getProjectAExecutionPlan();
143         }
144         if ( project.equals( ProjectDependencyGraphStub.B ) )
145         {
146             return getProjectBExecutionPlan();
147         }
148         // The remaining are basically "for future expansion"
149         List<MojoExecution> me = new ArrayList<>();
150         me.add( createMojoExecution( "resources", "default-resources", PROCESS_RESOURCES ) );
151         me.add( createMojoExecution( "compile", "default-compile", COMPILE ) );
152         return createExecutionPlan( project, me );
153     }
154 
155     public MavenExecutionPlan calculateExecutionPlan( MavenSession session, MavenProject project, List<Object> tasks )
156         throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
157         PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
158         NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
159     {
160         return calculateExecutionPlan( session, project, tasks, true );
161     }
162 
163     public void setupMojoExecution( MavenSession session, MavenProject project, MojoExecution mojoExecution, Set<MojoDescriptor> alreadyForkedExecutions )
164         throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
165         MojoNotFoundException, InvalidPluginDescriptorException, NoPluginFoundForPrefixException,
166         LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException
167     {
168     }
169 
170     public static MavenExecutionPlan getProjectAExecutionPlan()
171         throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
172         PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
173         NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
174     {
175         List<MojoExecution> me = new ArrayList<>();
176         me.add( createMojoExecution( "initialize", "default-initialize", INITIALIZE ) );
177         me.add( createMojoExecution( "resources", "default-resources", PROCESS_RESOURCES ) );
178         me.add( createMojoExecution( "compile", "default-compile", COMPILE ) );
179         me.add( createMojoExecution( "testResources", "default-testResources", PROCESS_TEST_RESOURCES ) );
180         me.add( createMojoExecution( "testCompile", "default-testCompile", TEST_COMPILE ) );
181         me.add( createMojoExecution( "test", "default-test", TEST ) );
182         me.add( createMojoExecution( "war", "default-war", PACKAGE ) );
183         me.add( createMojoExecution( "install", "default-install", INSTALL ) );
184         return createExecutionPlan( ProjectDependencyGraphStub.A.getExecutionProject(), me );
185     }
186 
187     public static MavenExecutionPlan getProjectBExecutionPlan()
188         throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
189         PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
190         NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
191     {
192         List<MojoExecution> me = new ArrayList<>();
193         me.add( createMojoExecution( "enforce", "enforce-versions", VALIDATE ) );
194         me.add( createMojoExecution( "resources", "default-resources", PROCESS_RESOURCES ) );
195         me.add( createMojoExecution( "compile", "default-compile", COMPILE ) );
196         me.add( createMojoExecution( "testResources", "default-testResources", PROCESS_TEST_RESOURCES ) );
197         me.add( createMojoExecution( "testCompile", "default-testCompile", TEST_COMPILE ) );
198         me.add( createMojoExecution( "test", "default-test", TEST ) );
199         return createExecutionPlan( ProjectDependencyGraphStub.B.getExecutionProject(), me );
200     }
201 
202 
203     private static MavenExecutionPlan createExecutionPlan( MavenProject project, List<MojoExecution> mojoExecutions )
204         throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
205         NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
206         LifecyclePhaseNotFoundException, LifecycleNotFoundException
207     {
208         final List<ExecutionPlanItem> planItemList =
209             ExecutionPlanItem.createExecutionPlanItems( project, mojoExecutions );
210         return new MavenExecutionPlan( planItemList, getDefaultLifecycles());
211     }
212 
213     private static DefaultLifecycles getDefaultLifecycles()
214     {
215         try
216         {
217             return DefaultLifecyclesStub.createDefaultLifecycles();
218         }
219         catch ( ComponentLookupException e )
220         {
221             // ignore
222             return null;
223         }
224     }
225 
226     private static MojoExecution createMojoExecution( String goal, String executionId, MojoDescriptor mojoDescriptor )
227     {
228         InputSource defaultBindings = new InputSource();
229         defaultBindings.setModelId( DefaultLifecyclePluginAnalyzer.DEFAULTLIFECYCLEBINDINGS_MODELID );
230 
231         final Plugin plugin = mojoDescriptor.getPluginDescriptor().getPlugin();
232         plugin.setLocation( "version", new InputLocation( 12, 34, defaultBindings ) );
233         MojoExecution result = new MojoExecution( plugin, goal, executionId );
234         result.setConfiguration( new Xpp3Dom( executionId + "-" + goal ) );
235         result.setMojoDescriptor( mojoDescriptor );
236         result.setLifecyclePhase( mojoDescriptor.getPhase() );
237 
238         return result;
239 
240     }
241 
242     public static MojoDescriptor createMojoDescriptor( String phaseName )
243     {
244         return createMojoDescriptor( phaseName, false );
245     }
246 
247     public static MojoDescriptor createMojoDescriptor( String phaseName, boolean threadSafe )
248     {
249         final MojoDescriptor mojoDescriptor = new MojoDescriptor();
250         mojoDescriptor.setPhase( phaseName );
251         final PluginDescriptor descriptor = new PluginDescriptor();
252         Plugin plugin = new Plugin();
253         plugin.setGroupId( "org.apache.maven.test.MavenExecutionPlan" );
254         plugin.setArtifactId( "stub-plugin-" + phaseName );
255         descriptor.setPlugin( plugin );
256         descriptor.setArtifactId( "artifact." + phaseName );
257         mojoDescriptor.setPluginDescriptor( descriptor );
258         mojoDescriptor.setThreadSafe( threadSafe );
259         return mojoDescriptor;
260     }
261 
262 
263     public static Set<String> getScopes()
264     {
265         return new HashSet<>( Arrays.asList( "compile" ) );
266     }
267 
268 }