001package org.apache.maven.lifecycle.internal; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.HashSet; 023import java.util.List; 024 025import org.apache.maven.artifact.Artifact; 026import org.apache.maven.execution.BuildSuccess; 027import org.apache.maven.execution.ExecutionEvent; 028import org.apache.maven.execution.MavenSession; 029import org.apache.maven.execution.ProjectExecutionEvent; 030import org.apache.maven.execution.ProjectExecutionListener; 031import org.apache.maven.lifecycle.MavenExecutionPlan; 032import org.apache.maven.lifecycle.internal.builder.BuilderCommon; 033import org.apache.maven.plugin.MojoExecution; 034import org.apache.maven.project.MavenProject; 035import org.codehaus.plexus.component.annotations.Component; 036import org.codehaus.plexus.component.annotations.Requirement; 037 038/** 039 * Builds one or more lifecycles for a full module 040 * 041 * @since 3.0 042 * @author Benjamin Bentmann 043 * @author Jason van Zyl 044 * @author Kristian Rosenvold (extracted class) 045 * <p/> 046 * NOTE: This class is not part of any public api and can be changed or deleted without prior notice. 047 */ 048@Component( role = LifecycleModuleBuilder.class ) 049public class LifecycleModuleBuilder 050{ 051 052 @Requirement 053 private MojoExecutor mojoExecutor; 054 055 @Requirement 056 private BuilderCommon builderCommon; 057 058 @Requirement 059 private ExecutionEventCatapult eventCatapult; 060 061 private ProjectExecutionListener projectExecutionListener; 062 063 // this tricks plexus-component-metadata generate required metadata 064 @Requirement 065 private List<ProjectExecutionListener> projectExecutionListeners; 066 067 public void setProjectExecutionListeners( final List<ProjectExecutionListener> listeners ) 068 { 069 this.projectExecutionListeners = listeners; 070 this.projectExecutionListener = new CompoundProjectExecutionListener( listeners ); 071 } 072 073 public void buildProject( MavenSession session, ReactorContext reactorContext, MavenProject currentProject, 074 TaskSegment taskSegment ) 075 { 076 buildProject( session, session, reactorContext, currentProject, taskSegment ); 077 } 078 079 public void buildProject( MavenSession session, MavenSession rootSession, ReactorContext reactorContext, 080 MavenProject currentProject, TaskSegment taskSegment ) 081 { 082 session.setCurrentProject( currentProject ); 083 084 long buildStartTime = System.currentTimeMillis(); 085 086 try 087 { 088 089 if ( reactorContext.getReactorBuildStatus().isHaltedOrBlacklisted( currentProject ) ) 090 { 091 eventCatapult.fire( ExecutionEvent.Type.ProjectSkipped, session, null ); 092 return; 093 } 094 095 BuilderCommon.attachToThread( currentProject ); 096 097 projectExecutionListener.beforeProjectExecution( new ProjectExecutionEvent( session, currentProject ) ); 098 099 eventCatapult.fire( ExecutionEvent.Type.ProjectStarted, session, null ); 100 101 MavenExecutionPlan executionPlan = 102 builderCommon.resolveBuildPlan( session, currentProject, taskSegment, new HashSet<Artifact>() ); 103 List<MojoExecution> mojoExecutions = executionPlan.getMojoExecutions(); 104 105 projectExecutionListener.beforeProjectLifecycleExecution( new ProjectExecutionEvent( session, 106 currentProject, 107 mojoExecutions ) ); 108 mojoExecutor.execute( session, mojoExecutions, reactorContext.getProjectIndex() ); 109 110 long buildEndTime = System.currentTimeMillis(); 111 112 projectExecutionListener.afterProjectExecutionSuccess( new ProjectExecutionEvent( session, currentProject, 113 mojoExecutions ) ); 114 115 reactorContext.getResult().addBuildSummary( new BuildSuccess( currentProject, buildEndTime - buildStartTime ) ); 116 117 eventCatapult.fire( ExecutionEvent.Type.ProjectSucceeded, session, null ); 118 } 119 catch ( Exception e ) 120 { 121 builderCommon.handleBuildError( reactorContext, rootSession, session, currentProject, e, buildStartTime ); 122 123 projectExecutionListener.afterProjectExecutionFailure( new ProjectExecutionEvent( session, currentProject, 124 e ) ); 125 } 126 finally 127 { 128 session.setCurrentProject( null ); 129 130 Thread.currentThread().setContextClassLoader( reactorContext.getOriginalContextClassLoader() ); 131 } 132 } 133}