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;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.Arrays;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import org.apache.maven.execution.MavenSession;
31  import org.apache.maven.lifecycle.internal.LifecycleExecutionPlanCalculator;
32  import org.apache.maven.lifecycle.internal.LifecycleStarter;
33  import org.apache.maven.lifecycle.internal.LifecycleTaskSegmentCalculator;
34  import org.apache.maven.lifecycle.internal.MojoDescriptorCreator;
35  import org.apache.maven.lifecycle.internal.MojoExecutor;
36  import org.apache.maven.lifecycle.internal.ProjectIndex;
37  import org.apache.maven.lifecycle.internal.TaskSegment;
38  import org.apache.maven.model.Plugin;
39  import org.apache.maven.plugin.InvalidPluginDescriptorException;
40  import org.apache.maven.plugin.MojoExecution;
41  import org.apache.maven.plugin.MojoNotFoundException;
42  import org.apache.maven.plugin.PluginDescriptorParsingException;
43  import org.apache.maven.plugin.PluginManagerException;
44  import org.apache.maven.plugin.PluginNotFoundException;
45  import org.apache.maven.plugin.PluginResolutionException;
46  import org.apache.maven.plugin.descriptor.MojoDescriptor;
47  import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
48  import org.apache.maven.plugin.version.PluginVersionResolutionException;
49  import org.apache.maven.project.MavenProject;
50  
51  /**
52   * A facade that provides lifecycle services to components outside maven core.
53   *
54   * Note that this component is not normally used from within core itself.
55   *
56   * @author Jason van Zyl
57   * @author Benjamin Bentmann
58   * @author Kristian Rosenvold
59   */
60  @Singleton
61  @Named
62  public class DefaultLifecycleExecutor implements LifecycleExecutor {
63  
64      @Inject
65      private LifeCyclePluginAnalyzer lifeCyclePluginAnalyzer;
66  
67      @Inject
68      private DefaultLifecycles defaultLifeCycles;
69  
70      @Inject
71      private LifecycleTaskSegmentCalculator lifecycleTaskSegmentCalculator;
72  
73      @Inject
74      private LifecycleExecutionPlanCalculator lifecycleExecutionPlanCalculator;
75  
76      @Inject
77      private MojoExecutor mojoExecutor;
78  
79      @Inject
80      private LifecycleStarter lifecycleStarter;
81  
82      public void execute(MavenSession session) {
83          lifecycleStarter.execute(session);
84      }
85  
86      @Inject
87      private MojoDescriptorCreator mojoDescriptorCreator;
88  
89      // These methods deal with construction intact Plugin object that look like they come from a standard
90      // <plugin/> block in a Maven POM. We have to do some wiggling to pull the sources of information
91      // together and this really shows the problem of constructing a sensible default configuration but
92      // it's all encapsulated here so it appears normalized to the POM builder.
93  
94      // We are going to take the project packaging and find all plugin in the default lifecycle and create
95      // fully populated Plugin objects, including executions with goals and default configuration taken
96      // from the plugin.xml inside a plugin.
97      //
98      // TODO This whole method could probably removed by injecting lifeCyclePluginAnalyzer straight into client site.
99      // TODO But for some reason the whole plexus appcontext refuses to start when I try this.
100 
101     public Set<Plugin> getPluginsBoundByDefaultToAllLifecycles(String packaging) {
102         return lifeCyclePluginAnalyzer.getPluginsBoundByDefaultToAllLifecycles(packaging);
103     }
104 
105     // USED BY MAVEN HELP PLUGIN
106 
107     @Deprecated
108     public Map<String, Lifecycle> getPhaseToLifecycleMap() {
109         return defaultLifeCycles.getPhaseToLifecycleMap();
110     }
111 
112     // NOTE: Backward-compat with maven-help-plugin:2.1
113 
114     @SuppressWarnings({"UnusedDeclaration"})
115     MojoDescriptor getMojoDescriptor(
116             String task,
117             MavenSession session,
118             MavenProject project,
119             String invokedVia,
120             boolean canUsePrefix,
121             boolean isOptionalMojo)
122             throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
123                     MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
124                     PluginVersionResolutionException {
125         return mojoDescriptorCreator.getMojoDescriptor(task, session, project);
126     }
127 
128     // Used by m2eclipse
129 
130     @SuppressWarnings({"UnusedDeclaration"})
131     public MavenExecutionPlan calculateExecutionPlan(MavenSession session, boolean setup, String... tasks)
132             throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
133                     MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
134                     PluginManagerException, LifecyclePhaseNotFoundException, LifecycleNotFoundException,
135                     PluginVersionResolutionException {
136         List<TaskSegment> taskSegments =
137                 lifecycleTaskSegmentCalculator.calculateTaskSegments(session, Arrays.asList(tasks));
138 
139         TaskSegment mergedSegment = new TaskSegment(false);
140 
141         for (TaskSegment taskSegment : taskSegments) {
142             mergedSegment.getTasks().addAll(taskSegment.getTasks());
143         }
144 
145         return lifecycleExecutionPlanCalculator.calculateExecutionPlan(
146                 session, session.getCurrentProject(), mergedSegment.getTasks(), setup);
147     }
148 
149     public MavenExecutionPlan calculateExecutionPlan(MavenSession session, String... tasks)
150             throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
151                     MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
152                     PluginManagerException, LifecyclePhaseNotFoundException, LifecycleNotFoundException,
153                     PluginVersionResolutionException {
154         return calculateExecutionPlan(session, true, tasks);
155     }
156 
157     // Site 3.x
158     public void calculateForkedExecutions(MojoExecution mojoExecution, MavenSession session)
159             throws MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
160                     PluginDescriptorParsingException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
161                     LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException {
162         lifecycleExecutionPlanCalculator.calculateForkedExecutions(mojoExecution, session);
163     }
164 
165     // Site 3.x
166     public List<MavenProject> executeForkedExecutions(MojoExecution mojoExecution, MavenSession session)
167             throws LifecycleExecutionException {
168         return mojoExecutor.executeForkedExecutions(mojoExecution, session, new ProjectIndex(session.getProjects()));
169     }
170 }