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          sessionScope.enter( reactorContext.getSessionScopeMemento() );
94          sessionScope.seed( MavenSession.class, session );
95          try
96          {
97  
98              if ( reactorContext.getReactorBuildStatus().isHaltedOrBlacklisted( currentProject ) )
99              {
100                 eventCatapult.fire( ExecutionEvent.Type.ProjectSkipped, session, null );
101                 return;
102             }
103 
104             BuilderCommon.attachToThread( currentProject );
105 
106             projectExecutionListener.beforeProjectExecution( new ProjectExecutionEvent( session, currentProject ) );
107 
108             eventCatapult.fire( ExecutionEvent.Type.ProjectStarted, session, null );
109 
110             MavenExecutionPlan executionPlan =
111                 builderCommon.resolveBuildPlan( session, currentProject, taskSegment, new HashSet<Artifact>() );
112             List<MojoExecution> mojoExecutions = executionPlan.getMojoExecutions();
113 
114             projectExecutionListener.beforeProjectLifecycleExecution( new ProjectExecutionEvent( session,
115                                                                                                  currentProject,
116                                                                                                  mojoExecutions ) );
117             mojoExecutor.execute( session, mojoExecutions, reactorContext.getProjectIndex() );
118 
119             long buildEndTime = System.currentTimeMillis();
120 
121             projectExecutionListener.afterProjectExecutionSuccess( new ProjectExecutionEvent( session, currentProject,
122                                                                                               mojoExecutions ) );
123 
124             reactorContext.getResult().addBuildSummary( new BuildSuccess( currentProject,
125                                                                           buildEndTime - buildStartTime ) );
126 
127             eventCatapult.fire( ExecutionEvent.Type.ProjectSucceeded, session, null );
128         }
129         catch ( Throwable t )
130         {
131             builderCommon.handleBuildError( reactorContext, rootSession, session, currentProject, t, buildStartTime );
132 
133             projectExecutionListener.afterProjectExecutionFailure( new ProjectExecutionEvent( session, currentProject,
134                                                                                               t ) );
135 
136             
137             if ( t instanceof RuntimeException )
138             {
139                 throw (RuntimeException) t;
140             }
141             if ( t instanceof Error )
142             {
143                 throw (Error) t;
144             }
145         }
146         finally
147         {
148             sessionScope.exit();
149 
150             session.setCurrentProject( null );
151 
152             Thread.currentThread().setContextClassLoader( reactorContext.getOriginalContextClassLoader() );
153         }
154     }
155 }