1 package org.apache.maven.lifecycle.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.HashSet;
23 import java.util.List;
24
25 import org.apache.maven.artifact.Artifact;
26 import org.apache.maven.execution.BuildSuccess;
27 import org.apache.maven.execution.ExecutionEvent;
28 import org.apache.maven.execution.MavenSession;
29 import org.apache.maven.execution.ProjectExecutionEvent;
30 import org.apache.maven.execution.ProjectExecutionListener;
31 import org.apache.maven.lifecycle.MavenExecutionPlan;
32 import org.apache.maven.lifecycle.internal.builder.BuilderCommon;
33 import org.apache.maven.plugin.MojoExecution;
34 import org.apache.maven.project.MavenProject;
35 import org.apache.maven.session.scope.internal.SessionScope;
36 import org.codehaus.plexus.component.annotations.Component;
37 import org.codehaus.plexus.component.annotations.Requirement;
38
39
40
41
42
43
44
45
46
47
48
49
50 @Component( role = LifecycleModuleBuilder.class )
51 public class LifecycleModuleBuilder
52 {
53
54 @Requirement
55 private MojoExecutor mojoExecutor;
56
57 @Requirement
58 private BuilderCommon builderCommon;
59
60 @Requirement
61 private ExecutionEventCatapult eventCatapult;
62
63 private ProjectExecutionListener projectExecutionListener;
64
65
66 @Requirement
67 private List<ProjectExecutionListener> projectExecutionListeners;
68
69 @Requirement
70 private SessionScope sessionScope;
71
72 public void setProjectExecutionListeners( final List<ProjectExecutionListener> listeners )
73 {
74 this.projectExecutionListeners = listeners;
75 this.projectExecutionListener = new CompoundProjectExecutionListener( listeners );
76 }
77
78 public void buildProject( MavenSession session, ReactorContext reactorContext, MavenProject currentProject,
79 TaskSegment taskSegment )
80 {
81 buildProject( session, session, reactorContext, currentProject, taskSegment );
82 }
83
84 public void buildProject( MavenSession session, MavenSession rootSession, ReactorContext reactorContext,
85 MavenProject currentProject, TaskSegment taskSegment )
86 {
87 session.setCurrentProject( currentProject );
88
89 long buildStartTime = System.currentTimeMillis();
90
91
92
93 final boolean scoped = session != rootSession;
94 if ( scoped )
95 {
96 sessionScope.enter();
97 sessionScope.seed( MavenSession.class, session );
98 }
99 try
100 {
101
102 if ( reactorContext.getReactorBuildStatus().isHaltedOrBlacklisted( currentProject ) )
103 {
104 eventCatapult.fire( ExecutionEvent.Type.ProjectSkipped, session, null );
105 return;
106 }
107
108 BuilderCommon.attachToThread( currentProject );
109
110 projectExecutionListener.beforeProjectExecution( new ProjectExecutionEvent( session, currentProject ) );
111
112 eventCatapult.fire( ExecutionEvent.Type.ProjectStarted, session, null );
113
114 MavenExecutionPlan executionPlan =
115 builderCommon.resolveBuildPlan( session, currentProject, taskSegment, new HashSet<Artifact>() );
116 List<MojoExecution> mojoExecutions = executionPlan.getMojoExecutions();
117
118 projectExecutionListener.beforeProjectLifecycleExecution( new ProjectExecutionEvent( session,
119 currentProject,
120 mojoExecutions ) );
121 mojoExecutor.execute( session, mojoExecutions, reactorContext.getProjectIndex() );
122
123 long buildEndTime = System.currentTimeMillis();
124
125 projectExecutionListener.afterProjectExecutionSuccess( new ProjectExecutionEvent( session, currentProject,
126 mojoExecutions ) );
127
128 reactorContext.getResult().addBuildSummary( new BuildSuccess( currentProject,
129 buildEndTime - buildStartTime ) );
130
131 eventCatapult.fire( ExecutionEvent.Type.ProjectSucceeded, session, null );
132 }
133 catch ( Throwable t )
134 {
135 builderCommon.handleBuildError( reactorContext, rootSession, session, currentProject, t, buildStartTime );
136
137 projectExecutionListener.afterProjectExecutionFailure( new ProjectExecutionEvent( session, currentProject,
138 t ) );
139
140
141 if ( t instanceof RuntimeException )
142 {
143 throw (RuntimeException) t;
144 }
145 if ( t instanceof Error )
146 {
147 throw (Error) t;
148 }
149 }
150 finally
151 {
152 if ( scoped )
153 {
154 sessionScope.exit();
155 }
156
157 session.setCurrentProject( null );
158
159 Thread.currentThread().setContextClassLoader( reactorContext.getOriginalContextClassLoader() );
160 }
161 }
162 }