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