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