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