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