View Javadoc
1   package org.apache.maven.lifecycle;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Arrays;
23  import java.util.List;
24  import java.util.Set;
25  
26  import javax.inject.Inject;
27  import javax.inject.Named;
28  import javax.inject.Singleton;
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.prefix.NoPluginFoundForPrefixException;
47  import org.apache.maven.plugin.version.PluginVersionResolutionException;
48  import org.apache.maven.project.MavenProject;
49  
50  /**
51   * A facade that provides lifecycle services to components outside maven core.
52   *
53   * Note that this component is not normally used from within core itself.
54   *
55   * @author Jason van Zyl
56   * @author Benjamin Bentmann
57   * @author Kristian Rosenvold
58   */
59  @Named
60  @Singleton
61  public class DefaultLifecycleExecutor
62      implements LifecycleExecutor
63  {
64  
65      private final LifeCyclePluginAnalyzer lifeCyclePluginAnalyzer;
66      private final DefaultLifecycles defaultLifeCycles;
67      private final LifecycleTaskSegmentCalculator lifecycleTaskSegmentCalculator;
68      private final LifecycleExecutionPlanCalculator lifecycleExecutionPlanCalculator;
69      private final MojoExecutor mojoExecutor;
70      private final LifecycleStarter lifecycleStarter;
71      private final MojoDescriptorCreator mojoDescriptorCreator;
72  
73      @Inject
74      public DefaultLifecycleExecutor(
75              LifeCyclePluginAnalyzer lifeCyclePluginAnalyzer,
76              DefaultLifecycles defaultLifeCycles,
77              LifecycleTaskSegmentCalculator lifecycleTaskSegmentCalculator,
78              LifecycleExecutionPlanCalculator lifecycleExecutionPlanCalculator,
79              MojoExecutor mojoExecutor,
80              LifecycleStarter lifecycleStarter,
81              MojoDescriptorCreator mojoDescriptorCreator )
82      {
83          this.lifeCyclePluginAnalyzer = lifeCyclePluginAnalyzer;
84          this.defaultLifeCycles = defaultLifeCycles;
85          this.lifecycleTaskSegmentCalculator = lifecycleTaskSegmentCalculator;
86          this.lifecycleExecutionPlanCalculator = lifecycleExecutionPlanCalculator;
87          this.mojoExecutor = mojoExecutor;
88          this.lifecycleStarter = lifecycleStarter;
89          this.mojoDescriptorCreator = mojoDescriptorCreator;
90      }
91  
92      public void execute( MavenSession session )
93      {
94          lifecycleStarter.execute( session );
95      }
96  
97      // These methods deal with construction intact Plugin object that look like they come from a standard
98      // <plugin/> block in a Maven POM. We have to do some wiggling to pull the sources of information
99      // together and this really shows the problem of constructing a sensible default configuration but
100     // it's all encapsulated here so it appears normalized to the POM builder.
101 
102     // We are going to take the project packaging and find all plugin in the default lifecycle and create
103     // fully populated Plugin objects, including executions with goals and default configuration taken
104     // from the plugin.xml inside a plugin.
105     //
106     // TODO This whole method could probably removed by injecting lifeCyclePluginAnalyzer straight into client site.
107     // TODO But for some reason the whole plexus appcontext refuses to start when I try this.
108 
109     public Set<Plugin> getPluginsBoundByDefaultToAllLifecycles( String packaging )
110     {
111         return lifeCyclePluginAnalyzer.getPluginsBoundByDefaultToAllLifecycles( packaging );
112     }
113 
114     // Used by m2eclipse
115 
116     @SuppressWarnings( { "UnusedDeclaration" } )
117     public MavenExecutionPlan calculateExecutionPlan( MavenSession session, boolean setup, String... tasks )
118         throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
119         MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
120         PluginManagerException, LifecyclePhaseNotFoundException, LifecycleNotFoundException,
121         PluginVersionResolutionException
122     {
123         List<TaskSegment> taskSegments =
124             lifecycleTaskSegmentCalculator.calculateTaskSegments( session, Arrays.asList( tasks ) );
125 
126         TaskSegment mergedSegment = new TaskSegment( false );
127 
128         for ( TaskSegment taskSegment : taskSegments )
129         {
130             mergedSegment.getTasks().addAll( taskSegment.getTasks() );
131         }
132 
133         return lifecycleExecutionPlanCalculator.calculateExecutionPlan( session, session.getCurrentProject(),
134                                                                         mergedSegment.getTasks(), setup );
135     }
136 
137     public MavenExecutionPlan calculateExecutionPlan( MavenSession session, String... tasks )
138         throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
139         MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
140         PluginManagerException, LifecyclePhaseNotFoundException, LifecycleNotFoundException,
141         PluginVersionResolutionException
142     {
143         return calculateExecutionPlan( session, true, tasks );
144     }
145 
146     // Site 3.x
147     public void calculateForkedExecutions( MojoExecution mojoExecution, MavenSession session )
148         throws MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
149         PluginDescriptorParsingException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
150         LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException
151     {
152         lifecycleExecutionPlanCalculator.calculateForkedExecutions( mojoExecution, session );
153     }
154 
155     // Site 3.x
156     public List<MavenProject> executeForkedExecutions( MojoExecution mojoExecution, MavenSession session )
157         throws LifecycleExecutionException
158     {
159         return mojoExecutor.executeForkedExecutions( mojoExecution, session,
160                                                      new ProjectIndex( session.getProjects() ) );
161     }
162 
163 }