View Javadoc
1   package org.apache.maven.lifecycle.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.execution.MavenSession;
23  import org.apache.maven.lifecycle.LifecycleNotFoundException;
24  import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
25  import org.apache.maven.plugin.InvalidPluginDescriptorException;
26  import org.apache.maven.plugin.MojoNotFoundException;
27  import org.apache.maven.plugin.PluginDescriptorParsingException;
28  import org.apache.maven.plugin.PluginNotFoundException;
29  import org.apache.maven.plugin.PluginResolutionException;
30  import org.apache.maven.plugin.descriptor.MojoDescriptor;
31  import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
32  import org.apache.maven.plugin.version.PluginVersionResolutionException;
33  import org.apache.maven.project.MavenProject;
34  import org.codehaus.plexus.component.annotations.Component;
35  import org.codehaus.plexus.component.annotations.Requirement;
36  import org.codehaus.plexus.util.StringUtils;
37  
38  import java.util.ArrayList;
39  import java.util.Arrays;
40  import java.util.List;
41  
42  /**
43   * Calculates the task segments in the build
44   *
45   * @since 3.0
46   * @author Benjamin Bentmann
47   * @author Jason van Zyl
48   * @author jdcasey
49   * @author Kristian Rosenvold (extracted class)
50   *         <p/>
51   *         NOTE: This class is not part of any public api and can be changed or deleted without prior notice.
52   */
53  
54  @Component( role = LifecycleTaskSegmentCalculator.class )
55  public class DefaultLifecycleTaskSegmentCalculator
56      implements LifecycleTaskSegmentCalculator
57  {
58      @Requirement
59      private MojoDescriptorCreator mojoDescriptorCreator;
60  
61      @Requirement
62      private LifecyclePluginResolver lifecyclePluginResolver;
63  
64      public DefaultLifecycleTaskSegmentCalculator()
65      {
66      }
67  
68      public List<TaskSegment> calculateTaskSegments( MavenSession session )
69          throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
70          MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
71          PluginVersionResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException
72      {
73  
74          MavenProject rootProject = session.getTopLevelProject();
75  
76          List<String> tasks = session.getGoals();
77  
78          if ( ( tasks == null || tasks.isEmpty() ) && !StringUtils.isEmpty( rootProject.getDefaultGoal() ) )
79          {
80              tasks = Arrays.asList( StringUtils.split( rootProject.getDefaultGoal() ) );
81          }
82  
83          return calculateTaskSegments( session, tasks );
84      }
85  
86      public List<TaskSegment> calculateTaskSegments( MavenSession session, List<String> tasks )
87          throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
88          MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
89          PluginVersionResolutionException
90      {
91          List<TaskSegment> taskSegments = new ArrayList<>( tasks.size() );
92  
93          TaskSegment currentSegment = null;
94  
95          for ( String task : tasks )
96          {
97              if ( isGoalSpecification( task ) )
98              {
99                  // "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 }