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 @Component( role = LifecycleModuleBuilder.class )
50 public class LifecycleModuleBuilder
51 {
52
53 @Requirement
54 private MojoExecutor mojoExecutor;
55
56 @Requirement
57 private BuilderCommon builderCommon;
58
59 @Requirement
60 private ExecutionEventCatapult eventCatapult;
61
62 private ProjectExecutionListener projectExecutionListener;
63
64
65 @Requirement
66 private List<ProjectExecutionListener> projectExecutionListeners;
67
68 @Requirement
69 private SessionScope sessionScope;
70
71 public void setProjectExecutionListeners( final List<ProjectExecutionListener> listeners )
72 {
73 this.projectExecutionListeners = listeners;
74 this.projectExecutionListener = new CompoundProjectExecutionListener( listeners );
75 }
76
77 public void buildProject( MavenSession session, ReactorContext reactorContext, MavenProject currentProject,
78 TaskSegment taskSegment )
79 {
80 buildProject( session, session, reactorContext, currentProject, taskSegment );
81 }
82
83 public void buildProject( MavenSession session, MavenSession rootSession, ReactorContext reactorContext,
84 MavenProject currentProject, TaskSegment taskSegment )
85 {
86 session.setCurrentProject( currentProject );
87
88 long buildStartTime = System.currentTimeMillis();
89
90
91
92 sessionScope.enter( reactorContext.getSessionScopeMemento() );
93 sessionScope.seed( MavenSession.class, session );
94 try
95 {
96
97 if ( reactorContext.getReactorBuildStatus().isHaltedOrBlacklisted( currentProject ) )
98 {
99 eventCatapult.fire( ExecutionEvent.Type.ProjectSkipped, session, null );
100 return;
101 }
102
103 BuilderCommon.attachToThread( currentProject );
104
105 projectExecutionListener.beforeProjectExecution( new ProjectExecutionEvent( session, currentProject ) );
106
107 eventCatapult.fire( ExecutionEvent.Type.ProjectStarted, session, null );
108
109 MavenExecutionPlan executionPlan =
110 builderCommon.resolveBuildPlan( session, currentProject, taskSegment, new HashSet<Artifact>() );
111 List<MojoExecution> mojoExecutions = executionPlan.getMojoExecutions();
112
113 projectExecutionListener.beforeProjectLifecycleExecution( new ProjectExecutionEvent( session,
114 currentProject,
115 mojoExecutions ) );
116 mojoExecutor.execute( session, mojoExecutions, reactorContext.getProjectIndex() );
117
118 long buildEndTime = System.currentTimeMillis();
119
120 projectExecutionListener.afterProjectExecutionSuccess( new ProjectExecutionEvent( session, currentProject,
121 mojoExecutions ) );
122
123 reactorContext.getResult().addBuildSummary( new BuildSuccess( currentProject,
124 buildEndTime - buildStartTime ) );
125
126 eventCatapult.fire( ExecutionEvent.Type.ProjectSucceeded, session, null );
127 }
128 catch ( Throwable t )
129 {
130 builderCommon.handleBuildError( reactorContext, rootSession, session, currentProject, t, buildStartTime );
131
132 projectExecutionListener.afterProjectExecutionFailure( new ProjectExecutionEvent( session, currentProject,
133 t ) );
134
135
136 if ( t instanceof RuntimeException )
137 {
138 throw (RuntimeException) t;
139 }
140 if ( t instanceof Error )
141 {
142 throw (Error) t;
143 }
144 }
145 finally
146 {
147 sessionScope.exit();
148
149 session.setCurrentProject( null );
150
151 Thread.currentThread().setContextClassLoader( reactorContext.getOriginalContextClassLoader() );
152 }
153 }
154 }