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