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