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      public int getNumberOfExceutions( ProjectBuildList projectBuildList )
92          throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
93          NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
94          LifecyclePhaseNotFoundException, LifecycleNotFoundException
95      {
96          int result = 0;
97          for ( ProjectSegment projectBuild : projectBuildList )
98          {
99              MavenExecutionPlan plan = calculateExecutionPlan( projectBuild.getSession(), projectBuild.getProject(),
100                                                               projectBuild.getTaskSegment().getTasks() );
101             result += plan.size();
102         }
103         return result;
104     }
105 
106     public void calculateForkedExecutions( MojoExecution mojoExecution, MavenSession session )
107         throws MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
108         PluginDescriptorParsingException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
109         LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException
110     {
111         // Maybe do something ?
112     }
113 
114     public MavenExecutionPlan calculateExecutionPlan( MavenSession session, MavenProject project, List<Object> tasks,
115                                                       boolean setup )
116         throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
117         PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
118         NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
119     {
120         if ( project.equals( ProjectDependencyGraphStub.A ) )
121         {
122             return getProjectAExceutionPlan();
123         }
124         if ( project.equals( ProjectDependencyGraphStub.B ) )
125         {
126             return getProjectBExecutionPlan();
127         }
128         // The remaining are basically "for future expansion"
129         List<MojoExecution> me = new ArrayList<MojoExecution>();
130         me.add( createMojoExecution( "resources", "default-resources", PROCESS_RESOURCES ) );
131         me.add( createMojoExecution( "compile", "default-compile", COMPILE ) );
132         return createExecutionPlan( project, me );
133     }
134 
135     public MavenExecutionPlan calculateExecutionPlan( MavenSession session, MavenProject project, List<Object> tasks )
136         throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
137         PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
138         NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
139     {
140         return calculateExecutionPlan( session, project, tasks, true );
141     }
142 
143     public void setupMojoExecution( MavenSession session, MavenProject project, MojoExecution mojoExecution )
144         throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
145         MojoNotFoundException, InvalidPluginDescriptorException, NoPluginFoundForPrefixException,
146         LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException
147     {
148     }
149 
150     public static MavenExecutionPlan getProjectAExceutionPlan()
151         throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
152         PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
153         NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
154     {
155         List<MojoExecution> me = new ArrayList<MojoExecution>();
156         me.add( createMojoExecution( "initialize", "default-initialize", INITIALIZE ) );
157         me.add( createMojoExecution( "resources", "default-resources", PROCESS_RESOURCES ) );
158         me.add( createMojoExecution( "compile", "default-compile", COMPILE ) );
159         me.add( createMojoExecution( "testResources", "default-testResources", PROCESS_TEST_RESOURCES ) );
160         me.add( createMojoExecution( "testCompile", "default-testCompile", TEST_COMPILE ) );
161         me.add( createMojoExecution( "test", "default-test", TEST ) );
162         me.add( createMojoExecution( "war", "default-war", PACKAGE ) );
163         me.add( createMojoExecution( "install", "default-install", INSTALL ) );
164         return createExecutionPlan( ProjectDependencyGraphStub.A.getExecutionProject(), me );
165     }
166 
167     public static MavenExecutionPlan getProjectBExecutionPlan()
168         throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
169         PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
170         NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
171     {
172         List<MojoExecution> me = new ArrayList<MojoExecution>();
173         me.add( createMojoExecution( "enforce", "enforce-versions", VALIDATE ) );
174         me.add( createMojoExecution( "resources", "default-resources", PROCESS_RESOURCES ) );
175         me.add( createMojoExecution( "compile", "default-compile", COMPILE ) );
176         me.add( createMojoExecution( "testResources", "default-testResources", PROCESS_TEST_RESOURCES ) );
177         me.add( createMojoExecution( "testCompile", "default-testCompile", TEST_COMPILE ) );
178         me.add( createMojoExecution( "test", "default-test", TEST ) );
179         return createExecutionPlan( ProjectDependencyGraphStub.B.getExecutionProject(), me );
180     }
181 
182 
183     private static MavenExecutionPlan createExecutionPlan( MavenProject project, List<MojoExecution> mojoExecutions )
184         throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
185         NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
186         LifecyclePhaseNotFoundException, LifecycleNotFoundException
187     {
188         final List<ExecutionPlanItem> planItemList =
189             ExecutionPlanItem.createExecutionPlanItems( project, mojoExecutions );
190         return new MavenExecutionPlan( planItemList, DefaultLifecyclesStub.createDefaultLifecycles() );
191     }
192 
193     private static MojoExecution createMojoExecution( String goal, String executionId, MojoDescriptor mojoDescriptor )
194     {
195         final Plugin plugin = mojoDescriptor.getPluginDescriptor().getPlugin();
196         MojoExecution result = new MojoExecution( plugin, goal, executionId );
197         result.setConfiguration( new Xpp3Dom( executionId + "-" + goal ) );
198         result.setMojoDescriptor( mojoDescriptor );
199         result.setLifecyclePhase( mojoDescriptor.getPhase() );
200 
201         return result;
202 
203     }
204 
205     public static MojoDescriptor createMojoDescriptor( String phaseName )
206     {
207         return createMojoDescriptor( phaseName, false );
208     }
209 
210     public static MojoDescriptor createMojoDescriptor( String phaseName, boolean threadSafe )
211     {
212         final MojoDescriptor mojoDescriptor = new MojoDescriptor();
213         mojoDescriptor.setPhase( phaseName );
214         final PluginDescriptor descriptor = new PluginDescriptor();
215         Plugin plugin = new Plugin();
216         plugin.setArtifactId( "org.apache.maven.test.MavenExecutionPlan" );
217         plugin.setGroupId( "stub-plugin-" + phaseName );
218         descriptor.setPlugin( plugin );
219         descriptor.setArtifactId( "artifact." + phaseName );
220         mojoDescriptor.setPluginDescriptor( descriptor );
221         mojoDescriptor.setThreadSafe( threadSafe );
222         return mojoDescriptor;
223     }
224 
225 
226     public static Set<String> getScopes()
227     {
228         return new HashSet<String>( Arrays.asList( "compile" ) );
229     }
230 
231 }