001package org.apache.maven.lifecycle;
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.execution.MavenSession;
023import org.apache.maven.lifecycle.internal.LifecycleExecutionPlanCalculator;
024import org.apache.maven.lifecycle.internal.LifecycleStarter;
025import org.apache.maven.lifecycle.internal.LifecycleTaskSegmentCalculator;
026import org.apache.maven.lifecycle.internal.MojoDescriptorCreator;
027import org.apache.maven.lifecycle.internal.MojoExecutor;
028import org.apache.maven.lifecycle.internal.ProjectIndex;
029import org.apache.maven.lifecycle.internal.TaskSegment;
030import org.apache.maven.model.Plugin;
031import org.apache.maven.plugin.InvalidPluginDescriptorException;
032import org.apache.maven.plugin.MojoExecution;
033import org.apache.maven.plugin.MojoNotFoundException;
034import org.apache.maven.plugin.PluginDescriptorParsingException;
035import org.apache.maven.plugin.PluginManagerException;
036import org.apache.maven.plugin.PluginNotFoundException;
037import org.apache.maven.plugin.PluginResolutionException;
038import org.apache.maven.plugin.descriptor.MojoDescriptor;
039import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
040import org.apache.maven.plugin.version.PluginVersionResolutionException;
041import org.apache.maven.project.MavenProject;
042import org.codehaus.plexus.component.annotations.Component;
043import org.codehaus.plexus.component.annotations.Requirement;
044
045import java.util.Arrays;
046import java.util.List;
047import java.util.Map;
048import java.util.Set;
049
050/**
051 * A facade that provides lifecycle services to components outside maven core.
052 *
053 * Note that this component is not normally used from within core itself.
054 *
055 * @author Jason van Zyl
056 * @author Benjamin Bentmann
057 * @author Kristian Rosenvold
058 */
059@Component( role = LifecycleExecutor.class )
060public class DefaultLifecycleExecutor
061    implements LifecycleExecutor
062{
063
064    @Requirement
065    private LifeCyclePluginAnalyzer lifeCyclePluginAnalyzer;
066
067    @Requirement
068    private DefaultLifecycles defaultLifeCycles;
069
070    @Requirement
071    private LifecycleTaskSegmentCalculator lifecycleTaskSegmentCalculator;
072
073    @Requirement
074    private LifecycleExecutionPlanCalculator lifecycleExecutionPlanCalculator;
075
076    @Requirement
077    private MojoExecutor mojoExecutor;
078
079    @Requirement
080    private LifecycleStarter lifecycleStarter;
081
082
083    public void execute( MavenSession session )
084    {
085        lifecycleStarter.execute( session );
086    }
087
088    @Requirement
089    private MojoDescriptorCreator mojoDescriptorCreator;
090
091    // These methods deal with construction intact Plugin object that look like they come from a standard
092    // <plugin/> block in a Maven POM. We have to do some wiggling to pull the sources of information
093    // together and this really shows the problem of constructing a sensible default configuration but
094    // it's all encapsulated here so it appears normalized to the POM builder.
095
096    // We are going to take the project packaging and find all plugin in the default lifecycle and create
097    // fully populated Plugin objects, including executions with goals and default configuration taken
098    // from the plugin.xml inside a plugin.
099    //
100    // TODO: This whole method could probably removed by injecting lifeCyclePluginAnalyzer straight into client site.
101    // TODO: But for some reason the whole plexus appcontext refuses to start when I try this.
102
103    public Set<Plugin> getPluginsBoundByDefaultToAllLifecycles( String packaging )
104    {
105        return lifeCyclePluginAnalyzer.getPluginsBoundByDefaultToAllLifecycles( packaging );
106    }
107
108    // USED BY MAVEN HELP PLUGIN
109
110    @Deprecated
111    public Map<String, Lifecycle> getPhaseToLifecycleMap()
112    {
113        return defaultLifeCycles.getPhaseToLifecycleMap();
114    }
115
116    // NOTE: Backward-compat with maven-help-plugin:2.1
117
118    @SuppressWarnings( { "UnusedDeclaration" } )
119    MojoDescriptor getMojoDescriptor( String task, MavenSession session, MavenProject project, String invokedVia,
120                                      boolean canUsePrefix, boolean isOptionalMojo )
121        throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
122        MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
123        PluginVersionResolutionException
124    {
125        return mojoDescriptorCreator.getMojoDescriptor( task, session, project );
126    }
127
128    // Used by m2eclipse
129
130    @SuppressWarnings( { "UnusedDeclaration" } )
131    public MavenExecutionPlan calculateExecutionPlan( MavenSession session, boolean setup, String... tasks )
132        throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
133        MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
134        PluginManagerException, LifecyclePhaseNotFoundException, LifecycleNotFoundException,
135        PluginVersionResolutionException
136    {
137        List<TaskSegment> taskSegments =
138            lifecycleTaskSegmentCalculator.calculateTaskSegments( session, Arrays.asList( tasks ) );
139
140        TaskSegment mergedSegment = new TaskSegment( false );
141
142        for ( TaskSegment taskSegment : taskSegments )
143        {
144            mergedSegment.getTasks().addAll( taskSegment.getTasks() );
145        }
146
147        return lifecycleExecutionPlanCalculator.calculateExecutionPlan( session, session.getCurrentProject(),
148                                                                        mergedSegment.getTasks(), setup );
149    }
150
151    public MavenExecutionPlan calculateExecutionPlan( MavenSession session, String... tasks )
152        throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
153        MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
154        PluginManagerException, LifecyclePhaseNotFoundException, LifecycleNotFoundException,
155        PluginVersionResolutionException
156    {
157        return calculateExecutionPlan( session, true, tasks );
158    }
159
160    // Site 3.x
161    public void calculateForkedExecutions( MojoExecution mojoExecution, MavenSession session )
162        throws MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
163        PluginDescriptorParsingException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
164        LifecyclePhaseNotFoundException, LifecycleNotFoundException, PluginVersionResolutionException
165    {
166        lifecycleExecutionPlanCalculator.calculateForkedExecutions( mojoExecution, session );
167    }
168
169    // Site 3.x
170    public List<MavenProject> executeForkedExecutions( MojoExecution mojoExecution, MavenSession session )
171        throws LifecycleExecutionException
172    {
173        return mojoExecutor.executeForkedExecutions( mojoExecution, session,
174                                                     new ProjectIndex( session.getProjects() ) );
175    }
176
177}