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   */
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 ConsumerPomArtifactTransformer consumerPomArtifactTransformer;
57      private final SessionScope sessionScope;
58  
59      @Inject
60      public LifecycleModuleBuilder(
61              MojoExecutor mojoExecutor,
62              BuilderCommon builderCommon,
63              ExecutionEventCatapult eventCatapult,
64              List<ProjectExecutionListener> listeners,
65              ConsumerPomArtifactTransformer consumerPomArtifactTransformer,
66              SessionScope sessionScope) {
67          this.mojoExecutor = mojoExecutor;
68          this.builderCommon = builderCommon;
69          this.eventCatapult = eventCatapult;
70          this.projectExecutionListener = new CompoundProjectExecutionListener(listeners);
71          this.consumerPomArtifactTransformer = consumerPomArtifactTransformer;
72          this.sessionScope = sessionScope;
73      }
74  
75      public void buildProject(
76              MavenSession session, ReactorContext reactorContext, MavenProject currentProject, TaskSegment taskSegment) {
77          buildProject(session, session, reactorContext, currentProject, taskSegment);
78      }
79  
80      public void buildProject(
81              MavenSession session,
82              MavenSession rootSession,
83              ReactorContext reactorContext,
84              MavenProject currentProject,
85              TaskSegment taskSegment) {
86          session.setCurrentProject(currentProject);
87  
88          long buildStartTime = System.currentTimeMillis();
89  
90          try {
91  
92              if (reactorContext.getReactorBuildStatus().isHaltedOrBlacklisted(currentProject)) {
93                  eventCatapult.fire(ExecutionEvent.Type.ProjectSkipped, session, null);
94                  return;
95              }
96  
97              consumerPomArtifactTransformer.injectTransformedArtifacts(currentProject, session.getRepositorySession());
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(
110                     new ProjectExecutionEvent(session, currentProject, mojoExecutions));
111             mojoExecutor.execute(session, mojoExecutions, reactorContext.getProjectIndex());
112 
113             long buildEndTime = System.currentTimeMillis();
114 
115             projectExecutionListener.afterProjectExecutionSuccess(
116                     new ProjectExecutionEvent(session, currentProject, mojoExecutions));
117 
118             reactorContext.getResult().addBuildSummary(new BuildSuccess(currentProject, buildEndTime - buildStartTime));
119 
120             eventCatapult.fire(ExecutionEvent.Type.ProjectSucceeded, session, null);
121         } catch (Throwable t) {
122             builderCommon.handleBuildError(reactorContext, rootSession, session, currentProject, t, buildStartTime);
123 
124             projectExecutionListener.afterProjectExecutionFailure(
125                     new ProjectExecutionEvent(session, currentProject, t));
126 
127             // rethrow original errors and runtime exceptions
128             if (t instanceof RuntimeException) {
129                 throw (RuntimeException) t;
130             }
131             if (t instanceof Error) {
132                 throw (Error) t;
133             }
134         } finally {
135             session.setCurrentProject(null);
136 
137             Thread.currentThread().setContextClassLoader(reactorContext.getOriginalContextClassLoader());
138         }
139     }
140 }