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