View Javadoc
1   package org.apache.maven.lifecycle.internal;
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.List;
23  import java.util.Map;
24  
25  import org.apache.maven.execution.ExecutionEvent;
26  import org.apache.maven.execution.MavenExecutionResult;
27  import org.apache.maven.execution.MavenSession;
28  import org.apache.maven.lifecycle.DefaultLifecycles;
29  import org.apache.maven.lifecycle.MissingProjectException;
30  import org.apache.maven.lifecycle.NoGoalSpecifiedException;
31  import org.apache.maven.lifecycle.internal.builder.Builder;
32  import org.apache.maven.lifecycle.internal.builder.BuilderNotFoundException;
33  import org.apache.maven.session.scope.internal.SessionScope;
34  import org.codehaus.plexus.component.annotations.Component;
35  import org.codehaus.plexus.component.annotations.Requirement;
36  import org.codehaus.plexus.logging.Logger;
37  
38  /**
39   * Starts the build life cycle
40   *
41   * @author Jason van Zyl
42   * @author Benjamin Bentmann
43   * @author Kristian Rosenvold
44   */
45  @Component( role = LifecycleStarter.class )
46  public class LifecycleStarter
47  {
48      @Requirement
49      private ExecutionEventCatapult eventCatapult;
50  
51      @Requirement
52      private DefaultLifecycles defaultLifeCycles;
53  
54      @Requirement
55      private Logger logger;
56  
57      @Requirement
58      private BuildListCalculator buildListCalculator;
59  
60      @Requirement
61      private LifecycleDebugLogger lifecycleDebugLogger;
62  
63      @Requirement
64      private LifecycleTaskSegmentCalculator lifecycleTaskSegmentCalculator;
65  
66      @Requirement
67      private Map<String, Builder> builders;
68      
69      @Requirement
70      private SessionScope sessionScope;
71  
72      public void execute( MavenSession session )
73      {
74          eventCatapult.fire( ExecutionEvent.Type.SessionStarted, session, null );
75  
76          ReactorContext reactorContext = null;
77          ProjectBuildList projectBuilds = null;
78          MavenExecutionResult result = session.getResult();
79  
80          try
81          {
82              if ( buildExecutionRequiresProject( session ) && projectIsNotPresent( session ) )
83              {
84                  throw new MissingProjectException( "The goal you specified requires a project to execute"
85                      + " but there is no POM in this directory (" + session.getExecutionRootDirectory() + ")."
86                      + " Please verify you invoked Maven from the correct directory." );
87              }
88  
89              List<TaskSegment> taskSegments = lifecycleTaskSegmentCalculator.calculateTaskSegments( session );
90              projectBuilds = buildListCalculator.calculateProjectBuilds( session, taskSegments );
91  
92              if ( projectBuilds.isEmpty() )
93              {
94                  throw new NoGoalSpecifiedException( "No goals have been specified for this build."
95                      + " You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or"
96                      + " <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>."
97                      + " Available lifecycle phases are: " + defaultLifeCycles.getLifecyclePhaseList() + "." );
98              }
99  
100             ProjectIndex projectIndex = new ProjectIndex( session.getProjects() );
101 
102             if ( logger.isDebugEnabled() )
103             {
104                 lifecycleDebugLogger.debugReactorPlan( projectBuilds );
105             }
106 
107             ClassLoader oldContextClassLoader = Thread.currentThread().getContextClassLoader();
108             ReactorBuildStatus reactorBuildStatus = new ReactorBuildStatus( session.getProjectDependencyGraph() );
109             reactorContext =
110                 new ReactorContext( result, projectIndex, oldContextClassLoader, reactorBuildStatus,
111                                     sessionScope.memento() );
112 
113             String builderId = session.getRequest().getBuilderId();
114             Builder builder = builders.get( builderId );
115             if ( builder == null )
116             {
117                 throw new BuilderNotFoundException( String.format( "The builder requested using id = %s cannot be"
118                     + " found", builderId ) );
119             }
120 
121             int degreeOfConcurrency = session.getRequest().getDegreeOfConcurrency();
122             if ( degreeOfConcurrency >= 2 )
123             {
124                 logger.info( "" );
125                 logger.info( String.format( "Using the %s implementation with a thread count of %d",
126                                             builder.getClass().getSimpleName(), degreeOfConcurrency ) );
127             }
128             builder.build( session, reactorContext, projectBuilds, taskSegments, reactorBuildStatus );
129 
130         }
131         catch ( Exception e )
132         {
133             result.addException( e );
134         }
135         finally
136         {
137             eventCatapult.fire( ExecutionEvent.Type.SessionEnded, session, null );
138         }
139     }
140 
141     private boolean buildExecutionRequiresProject( MavenSession session )
142     {
143         return lifecycleTaskSegmentCalculator.requiresProject( session );
144     }
145 
146     private boolean projectIsNotPresent( MavenSession session )
147     {
148         return !session.getRequest().isProjectPresent();
149     }
150 }