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 java.util.HashSet;
22  import java.util.List;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.execution.BuildSuccess;
26  import org.apache.maven.execution.ExecutionEvent;
27  import org.apache.maven.execution.MavenSession;
28  import org.apache.maven.execution.ProjectExecutionEvent;
29  import org.apache.maven.execution.ProjectExecutionListener;
30  import org.apache.maven.lifecycle.MavenExecutionPlan;
31  import org.apache.maven.lifecycle.internal.builder.BuilderCommon;
32  import org.apache.maven.plugin.MojoExecution;
33  import org.apache.maven.project.MavenProject;
34  import org.codehaus.plexus.component.annotations.Component;
35  import org.codehaus.plexus.component.annotations.Requirement;
36  
37  /**
38   * <p>
39   * Builds one or more lifecycles for a full module
40   * </p>
41   * <strong>NOTE:</strong> This class is not part of any public api and can be changed or deleted without prior notice.
42   *
43   * @since 3.0
44   * @author Benjamin Bentmann
45   * @author Jason van Zyl
46   * @author Kristian Rosenvold (extracted class)
47   */
48  @Component(role = LifecycleModuleBuilder.class)
49  public class LifecycleModuleBuilder {
50  
51      @Requirement
52      private MojoExecutor mojoExecutor;
53  
54      @Requirement
55      private BuilderCommon builderCommon;
56  
57      @Requirement
58      private ExecutionEventCatapult eventCatapult;
59  
60      private ProjectExecutionListener projectExecutionListener;
61  
62      // this tricks plexus-component-metadata generate required metadata
63      @Requirement
64      private List<ProjectExecutionListener> projectExecutionListeners;
65  
66      public void setProjectExecutionListeners(final List<ProjectExecutionListener> listeners) {
67          this.projectExecutionListeners = listeners;
68          this.projectExecutionListener = new CompoundProjectExecutionListener(listeners);
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              BuilderCommon.attachToThread(currentProject);
94  
95              projectExecutionListener.beforeProjectExecution(new ProjectExecutionEvent(session, currentProject));
96  
97              eventCatapult.fire(ExecutionEvent.Type.ProjectStarted, session, null);
98  
99              MavenExecutionPlan executionPlan =
100                     builderCommon.resolveBuildPlan(session, currentProject, taskSegment, new HashSet<Artifact>());
101             List<MojoExecution> mojoExecutions = executionPlan.getMojoExecutions();
102 
103             projectExecutionListener.beforeProjectLifecycleExecution(
104                     new ProjectExecutionEvent(session, currentProject, mojoExecutions));
105             mojoExecutor.execute(session, mojoExecutions, reactorContext.getProjectIndex());
106 
107             long buildEndTime = System.currentTimeMillis();
108 
109             projectExecutionListener.afterProjectExecutionSuccess(
110                     new ProjectExecutionEvent(session, currentProject, mojoExecutions));
111 
112             reactorContext.getResult().addBuildSummary(new BuildSuccess(currentProject, buildEndTime - buildStartTime));
113 
114             eventCatapult.fire(ExecutionEvent.Type.ProjectSucceeded, session, null);
115         } catch (Throwable t) {
116             builderCommon.handleBuildError(reactorContext, rootSession, session, currentProject, t, buildStartTime);
117 
118             projectExecutionListener.afterProjectExecutionFailure(
119                     new ProjectExecutionEvent(session, currentProject, t));
120 
121             // rethrow original errors and runtime exceptions
122             if (t instanceof RuntimeException) {
123                 throw (RuntimeException) t;
124             }
125             if (t instanceof Error) {
126                 throw (Error) t;
127             }
128         } finally {
129             session.setCurrentProject(null);
130 
131             Thread.currentThread().setContextClassLoader(reactorContext.getOriginalContextClassLoader());
132         }
133     }
134 }