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