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.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   * <p>
41   * Builds one or more lifecycles for a full module
42   * </p>
43   * <strong>NOTE:</strong> This class is not part of any public api and can be changed or deleted without prior notice.
44   * 
45   * @since 3.0
46   * @author Benjamin Bentmann
47   * @author Jason van Zyl
48   * @author Kristian Rosenvold (extracted class)
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      // this tricks plexus-component-metadata generate required metadata
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          // session may be different from rootSession seeded in DefaultMaven
92          // explicitly seed the right session here to make sure it is used by Guice
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             // rethrow original errors and runtime exceptions
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 }