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 javax.inject.Inject;
26  import javax.inject.Named;
27  import javax.inject.Singleton;
28  
29  import org.apache.maven.execution.BuildSuccess;
30  import org.apache.maven.execution.ExecutionEvent;
31  import org.apache.maven.execution.MavenSession;
32  import org.apache.maven.execution.ProjectExecutionEvent;
33  import org.apache.maven.execution.ProjectExecutionListener;
34  import org.apache.maven.lifecycle.MavenExecutionPlan;
35  import org.apache.maven.lifecycle.internal.builder.BuilderCommon;
36  import org.apache.maven.plugin.MojoExecution;
37  import org.apache.maven.project.MavenProject;
38  import org.apache.maven.session.scope.internal.SessionScope;
39  
40  /**
41   * <p>
42   * Builds one or more lifecycles for a full module
43   * </p>
44   * <strong>NOTE:</strong> This class is not part of any public api and can be changed or deleted without prior notice.
45   *
46   * @since 3.0
47   * @author Benjamin Bentmann
48   * @author Jason van Zyl
49   * @author Kristian Rosenvold (extracted class)
50   */
51  @Named
52  @Singleton
53  public class LifecycleModuleBuilder
54  {
55  
56      private final MojoExecutor mojoExecutor;
57      private final BuilderCommon builderCommon;
58      private final ExecutionEventCatapult eventCatapult;
59      private final ProjectExecutionListener projectExecutionListener;
60      private final SessionScope sessionScope;
61  
62      @Inject
63      public LifecycleModuleBuilder(
64              MojoExecutor mojoExecutor,
65              BuilderCommon builderCommon,
66              ExecutionEventCatapult eventCatapult,
67              List<ProjectExecutionListener> listeners,
68              SessionScope sessionScope )
69      {
70          this.mojoExecutor = mojoExecutor;
71          this.builderCommon = builderCommon;
72          this.eventCatapult = eventCatapult;
73          this.projectExecutionListener = new CompoundProjectExecutionListener( listeners );
74          this.sessionScope = sessionScope;
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          try
91          {
92  
93              if ( reactorContext.getReactorBuildStatus().isHaltedOrBlacklisted( currentProject ) )
94              {
95                  eventCatapult.fire( ExecutionEvent.Type.ProjectSkipped, session, null );
96                  return;
97              }
98  
99              BuilderCommon.attachToThread( currentProject );
100 
101             projectExecutionListener.beforeProjectExecution( new ProjectExecutionEvent( session, currentProject ) );
102 
103             eventCatapult.fire( ExecutionEvent.Type.ProjectStarted, session, null );
104 
105             MavenExecutionPlan executionPlan =
106                 builderCommon.resolveBuildPlan( session, currentProject, taskSegment, new HashSet<>() );
107             List<MojoExecution> mojoExecutions = executionPlan.getMojoExecutions();
108 
109             projectExecutionListener.beforeProjectLifecycleExecution( new ProjectExecutionEvent( session,
110                                                                                                  currentProject,
111                                                                                                  mojoExecutions ) );
112             mojoExecutor.execute( session, mojoExecutions, reactorContext.getProjectIndex() );
113 
114             long buildEndTime = System.currentTimeMillis();
115 
116             projectExecutionListener.afterProjectExecutionSuccess( new ProjectExecutionEvent( session, currentProject,
117                                                                                               mojoExecutions ) );
118 
119             reactorContext.getResult().addBuildSummary( new BuildSuccess( currentProject,
120                                                                           buildEndTime - buildStartTime ) );
121 
122             eventCatapult.fire( ExecutionEvent.Type.ProjectSucceeded, session, null );
123         }
124         catch ( Throwable t )
125         {
126             builderCommon.handleBuildError( reactorContext, rootSession, session, currentProject, t, buildStartTime );
127 
128             projectExecutionListener.afterProjectExecutionFailure( new ProjectExecutionEvent( session, currentProject,
129                                                                                               t ) );
130 
131             // rethrow original errors and runtime exceptions
132             if ( t instanceof RuntimeException )
133             {
134                 throw (RuntimeException) t;
135             }
136             if ( t instanceof Error )
137             {
138                 throw (Error) t;
139             }
140         }
141         finally
142         {
143             session.setCurrentProject( null );
144 
145             Thread.currentThread().setContextClassLoader( reactorContext.getOriginalContextClassLoader() );
146         }
147     }
148 }