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