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     @SuppressWarnings( { "UnusedDeclaration" } )
111     @Deprecated
112     public Map<String, Lifecycle> getPhaseToLifecycleMap()
113     {
114         return defaultLifeCycles.getPhaseToLifecycleMap();
115     }
116 
117     // NOTE: Backward-compat with maven-help-plugin:2.1
118 
119     @SuppressWarnings( { "UnusedDeclaration" } )
120     MojoDescriptor getMojoDescriptor( String task, MavenSession session, MavenProject project, String invokedVia,
121                                       boolean canUsePrefix, boolean isOptionalMojo )
122         throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
123         MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
124         PluginVersionResolutionException
125     {
126         return mojoDescriptorCreator.getMojoDescriptor( task, session, project );
127     }
128 
129     // Used by m2eclipse
130 
131     @SuppressWarnings( { "UnusedDeclaration" } )
132     public MavenExecutionPlan calculateExecutionPlan( MavenSession session, boolean setup, String... tasks )
133         throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
134         MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
135         PluginManagerException, LifecyclePhaseNotFoundException, LifecycleNotFoundException,
136         PluginVersionResolutionException
137     {
138         List<TaskSegment> taskSegments =
139             lifecycleTaskSegmentCalculator.calculateTaskSegments( session, Arrays.asList( tasks ) );
140 
141         TaskSegment mergedSegment = new TaskSegment( false );
142 
143         for ( TaskSegment taskSegment : taskSegments )
144         {
145             mergedSegment.getTasks().addAll( taskSegment.getTasks() );
146         }
147 
148         return lifecycleExecutionPlanCalculator.calculateExecutionPlan( session, session.getCurrentProject(),
149                                                                         mergedSegment.getTasks(), setup );
150     }
151 
152     public MavenExecutionPlan calculateExecutionPlan( MavenSession session, String... tasks )
153         throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
154         MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
155         PluginManagerException, LifecyclePhaseNotFoundException, LifecycleNotFoundException,
156         PluginVersionResolutionException
157     {
158         return calculateExecutionPlan( session, true, tasks );
159     }
160 
161     // Site 3.x
162     public void calculateForkedExecutions( MojoExecution mojoExecution, MavenSession session )
163         throws MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
164         PluginDescriptorParsingException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
165         LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException
166     {
167         lifecycleExecutionPlanCalculator.calculateForkedExecutions( mojoExecution, session );
168     }
169 
170 
171     // Site 3.x
172     public List<MavenProject> executeForkedExecutions( MojoExecution mojoExecution, MavenSession session )
173         throws LifecycleExecutionException
174     {
175         return mojoExecutor.executeForkedExecutions( mojoExecution, session, new ProjectIndex( session.getProjects() ) );
176     }
177 
178 }