001package org.apache.maven.lifecycle.internal.builder;
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.InternalErrorException;
023import org.apache.maven.artifact.Artifact;
024import org.apache.maven.execution.BuildFailure;
025import org.apache.maven.execution.ExecutionEvent;
026import org.apache.maven.execution.MavenExecutionRequest;
027import org.apache.maven.execution.MavenSession;
028import org.apache.maven.lifecycle.LifecycleExecutionException;
029import org.apache.maven.lifecycle.LifecycleNotFoundException;
030import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
031import org.apache.maven.lifecycle.MavenExecutionPlan;
032import org.apache.maven.lifecycle.internal.ExecutionEventCatapult;
033import org.apache.maven.lifecycle.internal.LifecycleDebugLogger;
034import org.apache.maven.lifecycle.internal.LifecycleExecutionPlanCalculator;
035import org.apache.maven.lifecycle.internal.ReactorContext;
036import org.apache.maven.lifecycle.internal.TaskSegment;
037import org.apache.maven.model.Plugin;
038import org.apache.maven.plugin.InvalidPluginDescriptorException;
039import org.apache.maven.plugin.MojoNotFoundException;
040import org.apache.maven.plugin.PluginDescriptorParsingException;
041import org.apache.maven.plugin.PluginNotFoundException;
042import org.apache.maven.plugin.PluginResolutionException;
043import org.apache.maven.plugin.descriptor.MojoDescriptor;
044import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
045import org.apache.maven.plugin.version.PluginVersionResolutionException;
046import org.apache.maven.project.MavenProject;
047import org.codehaus.plexus.classworlds.realm.ClassRealm;
048import org.codehaus.plexus.component.annotations.Component;
049import org.codehaus.plexus.component.annotations.Requirement;
050import org.codehaus.plexus.logging.Logger;
051
052import java.util.Set;
053
054/**
055 * Common code that is shared by the LifecycleModuleBuilder and the LifeCycleWeaveBuilder
056 *
057 * @since 3.0
058 * @author Kristian Rosenvold
059 *         Builds one or more lifecycles for a full module
060 *         NOTE: This class is not part of any public api and can be changed or deleted without prior notice.
061 */
062@Component( role = BuilderCommon.class )
063public class BuilderCommon
064{
065    @Requirement
066    private LifecycleDebugLogger lifecycleDebugLogger;
067
068    @Requirement
069    private LifecycleExecutionPlanCalculator lifeCycleExecutionPlanCalculator;
070
071    @Requirement
072    private ExecutionEventCatapult eventCatapult;
073
074    @Requirement
075    private Logger logger;
076
077
078    public BuilderCommon()
079    {
080    }
081
082    public BuilderCommon( LifecycleDebugLogger lifecycleDebugLogger,
083                          LifecycleExecutionPlanCalculator lifeCycleExecutionPlanCalculator, Logger logger )
084    {
085        this.lifecycleDebugLogger = lifecycleDebugLogger;
086        this.lifeCycleExecutionPlanCalculator = lifeCycleExecutionPlanCalculator;
087        this.logger = logger;
088    }
089
090    public MavenExecutionPlan resolveBuildPlan( MavenSession session, MavenProject project, TaskSegment taskSegment,
091                                                Set<Artifact> projectArtifacts )
092        throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
093        PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
094        NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException,
095        LifecycleExecutionException
096    {
097        MavenExecutionPlan executionPlan =
098            lifeCycleExecutionPlanCalculator.calculateExecutionPlan( session, project, taskSegment.getTasks() );
099
100        lifecycleDebugLogger.debugProjectPlan( project, executionPlan );
101
102        if ( session.getRequest().getDegreeOfConcurrency() > 1 )
103        {
104            final Set<Plugin> unsafePlugins = executionPlan.getNonThreadSafePlugins();
105            if ( !unsafePlugins.isEmpty()  && logger.isDebugEnabled() )
106            {
107                logger.warn( "*****************************************************************" );
108                logger.warn( "* Your build is requesting parallel execution, but project      *" );
109                logger.warn( "* contains the following plugin(s) that have goals not marked   *" );
110                logger.warn( "* as @threadSafe to support parallel building.                  *" );
111                logger.warn( "* While this /may/ work fine, please look for plugin updates    *" );
112                logger.warn( "* and/or request plugins be made thread-safe.                   *" );
113                logger.warn( "* If reporting an issue, report it against the plugin in        *" );
114                logger.warn( "* question, not against maven-core                              *" );
115                logger.warn( "*****************************************************************" );
116                if ( logger.isDebugEnabled() )
117                {
118                    final Set<MojoDescriptor> unsafeGoals = executionPlan.getNonThreadSafeMojos();
119                    logger.warn( "The following goals are not marked @threadSafe in " + project.getName() + ":" );
120                    for ( MojoDescriptor unsafeGoal : unsafeGoals )
121                    {
122                        logger.warn( unsafeGoal.getId() );
123                    }
124                }
125                else
126                {
127                    logger.warn( "The following plugins are not marked @threadSafe in " + project.getName() + ":" );
128                    for ( Plugin unsafePlugin : unsafePlugins )
129                    {
130                        logger.warn( unsafePlugin.getId() );
131                    }
132                    logger.warn( "Enable debug to see more precisely which goals are not marked @threadSafe." );
133                }
134                logger.warn( "*****************************************************************" );
135            }
136        }
137
138        return executionPlan;
139    }
140
141    public void handleBuildError( final ReactorContext buildContext, final MavenSession rootSession,
142                                  final MavenSession currentSession, final MavenProject mavenProject, Exception e,
143                                  final long buildStartTime )
144    {
145        if ( e instanceof RuntimeException )
146        {
147            e = new InternalErrorException( "Internal error: " + e, e );
148        }
149
150        buildContext.getResult().addException( e );
151
152        long buildEndTime = System.currentTimeMillis();
153
154        buildContext.getResult().addBuildSummary( new BuildFailure( mavenProject, buildEndTime - buildStartTime, e ) );
155
156        eventCatapult.fire( ExecutionEvent.Type.ProjectFailed, currentSession, null, e );
157
158        if ( MavenExecutionRequest.REACTOR_FAIL_NEVER.equals( rootSession.getReactorFailureBehavior() ) )
159        {
160            // continue the build
161        }
162        else if ( MavenExecutionRequest.REACTOR_FAIL_AT_END.equals( rootSession.getReactorFailureBehavior() ) )
163        {
164            // continue the build but ban all projects that depend on the failed one
165            buildContext.getReactorBuildStatus().blackList( mavenProject );
166        }
167        else if ( MavenExecutionRequest.REACTOR_FAIL_FAST.equals( rootSession.getReactorFailureBehavior() ) )
168        {
169            buildContext.getReactorBuildStatus().halt();
170        }
171        else
172        {
173            throw new IllegalArgumentException(
174                "invalid reactor failure behavior " + rootSession.getReactorFailureBehavior() );
175        }
176    }
177
178    public static void attachToThread( MavenProject currentProject )
179    {
180        ClassRealm projectRealm = currentProject.getClassRealm();
181        if ( projectRealm != null )
182        {
183            Thread.currentThread().setContextClassLoader( projectRealm );
184        }
185    }
186
187    // Todo: I'm really wondering where this method belongs; smells like it should be on MavenProject, but for some reason
188    // it isn't ? This localization is kind-of a code smell.
189
190    public static String getKey( MavenProject project )
191    {
192        return project.getGroupId() + ':' + project.getArtifactId() + ':' + project.getVersion();
193    }
194
195}