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