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