View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.lifecycle.internal;
20  
21  import java.util.HashSet;
22  import java.util.List;
23  import javax.inject.Inject;
24  import javax.inject.Named;
25  import javax.inject.Singleton;
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  
37  /**
38   * <p>
39   * Builds one or more lifecycles for a full module
40   * </p>
41   * <strong>NOTE:</strong> This class is not part of any public api and can be changed or deleted without prior notice.
42   *
43   * @since 3.0
44   * @author Benjamin Bentmann
45   * @author Jason van Zyl
46   * @author Kristian Rosenvold (extracted class)
47   */
48  @Named
49  @Singleton
50  public class LifecycleModuleBuilder {
51  
52      private final MojoExecutor mojoExecutor;
53      private final BuilderCommon builderCommon;
54      private final ExecutionEventCatapult eventCatapult;
55      private final ProjectExecutionListener projectExecutionListener;
56      private final SessionScope sessionScope;
57  
58      @Inject
59      public LifecycleModuleBuilder(
60              MojoExecutor mojoExecutor,
61              BuilderCommon builderCommon,
62              ExecutionEventCatapult eventCatapult,
63              List<ProjectExecutionListener> listeners,
64              SessionScope sessionScope) {
65          this.mojoExecutor = mojoExecutor;
66          this.builderCommon = builderCommon;
67          this.eventCatapult = eventCatapult;
68          this.projectExecutionListener = new CompoundProjectExecutionListener(listeners);
69          this.sessionScope = sessionScope;
70      }
71  
72      public void buildProject(
73              MavenSession session, ReactorContext reactorContext, MavenProject currentProject, TaskSegment taskSegment) {
74          buildProject(session, session, reactorContext, currentProject, taskSegment);
75      }
76  
77      public void buildProject(
78              MavenSession session,
79              MavenSession rootSession,
80              ReactorContext reactorContext,
81              MavenProject currentProject,
82              TaskSegment taskSegment) {
83          session.setCurrentProject(currentProject);
84  
85          long buildStartTime = System.currentTimeMillis();
86  
87          try {
88  
89              if (reactorContext.getReactorBuildStatus().isHaltedOrBlacklisted(currentProject)) {
90                  eventCatapult.fire(ExecutionEvent.Type.ProjectSkipped, session, null);
91                  return;
92              }
93  
94              BuilderCommon.attachToThread(currentProject);
95  
96              projectExecutionListener.beforeProjectExecution(new ProjectExecutionEvent(session, currentProject));
97  
98              eventCatapult.fire(ExecutionEvent.Type.ProjectStarted, session, null);
99  
100             MavenExecutionPlan executionPlan =
101                     builderCommon.resolveBuildPlan(session, currentProject, taskSegment, new HashSet<>());
102             List<MojoExecution> mojoExecutions = executionPlan.getMojoExecutions();
103 
104             projectExecutionListener.beforeProjectLifecycleExecution(
105                     new ProjectExecutionEvent(session, currentProject, mojoExecutions));
106             mojoExecutor.execute(session, mojoExecutions, reactorContext.getProjectIndex());
107 
108             long buildEndTime = System.currentTimeMillis();
109 
110             projectExecutionListener.afterProjectExecutionSuccess(
111                     new ProjectExecutionEvent(session, currentProject, mojoExecutions));
112 
113             reactorContext.getResult().addBuildSummary(new BuildSuccess(currentProject, buildEndTime - buildStartTime));
114 
115             eventCatapult.fire(ExecutionEvent.Type.ProjectSucceeded, session, null);
116         } catch (Throwable t) {
117             builderCommon.handleBuildError(reactorContext, rootSession, session, currentProject, t, buildStartTime);
118 
119             projectExecutionListener.afterProjectExecutionFailure(
120                     new ProjectExecutionEvent(session, currentProject, t));
121 
122             // rethrow original errors and runtime exceptions
123             if (t instanceof RuntimeException) {
124                 throw (RuntimeException) t;
125             }
126             if (t instanceof Error) {
127                 throw (Error) t;
128             }
129         } finally {
130             session.setCurrentProject(null);
131 
132             Thread.currentThread().setContextClassLoader(reactorContext.getOriginalContextClassLoader());
133         }
134     }
135 }