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.List;
46  import java.util.Map;
47  import java.util.Set;
48  
49  /**
50   * A facade that provides lifecycle services to components outside maven core.
51   *
52   * Note that this component is not normally used from within core itself.
53   *
54   * @author Jason van Zyl
55   * @author Benjamin Bentmann
56   * @author Kristian Rosenvold
57   */
58  @Component( role = LifecycleExecutor.class )
59  public class DefaultLifecycleExecutor
60      implements LifecycleExecutor
61  {
62  
63      @Requirement
64      private LifeCyclePluginAnalyzer lifeCyclePluginAnalyzer;
65  
66      @Requirement
67      private DefaultLifecycles defaultLifeCycles;
68  
69      @Requirement
70      private LifecycleTaskSegmentCalculator lifecycleTaskSegmentCalculator;
71  
72      @Requirement
73      private LifecycleExecutionPlanCalculator lifecycleExecutionPlanCalculator;
74  
75      @Requirement
76      private MojoExecutor mojoExecutor;
77  
78      @Requirement
79      private LifecycleStarter lifecycleStarter;
80  
81  
82      public void execute( MavenSession session )
83      {
84          lifecycleStarter.execute( session );
85      }
86  
87      @Requirement
88      private MojoDescriptorCreator mojoDescriptorCreator;
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     {
104         return lifeCyclePluginAnalyzer.getPluginsBoundByDefaultToAllLifecycles( packaging );
105     }
106 
107     // USED BY MAVEN HELP PLUGIN
108 
109     @SuppressWarnings( { "UnusedDeclaration" } )
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, String... tasks )
132         throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
133         MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
134         PluginManagerException, LifecyclePhaseNotFoundException, LifecycleNotFoundException,
135         PluginVersionResolutionException
136     {
137 
138         List<TaskSegment> taskSegments = lifecycleTaskSegmentCalculator.calculateTaskSegments( session );
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() );
149     }
150 
151     // Site 3.x
152     public void calculateForkedExecutions( MojoExecution mojoExecution, MavenSession session )
153         throws MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
154         PluginDescriptorParsingException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
155         LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException
156     {
157         lifecycleExecutionPlanCalculator.calculateForkedExecutions( mojoExecution, session );
158     }
159 
160 
161     // Site 3.x
162     public List<MavenProject> executeForkedExecutions( MojoExecution mojoExecution, MavenSession session )
163         throws LifecycleExecutionException
164     {
165         return mojoExecutor.executeForkedExecutions( mojoExecution, session, new ProjectIndex( session.getProjects() ) );
166     }
167 
168 }