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 org.apache.maven.artifact.Artifact;
023import org.apache.maven.execution.BuildSuccess;
024import org.apache.maven.execution.ExecutionEvent;
025import org.apache.maven.execution.MavenSession;
026import org.apache.maven.lifecycle.MavenExecutionPlan;
027import org.apache.maven.project.MavenProject;
028import org.codehaus.plexus.component.annotations.Component;
029import org.codehaus.plexus.component.annotations.Requirement;
030
031import java.util.HashSet;
032
033/**
034 * Builds one or more lifecycles for a full module
035 *
036 * @since 3.0
037 * @author Benjamin Bentmann
038 * @author Jason van Zyl
039 * @author Kristian Rosenvold (extracted class)
040 *         <p/>
041 *         NOTE: This class is not part of any public api and can be changed or deleted without prior notice.
042 */
043@Component( role = LifecycleModuleBuilder.class )
044public class LifecycleModuleBuilder
045{
046
047    @Requirement
048    private MojoExecutor mojoExecutor;
049
050    @Requirement
051    private BuilderCommon builderCommon;
052
053    @Requirement
054    private ExecutionEventCatapult eventCatapult;
055
056    public void buildProject( MavenSession session, ReactorContext reactorContext, MavenProject currentProject,
057                              TaskSegment taskSegment )
058    {
059        buildProject( session, session, reactorContext, currentProject, taskSegment );
060    }
061
062    public void buildProject( MavenSession session, MavenSession rootSession, ReactorContext reactorContext,
063                              MavenProject currentProject, TaskSegment taskSegment )
064    {
065        session.setCurrentProject( currentProject );
066
067        long buildStartTime = System.currentTimeMillis();
068
069        try
070        {
071
072            if ( reactorContext.getReactorBuildStatus().isHaltedOrBlacklisted( currentProject ) )
073            {
074                eventCatapult.fire( ExecutionEvent.Type.ProjectSkipped, session, null );
075                return;
076            }
077
078            eventCatapult.fire( ExecutionEvent.Type.ProjectStarted, session, null );
079
080            BuilderCommon.attachToThread( currentProject );
081            MavenExecutionPlan executionPlan =
082                builderCommon.resolveBuildPlan( session, currentProject, taskSegment, new HashSet<Artifact>() );
083
084            mojoExecutor.execute( session, executionPlan.getMojoExecutions(), reactorContext.getProjectIndex() );
085
086            long buildEndTime = System.currentTimeMillis();
087
088            reactorContext.getResult().addBuildSummary(
089                new BuildSuccess( currentProject, buildEndTime - buildStartTime ) );
090
091            eventCatapult.fire( ExecutionEvent.Type.ProjectSucceeded, session, null );
092        }
093        catch ( Exception e )
094        {
095            builderCommon.handleBuildError( reactorContext, rootSession, session, currentProject, e, buildStartTime );
096        }
097        finally
098        {
099            session.setCurrentProject( null );
100
101            Thread.currentThread().setContextClassLoader( reactorContext.getOriginalContextClassLoader() );
102        }
103    }
104}