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