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 java.util.HashSet;
22 import java.util.List;
23
24 import org.apache.maven.artifact.Artifact;
25 import org.apache.maven.execution.BuildSuccess;
26 import org.apache.maven.execution.ExecutionEvent;
27 import org.apache.maven.execution.MavenSession;
28 import org.apache.maven.execution.ProjectExecutionEvent;
29 import org.apache.maven.execution.ProjectExecutionListener;
30 import org.apache.maven.lifecycle.MavenExecutionPlan;
31 import org.apache.maven.lifecycle.internal.builder.BuilderCommon;
32 import org.apache.maven.plugin.MojoExecution;
33 import org.apache.maven.project.MavenProject;
34 import org.codehaus.plexus.component.annotations.Component;
35 import org.codehaus.plexus.component.annotations.Requirement;
36
37
38
39
40
41
42
43
44
45
46
47
48 @Component(role = LifecycleModuleBuilder.class)
49 public class LifecycleModuleBuilder {
50
51 @Requirement
52 private MojoExecutor mojoExecutor;
53
54 @Requirement
55 private BuilderCommon builderCommon;
56
57 @Requirement
58 private ExecutionEventCatapult eventCatapult;
59
60 private ProjectExecutionListener projectExecutionListener;
61
62
63 @Requirement
64 private List<ProjectExecutionListener> projectExecutionListeners;
65
66 public void setProjectExecutionListeners(final List<ProjectExecutionListener> listeners) {
67 this.projectExecutionListeners = listeners;
68 this.projectExecutionListener = new CompoundProjectExecutionListener(listeners);
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 BuilderCommon.attachToThread(currentProject);
94
95 projectExecutionListener.beforeProjectExecution(new ProjectExecutionEvent(session, currentProject));
96
97 eventCatapult.fire(ExecutionEvent.Type.ProjectStarted, session, null);
98
99 MavenExecutionPlan executionPlan =
100 builderCommon.resolveBuildPlan(session, currentProject, taskSegment, new HashSet<Artifact>());
101 List<MojoExecution> mojoExecutions = executionPlan.getMojoExecutions();
102
103 projectExecutionListener.beforeProjectLifecycleExecution(
104 new ProjectExecutionEvent(session, currentProject, mojoExecutions));
105 mojoExecutor.execute(session, mojoExecutions, reactorContext.getProjectIndex());
106
107 long buildEndTime = System.currentTimeMillis();
108
109 projectExecutionListener.afterProjectExecutionSuccess(
110 new ProjectExecutionEvent(session, currentProject, mojoExecutions));
111
112 reactorContext.getResult().addBuildSummary(new BuildSuccess(currentProject, buildEndTime - buildStartTime));
113
114 eventCatapult.fire(ExecutionEvent.Type.ProjectSucceeded, session, null);
115 } catch (Throwable t) {
116 builderCommon.handleBuildError(reactorContext, rootSession, session, currentProject, t, buildStartTime);
117
118 projectExecutionListener.afterProjectExecutionFailure(
119 new ProjectExecutionEvent(session, currentProject, t));
120
121
122 if (t instanceof RuntimeException) {
123 throw (RuntimeException) t;
124 }
125 if (t instanceof Error) {
126 throw (Error) t;
127 }
128 } finally {
129 session.setCurrentProject(null);
130
131 Thread.currentThread().setContextClassLoader(reactorContext.getOriginalContextClassLoader());
132 }
133 }
134 }