001 package 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
022 import org.apache.maven.execution.MavenSession;
023 import org.apache.maven.lifecycle.LifecycleNotFoundException;
024 import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
025 import org.apache.maven.plugin.InvalidPluginDescriptorException;
026 import org.apache.maven.plugin.MojoNotFoundException;
027 import org.apache.maven.plugin.PluginDescriptorParsingException;
028 import org.apache.maven.plugin.PluginNotFoundException;
029 import org.apache.maven.plugin.PluginResolutionException;
030 import org.apache.maven.plugin.descriptor.MojoDescriptor;
031 import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
032 import org.apache.maven.plugin.version.PluginVersionResolutionException;
033 import org.apache.maven.project.MavenProject;
034 import org.codehaus.plexus.component.annotations.Component;
035 import org.codehaus.plexus.component.annotations.Requirement;
036 import org.codehaus.plexus.util.StringUtils;
037
038 import java.util.ArrayList;
039 import java.util.Arrays;
040 import 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 )
055 public 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() )
079 {
080 if ( !StringUtils.isEmpty( rootProject.getDefaultGoal() ) )
081 {
082 tasks = Arrays.asList( StringUtils.split( rootProject.getDefaultGoal() ) );
083 }
084 }
085
086 return calculateTaskSegments( session, tasks );
087 }
088
089 public List<TaskSegment> calculateTaskSegments( MavenSession session, List<String> tasks )
090 throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
091 MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
092 PluginVersionResolutionException
093 {
094 List<TaskSegment> taskSegments = new ArrayList<TaskSegment>( tasks.size() );
095
096 TaskSegment currentSegment = null;
097
098 for ( String task : tasks )
099 {
100 if ( isGoalSpecification( task ) )
101 {
102 // "pluginPrefix:goal" or "groupId:artifactId[:version]:goal"
103
104 lifecyclePluginResolver.resolveMissingPluginVersions( session.getTopLevelProject(), session );
105
106 MojoDescriptor mojoDescriptor =
107 mojoDescriptorCreator.getMojoDescriptor( task, session, session.getTopLevelProject() );
108
109 boolean aggregating = mojoDescriptor.isAggregator() || !mojoDescriptor.isProjectRequired();
110
111 if ( currentSegment == null || currentSegment.isAggregating() != aggregating )
112 {
113 currentSegment = new TaskSegment( aggregating );
114 taskSegments.add( currentSegment );
115 }
116
117 currentSegment.getTasks().add( new GoalTask( task ) );
118 }
119 else
120 {
121 // lifecycle phase
122
123 if ( currentSegment == null || currentSegment.isAggregating() )
124 {
125 currentSegment = new TaskSegment( false );
126 taskSegments.add( currentSegment );
127 }
128
129 currentSegment.getTasks().add( new LifecycleTask( task ) );
130 }
131 }
132
133 return taskSegments;
134 }
135
136 public boolean requiresProject( MavenSession session )
137 {
138 List<String> goals = session.getGoals();
139 if ( goals != null )
140 {
141 for ( String goal : goals )
142 {
143 if ( !isGoalSpecification( goal ) )
144 {
145 return true;
146 }
147 }
148 }
149 return false;
150 }
151
152
153 private boolean isGoalSpecification( String task )
154 {
155 return task.indexOf( ':' ) >= 0;
156 }
157
158 }