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   */
56  public class LifecycleExecutionPlanCalculatorStub implements LifecycleExecutionPlanCalculator {
57      // clean
58  
59      public static final MojoDescriptor PRE_CLEAN = createMojoDescriptor("pre-clean");
60  
61      public static final MojoDescriptor CLEAN = createMojoDescriptor("clean");
62  
63      public static final MojoDescriptor POST_CLEAN = createMojoDescriptor("post-clean");
64  
65      // default (or at least some of them)
66  
67      public static final MojoDescriptor VALIDATE = createMojoDescriptor("validate");
68  
69      public static final MojoDescriptor INITIALIZE = createMojoDescriptor("initialize");
70  
71      public static final MojoDescriptor TEST_COMPILE = createMojoDescriptor("test-compile");
72  
73      public static final MojoDescriptor PROCESS_TEST_RESOURCES = createMojoDescriptor("process-test-resources");
74  
75      public static final MojoDescriptor PROCESS_RESOURCES = createMojoDescriptor("process-resources");
76  
77      public static final MojoDescriptor COMPILE = createMojoDescriptor("compile", true);
78  
79      public static final MojoDescriptor TEST = createMojoDescriptor("test");
80  
81      public static final MojoDescriptor PACKAGE = createMojoDescriptor("package");
82  
83      public static final MojoDescriptor INSTALL = createMojoDescriptor("install");
84  
85      // site
86  
87      public static final MojoDescriptor PRE_SITE = createMojoDescriptor("pre-site");
88  
89      public static final MojoDescriptor SITE = createMojoDescriptor("site");
90  
91      public static final MojoDescriptor POST_SITE = createMojoDescriptor("post-site");
92  
93      public static final MojoDescriptor SITE_DEPLOY = createMojoDescriptor("site-deploy");
94  
95      // wrapper
96  
97      public static final 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,
106                     PluginResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException {
107         int result = 0;
108         for (ProjectSegment projectBuild : projectBuildList) {
109             MavenExecutionPlan plan = calculateExecutionPlan(
110                     projectBuild.getSession(),
111                     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,
121                     PluginResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException {
122         return getNumberOfExceutions(projectBuildList);
123     }
124 
125     public void calculateForkedExecutions(MojoExecution mojoExecution, MavenSession session)
126             throws MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
127                     PluginDescriptorParsingException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
128                     LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException {
129         // Maybe do something ?
130     }
131 
132     public MavenExecutionPlan calculateExecutionPlan(
133             MavenSession session, MavenProject project, List<Object> tasks, boolean setup)
134             throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
135                     PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
136                     NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException {
137         if (project.equals(ProjectDependencyGraphStub.A)) {
138             return getProjectAExecutionPlan();
139         }
140         if (project.equals(ProjectDependencyGraphStub.B)) {
141             return getProjectBExecutionPlan();
142         }
143         // The remaining are basically "for future expansion"
144         List<MojoExecution> me = new ArrayList<>();
145         me.add(createMojoExecution("resources", "default-resources", PROCESS_RESOURCES));
146         me.add(createMojoExecution("compile", "default-compile", COMPILE));
147         return createExecutionPlan(project, me);
148     }
149 
150     public MavenExecutionPlan calculateExecutionPlan(MavenSession session, MavenProject project, List<Object> tasks)
151             throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
152                     PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
153                     NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException {
154         return calculateExecutionPlan(session, project, tasks, true);
155     }
156 
157     public void setupMojoExecution(
158             MavenSession session,
159             MavenProject project,
160             MojoExecution mojoExecution,
161             Set<MojoDescriptor> alreadyForkedExecutions)
162             throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
163                     MojoNotFoundException, InvalidPluginDescriptorException, NoPluginFoundForPrefixException,
164                     LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException {}
165 
166     public static MavenExecutionPlan getProjectAExecutionPlan()
167             throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
168                     PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
169                     NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException {
170         List<MojoExecution> me = new ArrayList<>();
171         me.add(createMojoExecution("initialize", "default-initialize", INITIALIZE));
172         me.add(createMojoExecution("resources", "default-resources", PROCESS_RESOURCES));
173         me.add(createMojoExecution("compile", "default-compile", COMPILE));
174         me.add(createMojoExecution("testResources", "default-testResources", PROCESS_TEST_RESOURCES));
175         me.add(createMojoExecution("testCompile", "default-testCompile", TEST_COMPILE));
176         me.add(createMojoExecution("test", "default-test", TEST));
177         me.add(createMojoExecution("war", "default-war", PACKAGE));
178         me.add(createMojoExecution("install", "default-install", INSTALL));
179         return createExecutionPlan(ProjectDependencyGraphStub.A.getExecutionProject(), me);
180     }
181 
182     public static MavenExecutionPlan getProjectBExecutionPlan()
183             throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
184                     PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
185                     NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException {
186         List<MojoExecution> me = new ArrayList<>();
187         me.add(createMojoExecution("enforce", "enforce-versions", VALIDATE));
188         me.add(createMojoExecution("resources", "default-resources", PROCESS_RESOURCES));
189         me.add(createMojoExecution("compile", "default-compile", COMPILE));
190         me.add(createMojoExecution("testResources", "default-testResources", PROCESS_TEST_RESOURCES));
191         me.add(createMojoExecution("testCompile", "default-testCompile", TEST_COMPILE));
192         me.add(createMojoExecution("test", "default-test", TEST));
193         return createExecutionPlan(ProjectDependencyGraphStub.B.getExecutionProject(), me);
194     }
195 
196     private static MavenExecutionPlan createExecutionPlan(MavenProject project, List<MojoExecution> mojoExecutions)
197             throws InvalidPluginDescriptorException, PluginVersionResolutionException, PluginDescriptorParsingException,
198                     NoPluginFoundForPrefixException, MojoNotFoundException, PluginNotFoundException,
199                     PluginResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException {
200         final List<ExecutionPlanItem> planItemList =
201                 ExecutionPlanItem.createExecutionPlanItems(project, mojoExecutions);
202         return new MavenExecutionPlan(planItemList, getDefaultLifecycles());
203     }
204 
205     private static DefaultLifecycles getDefaultLifecycles() {
206         try {
207             return DefaultLifecyclesStub.createDefaultLifecycles();
208         } catch (ComponentLookupException e) {
209             // ignore
210             return null;
211         }
212     }
213 
214     private static MojoExecution createMojoExecution(String goal, String executionId, MojoDescriptor mojoDescriptor) {
215         InputSource defaultBindings = new InputSource();
216         defaultBindings.setModelId(DefaultLifecyclePluginAnalyzer.DEFAULTLIFECYCLEBINDINGS_MODELID);
217 
218         final Plugin plugin = mojoDescriptor.getPluginDescriptor().getPlugin();
219         plugin.setLocation("version", new InputLocation(12, 34, defaultBindings));
220         MojoExecution result = new MojoExecution(plugin, goal, executionId);
221         result.setConfiguration(new XmlNodeImpl(executionId + "-" + goal));
222         result.setMojoDescriptor(mojoDescriptor);
223         result.setLifecyclePhase(mojoDescriptor.getPhase());
224 
225         return result;
226     }
227 
228     public static MojoDescriptor createMojoDescriptor(String phaseName) {
229         return createMojoDescriptor(phaseName, false);
230     }
231 
232     public static MojoDescriptor createMojoDescriptor(String phaseName, boolean threadSafe) {
233         final MojoDescriptor mojoDescriptor = new MojoDescriptor();
234         mojoDescriptor.setPhase(phaseName);
235         final PluginDescriptor descriptor = new PluginDescriptor();
236         Plugin plugin = new Plugin();
237         plugin.setGroupId("org.apache.maven.test.MavenExecutionPlan");
238         plugin.setArtifactId("stub-plugin-" + phaseName);
239         descriptor.setPlugin(plugin);
240         descriptor.setArtifactId("artifact." + phaseName);
241         mojoDescriptor.setPluginDescriptor(descriptor);
242         mojoDescriptor.setThreadSafe(threadSafe);
243         return mojoDescriptor;
244     }
245 
246     public static Set<String> getScopes() {
247         return new HashSet<>(Arrays.asList("compile"));
248     }
249 }