View Javadoc
1   package org.apache.maven.lifecycle.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Builds one or more lifecycles for a full module
40   * 
41   * @since 3.0
42   * @author Benjamin Bentmann
43   * @author Jason van Zyl
44   * @author Kristian Rosenvold (extracted class)
45   *         <p/>
46   *         NOTE: This class is not part of any public api and can be changed or deleted without prior notice.
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      // this tricks plexus-component-metadata generate required metadata
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 }