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.execution.MavenSession;
023import org.apache.maven.lifecycle.LifecycleNotFoundException;
024import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
025import org.apache.maven.plugin.InvalidPluginDescriptorException;
026import org.apache.maven.plugin.MojoNotFoundException;
027import org.apache.maven.plugin.PluginDescriptorParsingException;
028import org.apache.maven.plugin.PluginNotFoundException;
029import org.apache.maven.plugin.PluginResolutionException;
030import org.apache.maven.plugin.descriptor.MojoDescriptor;
031import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
032import org.apache.maven.plugin.version.PluginVersionResolutionException;
033import org.apache.maven.project.MavenProject;
034import org.codehaus.plexus.component.annotations.Component;
035import org.codehaus.plexus.component.annotations.Requirement;
036import org.codehaus.plexus.util.StringUtils;
037
038import java.util.ArrayList;
039import java.util.Arrays;
040import java.util.List;
041
042/**
043 * Calculates the task segments in the build
044 *
045 * @since 3.0
046 * @author Benjamin Bentmann
047 * @author Jason van Zyl
048 * @author jdcasey
049 * @author Kristian Rosenvold (extracted class)
050 *         <p/>
051 *         NOTE: This class is not part of any public api and can be changed or deleted without prior notice.
052 */
053
054@Component( role = LifecycleTaskSegmentCalculator.class )
055public class DefaultLifecycleTaskSegmentCalculator
056    implements LifecycleTaskSegmentCalculator
057{
058    @Requirement
059    private MojoDescriptorCreator mojoDescriptorCreator;
060
061    @Requirement
062    private LifecyclePluginResolver lifecyclePluginResolver;
063
064    public DefaultLifecycleTaskSegmentCalculator()
065    {
066    }
067
068    public List<TaskSegment> calculateTaskSegments( MavenSession session )
069        throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
070        MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
071        PluginVersionResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException
072    {
073
074        MavenProject rootProject = session.getTopLevelProject();
075
076        List<String> tasks = session.getGoals();
077
078        if ( ( tasks == null || tasks.isEmpty() ) && !StringUtils.isEmpty( rootProject.getDefaultGoal() ) )
079        {
080            tasks = Arrays.asList( StringUtils.split( rootProject.getDefaultGoal() ) );
081        }
082
083        return calculateTaskSegments( session, tasks );
084    }
085
086    public List<TaskSegment> calculateTaskSegments( MavenSession session, List<String> tasks )
087        throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
088        MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
089        PluginVersionResolutionException
090    {
091        List<TaskSegment> taskSegments = new ArrayList<TaskSegment>( tasks.size() );
092
093        TaskSegment currentSegment = null;
094
095        for ( String task : tasks )
096        {
097            if ( isGoalSpecification( task ) )
098            {
099                // "pluginPrefix:goal" or "groupId:artifactId[:version]:goal"
100
101                lifecyclePluginResolver.resolveMissingPluginVersions( session.getTopLevelProject(), session );
102
103                MojoDescriptor mojoDescriptor =
104                    mojoDescriptorCreator.getMojoDescriptor( task, session, session.getTopLevelProject() );
105
106                boolean aggregating = mojoDescriptor.isAggregator() || !mojoDescriptor.isProjectRequired();
107
108                if ( currentSegment == null || currentSegment.isAggregating() != aggregating )
109                {
110                    currentSegment = new TaskSegment( aggregating );
111                    taskSegments.add( currentSegment );
112                }
113
114                currentSegment.getTasks().add( new GoalTask( task ) );
115            }
116            else
117            {
118                // lifecycle phase
119
120                if ( currentSegment == null || currentSegment.isAggregating() )
121                {
122                    currentSegment = new TaskSegment( false );
123                    taskSegments.add( currentSegment );
124                }
125
126                currentSegment.getTasks().add( new LifecycleTask( task ) );
127            }
128        }
129
130        return taskSegments;
131    }
132
133    public boolean requiresProject( MavenSession session )
134    {
135        List<String> goals = session.getGoals();
136        if ( goals != null )
137        {
138            for ( String goal : goals )
139            {
140                if ( !isGoalSpecification( goal ) )
141                {
142                    return true;
143                }
144            }
145        }
146        return false;
147    }
148
149
150    private boolean isGoalSpecification( String task )
151    {
152        return task.indexOf( ':' ) >= 0;
153    }
154
155}