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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.execution.BuildSuccess;
24  import org.apache.maven.execution.ExecutionEvent;
25  import org.apache.maven.execution.MavenSession;
26  import org.apache.maven.lifecycle.MavenExecutionPlan;
27  import org.apache.maven.project.MavenProject;
28  import org.codehaus.plexus.component.annotations.Component;
29  import org.codehaus.plexus.component.annotations.Requirement;
30  
31  import java.util.HashSet;
32  
33  /**
34   * Builds one or more lifecycles for a full module
35   * 
36   * @since 3.0
37   * @author Benjamin Bentmann
38   * @author Jason van Zyl
39   * @author Kristian Rosenvold (extracted class)
40   *         <p/>
41   *         NOTE: This class is not part of any public api and can be changed or deleted without prior notice.
42   */
43  @Component( role = LifecycleModuleBuilder.class )
44  public class LifecycleModuleBuilder
45  {
46  
47      @Requirement
48      private MojoExecutor mojoExecutor;
49  
50      @Requirement
51      private BuilderCommon builderCommon;
52  
53      @Requirement
54      private ExecutionEventCatapult eventCatapult;
55  
56      public void buildProject( MavenSession session, ReactorContext reactorContext, MavenProject currentProject,
57                                TaskSegment taskSegment )
58      {
59          buildProject( session, session, reactorContext, currentProject, taskSegment );
60      }
61  
62      public void buildProject( MavenSession session, MavenSession rootSession, ReactorContext reactorContext,
63                                MavenProject currentProject, TaskSegment taskSegment )
64      {
65          session.setCurrentProject( currentProject );
66  
67          long buildStartTime = System.currentTimeMillis();
68  
69          try
70          {
71  
72              if ( reactorContext.getReactorBuildStatus().isHaltedOrBlacklisted( currentProject ) )
73              {
74                  eventCatapult.fire( ExecutionEvent.Type.ProjectSkipped, session, null );
75                  return;
76              }
77  
78              eventCatapult.fire( ExecutionEvent.Type.ProjectStarted, session, null );
79  
80              BuilderCommon.attachToThread( currentProject );
81              MavenExecutionPlan executionPlan =
82                  builderCommon.resolveBuildPlan( session, currentProject, taskSegment, new HashSet<Artifact>() );
83  
84              mojoExecutor.execute( session, executionPlan.getMojoExecutions(), reactorContext.getProjectIndex() );
85  
86              long buildEndTime = System.currentTimeMillis();
87  
88              reactorContext.getResult().addBuildSummary(
89                  new BuildSuccess( currentProject, buildEndTime - buildStartTime ) );
90  
91              eventCatapult.fire( ExecutionEvent.Type.ProjectSucceeded, session, null );
92          }
93          catch ( Exception e )
94          {
95              builderCommon.handleBuildError( reactorContext, rootSession, currentProject, e, buildStartTime );
96          }
97          finally
98          {
99              session.setCurrentProject( null );
100 
101             Thread.currentThread().setContextClassLoader( reactorContext.getOriginalContextClassLoader() );
102         }
103     }
104 }