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 javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.HashSet;
26  import java.util.List;
27  
28  import org.apache.maven.execution.BuildSuccess;
29  import org.apache.maven.execution.ExecutionEvent;
30  import org.apache.maven.execution.MavenSession;
31  import org.apache.maven.execution.ProjectExecutionEvent;
32  import org.apache.maven.execution.ProjectExecutionListener;
33  import org.apache.maven.internal.transformation.ConsumerPomArtifactTransformer;
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      private final MojoExecutor mojoExecutor;
56      private final BuilderCommon builderCommon;
57      private final ExecutionEventCatapult eventCatapult;
58      private final ProjectExecutionListener projectExecutionListener;
59      private final ConsumerPomArtifactTransformer consumerPomArtifactTransformer;
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              ConsumerPomArtifactTransformer consumerPomArtifactTransformer,
69              SessionScope sessionScope) {
70          this.mojoExecutor = mojoExecutor;
71          this.builderCommon = builderCommon;
72          this.eventCatapult = eventCatapult;
73          this.projectExecutionListener = new CompoundProjectExecutionListener(listeners);
74          this.consumerPomArtifactTransformer = consumerPomArtifactTransformer;
75          this.sessionScope = sessionScope;
76      }
77  
78      public void buildProject(
79              MavenSession session, ReactorContext reactorContext, MavenProject currentProject, TaskSegment taskSegment) {
80          buildProject(session, session, reactorContext, currentProject, taskSegment);
81      }
82  
83      public void buildProject(
84              MavenSession session,
85              MavenSession rootSession,
86              ReactorContext reactorContext,
87              MavenProject currentProject,
88              TaskSegment taskSegment) {
89          session.setCurrentProject(currentProject);
90  
91          long buildStartTime = System.currentTimeMillis();
92  
93          try {
94  
95              if (reactorContext.getReactorBuildStatus().isHaltedOrBlacklisted(currentProject)) {
96                  eventCatapult.fire(ExecutionEvent.Type.ProjectSkipped, session, null);
97                  return;
98              }
99  
100             consumerPomArtifactTransformer.injectTransformedArtifacts(currentProject, session.getRepositorySession());
101 
102             BuilderCommon.attachToThread(currentProject);
103 
104             projectExecutionListener.beforeProjectExecution(new ProjectExecutionEvent(session, currentProject));
105 
106             eventCatapult.fire(ExecutionEvent.Type.ProjectStarted, session, null);
107 
108             MavenExecutionPlan executionPlan =
109                     builderCommon.resolveBuildPlan(session, currentProject, taskSegment, new HashSet<>());
110             List<MojoExecution> mojoExecutions = executionPlan.getMojoExecutions();
111 
112             projectExecutionListener.beforeProjectLifecycleExecution(
113                     new ProjectExecutionEvent(session, currentProject, mojoExecutions));
114             mojoExecutor.execute(session, mojoExecutions, reactorContext.getProjectIndex());
115 
116             long buildEndTime = System.currentTimeMillis();
117 
118             projectExecutionListener.afterProjectExecutionSuccess(
119                     new ProjectExecutionEvent(session, currentProject, mojoExecutions));
120 
121             reactorContext.getResult().addBuildSummary(new BuildSuccess(currentProject, buildEndTime - buildStartTime));
122 
123             eventCatapult.fire(ExecutionEvent.Type.ProjectSucceeded, session, null);
124         } catch (Throwable t) {
125             builderCommon.handleBuildError(reactorContext, rootSession, session, currentProject, t, buildStartTime);
126 
127             projectExecutionListener.afterProjectExecutionFailure(
128                     new ProjectExecutionEvent(session, currentProject, t));
129 
130             // rethrow original errors and runtime exceptions
131             if (t instanceof RuntimeException) {
132                 throw (RuntimeException) t;
133             }
134             if (t instanceof Error) {
135                 throw (Error) t;
136             }
137         } finally {
138             session.setCurrentProject(null);
139 
140             Thread.currentThread().setContextClassLoader(reactorContext.getOriginalContextClassLoader());
141         }
142     }
143 }