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