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