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 javax.inject.Inject;
26 import javax.inject.Named;
27 import javax.inject.Singleton;
28
29 import org.apache.maven.execution.BuildSuccess;
30 import org.apache.maven.execution.ExecutionEvent;
31 import org.apache.maven.execution.MavenSession;
32 import org.apache.maven.execution.ProjectExecutionEvent;
33 import org.apache.maven.execution.ProjectExecutionListener;
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
56 private final MojoExecutor mojoExecutor;
57 private final BuilderCommon builderCommon;
58 private final ExecutionEventCatapult eventCatapult;
59 private final ProjectExecutionListener projectExecutionListener;
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 SessionScope sessionScope )
69 {
70 this.mojoExecutor = mojoExecutor;
71 this.builderCommon = builderCommon;
72 this.eventCatapult = eventCatapult;
73 this.projectExecutionListener = new CompoundProjectExecutionListener( listeners );
74 this.sessionScope = sessionScope;
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 try
91 {
92
93 if ( reactorContext.getReactorBuildStatus().isHaltedOrBlacklisted( currentProject ) )
94 {
95 eventCatapult.fire( ExecutionEvent.Type.ProjectSkipped, session, null );
96 return;
97 }
98
99 BuilderCommon.attachToThread( currentProject );
100
101 projectExecutionListener.beforeProjectExecution( new ProjectExecutionEvent( session, currentProject ) );
102
103 eventCatapult.fire( ExecutionEvent.Type.ProjectStarted, session, null );
104
105 MavenExecutionPlan executionPlan =
106 builderCommon.resolveBuildPlan( session, currentProject, taskSegment, new HashSet<>() );
107 List<MojoExecution> mojoExecutions = executionPlan.getMojoExecutions();
108
109 projectExecutionListener.beforeProjectLifecycleExecution( new ProjectExecutionEvent( session,
110 currentProject,
111 mojoExecutions ) );
112 mojoExecutor.execute( session, mojoExecutions, reactorContext.getProjectIndex() );
113
114 long buildEndTime = System.currentTimeMillis();
115
116 projectExecutionListener.afterProjectExecutionSuccess( new ProjectExecutionEvent( session, currentProject,
117 mojoExecutions ) );
118
119 reactorContext.getResult().addBuildSummary( new BuildSuccess( currentProject,
120 buildEndTime - buildStartTime ) );
121
122 eventCatapult.fire( ExecutionEvent.Type.ProjectSucceeded, session, null );
123 }
124 catch ( Throwable t )
125 {
126 builderCommon.handleBuildError( reactorContext, rootSession, session, currentProject, t, buildStartTime );
127
128 projectExecutionListener.afterProjectExecutionFailure( new ProjectExecutionEvent( session, currentProject,
129 t ) );
130
131
132 if ( t instanceof RuntimeException )
133 {
134 throw (RuntimeException) t;
135 }
136 if ( t instanceof Error )
137 {
138 throw (Error) t;
139 }
140 }
141 finally
142 {
143 session.setCurrentProject( null );
144
145 Thread.currentThread().setContextClassLoader( reactorContext.getOriginalContextClassLoader() );
146 }
147 }
148 }