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.artifact.Artifact;
29  import org.apache.maven.execution.BuildSuccess;
30  import org.apache.maven.execution.ExecutionEvent;
31  import org.apache.maven.execution.MavenSession;
32  import org.apache.maven.execution.ProjectExecutionEvent;
33  import org.apache.maven.execution.ProjectExecutionListener;
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   * @author Benjamin Bentmann
47   * @author Jason van Zyl
48   * @author Kristian Rosenvold (extracted class)
49   */
50  @Singleton
51  @Named
52  public class LifecycleModuleBuilder {
53  
54      @Inject
55      private MojoExecutor mojoExecutor;
56  
57      @Inject
58      private BuilderCommon builderCommon;
59  
60      @Inject
61      private ExecutionEventCatapult eventCatapult;
62  
63      private ProjectExecutionListener projectExecutionListener;
64  
65      // TODO: this is hack here; old code was tricking plexus to call setter method; to preserve binary compat, not much
66      // can be done, but marking this
67      @Inject
68      public void setProjectExecutionListeners(final List<ProjectExecutionListener> listeners) {
69          this.projectExecutionListener = new CompoundProjectExecutionListener(listeners);
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<Artifact>());
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 }