View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.lifecycle.internal;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.apache.maven.execution.ExecutionEvent;
29  import org.apache.maven.execution.MavenExecutionResult;
30  import org.apache.maven.execution.MavenSession;
31  import org.apache.maven.lifecycle.DefaultLifecycles;
32  import org.apache.maven.lifecycle.MissingProjectException;
33  import org.apache.maven.lifecycle.NoGoalSpecifiedException;
34  import org.apache.maven.lifecycle.internal.builder.Builder;
35  import org.apache.maven.lifecycle.internal.builder.BuilderNotFoundException;
36  import org.apache.maven.session.scope.internal.SessionScope;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  /**
41   * Starts the build life cycle
42   *
43   * @author Jason van Zyl
44   * @author Benjamin Bentmann
45   * @author Kristian Rosenvold
46   */
47  @Singleton
48  @Named
49  public class LifecycleStarter {
50      private final Logger logger = LoggerFactory.getLogger(getClass());
51  
52      @Inject
53      private ExecutionEventCatapult eventCatapult;
54  
55      @Inject
56      private DefaultLifecycles defaultLifeCycles;
57  
58      @Inject
59      private BuildListCalculator buildListCalculator;
60  
61      @Inject
62      private LifecycleDebugLogger lifecycleDebugLogger;
63  
64      @Inject
65      private LifecycleTaskSegmentCalculator lifecycleTaskSegmentCalculator;
66  
67      @Inject
68      private Map<String, Builder> builders;
69  
70      @Inject
71      private SessionScope sessionScope;
72  
73      public void execute(MavenSession session) {
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              if (buildExecutionRequiresProject(session) && projectIsNotPresent(session)) {
82                  throw new MissingProjectException("The goal you specified requires a project to execute"
83                          + " but there is no POM in this directory (" + session.getExecutionRootDirectory() + ")."
84                          + " Please verify you invoked Maven from the correct directory.");
85              }
86  
87              List<TaskSegment> taskSegments = lifecycleTaskSegmentCalculator.calculateTaskSegments(session);
88              projectBuilds = buildListCalculator.calculateProjectBuilds(session, taskSegments);
89  
90              if (projectBuilds.isEmpty()) {
91                  throw new NoGoalSpecifiedException("No goals have been specified for this build."
92                          + " You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or"
93                          + " <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>."
94                          + " Available lifecycle phases are: " + defaultLifeCycles.getLifecyclePhaseList() + ".");
95              }
96  
97              ProjectIndex projectIndex = new ProjectIndex(session.getProjects());
98  
99              if (logger.isDebugEnabled()) {
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                 throw new BuilderNotFoundException(
111                         String.format("The builder requested using id = %s cannot be" + " found", builderId));
112             }
113 
114             int degreeOfConcurrency = session.getRequest().getDegreeOfConcurrency();
115             if (degreeOfConcurrency > 1) {
116                 logger.info("");
117                 logger.info(String.format(
118                         "Using the %s implementation with a thread count of %d",
119                         builder.getClass().getSimpleName(), degreeOfConcurrency));
120             }
121             builder.build(session, reactorContext, projectBuilds, taskSegments, reactorBuildStatus);
122 
123         } catch (Exception e) {
124             result.addException(e);
125         } finally {
126             eventCatapult.fire(ExecutionEvent.Type.SessionEnded, session, null);
127         }
128     }
129 
130     private boolean buildExecutionRequiresProject(MavenSession session) {
131         return lifecycleTaskSegmentCalculator.requiresProject(session);
132     }
133 
134     private boolean projectIsNotPresent(MavenSession session) {
135         return !session.getRequest().isProjectPresent();
136     }
137 }