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.HashSet;
26 import java.util.List;
27
28 import org.apache.maven.execution.BuildSuccess;
29 import org.apache.maven.execution.ExecutionEvent;
30 import org.apache.maven.execution.MavenSession;
31 import org.apache.maven.execution.ProjectExecutionEvent;
32 import org.apache.maven.execution.ProjectExecutionListener;
33 import org.apache.maven.internal.transformation.ConsumerPomArtifactTransformer;
34 import org.apache.maven.lifecycle.MavenExecutionPlan;
35 import org.apache.maven.lifecycle.internal.builder.BuilderCommon;
36 import org.apache.maven.plugin.MojoExecution;
37 import org.apache.maven.project.MavenProject;
38 import org.apache.maven.session.scope.internal.SessionScope;
39
40
41
42
43
44
45
46
47
48
49
50
51 @Named
52 @Singleton
53 public class LifecycleModuleBuilder {
54
55 private final MojoExecutor mojoExecutor;
56 private final BuilderCommon builderCommon;
57 private final ExecutionEventCatapult eventCatapult;
58 private final ProjectExecutionListener projectExecutionListener;
59 private final ConsumerPomArtifactTransformer consumerPomArtifactTransformer;
60 private final SessionScope sessionScope;
61
62 @Inject
63 public LifecycleModuleBuilder(
64 MojoExecutor mojoExecutor,
65 BuilderCommon builderCommon,
66 ExecutionEventCatapult eventCatapult,
67 List<ProjectExecutionListener> listeners,
68 ConsumerPomArtifactTransformer consumerPomArtifactTransformer,
69 SessionScope sessionScope) {
70 this.mojoExecutor = mojoExecutor;
71 this.builderCommon = builderCommon;
72 this.eventCatapult = eventCatapult;
73 this.projectExecutionListener = new CompoundProjectExecutionListener(listeners);
74 this.consumerPomArtifactTransformer = consumerPomArtifactTransformer;
75 this.sessionScope = sessionScope;
76 }
77
78 public void buildProject(
79 MavenSession session, ReactorContext reactorContext, MavenProject currentProject, TaskSegment taskSegment) {
80 buildProject(session, session, reactorContext, currentProject, taskSegment);
81 }
82
83 public void buildProject(
84 MavenSession session,
85 MavenSession rootSession,
86 ReactorContext reactorContext,
87 MavenProject currentProject,
88 TaskSegment taskSegment) {
89 session.setCurrentProject(currentProject);
90
91 long buildStartTime = System.currentTimeMillis();
92
93 try {
94
95 if (reactorContext.getReactorBuildStatus().isHaltedOrBlacklisted(currentProject)) {
96 eventCatapult.fire(ExecutionEvent.Type.ProjectSkipped, session, null);
97 return;
98 }
99
100 consumerPomArtifactTransformer.injectTransformedArtifacts(currentProject, session.getRepositorySession());
101
102 BuilderCommon.attachToThread(currentProject);
103
104 projectExecutionListener.beforeProjectExecution(new ProjectExecutionEvent(session, currentProject));
105
106 eventCatapult.fire(ExecutionEvent.Type.ProjectStarted, session, null);
107
108 MavenExecutionPlan executionPlan =
109 builderCommon.resolveBuildPlan(session, currentProject, taskSegment, new HashSet<>());
110 List<MojoExecution> mojoExecutions = executionPlan.getMojoExecutions();
111
112 projectExecutionListener.beforeProjectLifecycleExecution(
113 new ProjectExecutionEvent(session, currentProject, mojoExecutions));
114 mojoExecutor.execute(session, mojoExecutions, reactorContext.getProjectIndex());
115
116 long buildEndTime = System.currentTimeMillis();
117
118 projectExecutionListener.afterProjectExecutionSuccess(
119 new ProjectExecutionEvent(session, currentProject, mojoExecutions));
120
121 reactorContext.getResult().addBuildSummary(new BuildSuccess(currentProject, buildEndTime - buildStartTime));
122
123 eventCatapult.fire(ExecutionEvent.Type.ProjectSucceeded, session, null);
124 } catch (Throwable t) {
125 builderCommon.handleBuildError(reactorContext, rootSession, session, currentProject, t, buildStartTime);
126
127 projectExecutionListener.afterProjectExecutionFailure(
128 new ProjectExecutionEvent(session, currentProject, t));
129
130
131 if (t instanceof RuntimeException) {
132 throw (RuntimeException) t;
133 }
134 if (t instanceof Error) {
135 throw (Error) t;
136 }
137 } finally {
138 session.setCurrentProject(null);
139
140 Thread.currentThread().setContextClassLoader(reactorContext.getOriginalContextClassLoader());
141 }
142 }
143 }