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