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   * Builds one or more lifecycles for a full module
41   *
42   * @since 3.0
43   * @author Benjamin Bentmann
44   * @author Jason van Zyl
45   * @author Kristian Rosenvold (extracted class)
46   *         <p/>
47   *         NOTE: This class is not part of any public api and can be changed or deleted without prior notice.
48   */
49  @Component( role = LifecycleModuleBuilder.class )
50  public class LifecycleModuleBuilder
51  {
52  
53      @Requirement
54      private MojoExecutor mojoExecutor;
55  
56      @Requirement
57      private BuilderCommon builderCommon;
58  
59      @Requirement
60      private ExecutionEventCatapult eventCatapult;
61  
62      private ProjectExecutionListener projectExecutionListener;
63  
64      // this tricks plexus-component-metadata generate required metadata
65      @Requirement
66      private List<ProjectExecutionListener> projectExecutionListeners;
67  
68      @Requirement
69      private SessionScope sessionScope;
70  
71      public void setProjectExecutionListeners( final List<ProjectExecutionListener> listeners )
72      {
73          this.projectExecutionListeners = listeners;
74          this.projectExecutionListener = new CompoundProjectExecutionListener( listeners );
75      }
76  
77      public void buildProject( MavenSession session, ReactorContext reactorContext, MavenProject currentProject,
78                                TaskSegment taskSegment )
79      {
80          buildProject( session, session, reactorContext, currentProject, taskSegment );
81      }
82  
83      public void buildProject( MavenSession session, MavenSession rootSession, ReactorContext reactorContext,
84                                MavenProject currentProject, TaskSegment taskSegment )
85      {
86          session.setCurrentProject( currentProject );
87  
88          long buildStartTime = System.currentTimeMillis();
89  
90          // session may be different from rootSession seeded in DefaultMaven
91          // explicitly seed the right session here to make sure it is used by Guice
92          sessionScope.enter( reactorContext.getSessionScopeMemento() );
93          sessionScope.seed( MavenSession.class, session );
94          try
95          {
96  
97              if ( reactorContext.getReactorBuildStatus().isHaltedOrBlacklisted( currentProject ) )
98              {
99                  eventCatapult.fire( ExecutionEvent.Type.ProjectSkipped, session, null );
100                 return;
101             }
102 
103             BuilderCommon.attachToThread( currentProject );
104 
105             projectExecutionListener.beforeProjectExecution( new ProjectExecutionEvent( session, currentProject ) );
106 
107             eventCatapult.fire( ExecutionEvent.Type.ProjectStarted, session, null );
108 
109             MavenExecutionPlan executionPlan =
110                 builderCommon.resolveBuildPlan( session, currentProject, taskSegment, new HashSet<Artifact>() );
111             List<MojoExecution> mojoExecutions = executionPlan.getMojoExecutions();
112 
113             projectExecutionListener.beforeProjectLifecycleExecution( new ProjectExecutionEvent( session,
114                                                                                                  currentProject,
115                                                                                                  mojoExecutions ) );
116             mojoExecutor.execute( session, mojoExecutions, reactorContext.getProjectIndex() );
117 
118             long buildEndTime = System.currentTimeMillis();
119 
120             projectExecutionListener.afterProjectExecutionSuccess( new ProjectExecutionEvent( session, currentProject,
121                                                                                               mojoExecutions ) );
122 
123             reactorContext.getResult().addBuildSummary( new BuildSuccess( currentProject,
124                                                                           buildEndTime - buildStartTime ) );
125 
126             eventCatapult.fire( ExecutionEvent.Type.ProjectSucceeded, session, null );
127         }
128         catch ( Throwable t )
129         {
130             builderCommon.handleBuildError( reactorContext, rootSession, session, currentProject, t, buildStartTime );
131 
132             projectExecutionListener.afterProjectExecutionFailure( new ProjectExecutionEvent( session, currentProject,
133                                                                                               t ) );
134 
135             // rethrow original errors and runtime exceptions
136             if ( t instanceof RuntimeException )
137             {
138                 throw (RuntimeException) t;
139             }
140             if ( t instanceof Error )
141             {
142                 throw (Error) t;
143             }
144         }
145         finally
146         {
147             sessionScope.exit();
148 
149             session.setCurrentProject( null );
150 
151             Thread.currentThread().setContextClassLoader( reactorContext.getOriginalContextClassLoader() );
152         }
153     }
154 }