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