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