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() )
79          {
80              if ( !StringUtils.isEmpty( rootProject.getDefaultGoal() ) )
81              {
82                  tasks = Arrays.asList( StringUtils.split( rootProject.getDefaultGoal() ) );
83              }
84          }
85  
86          return calculateTaskSegments( session, tasks );
87      }
88  
89      public List<TaskSegment> calculateTaskSegments( MavenSession session, List<String> tasks )
90          throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
91          MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
92          PluginVersionResolutionException
93      {
94          List<TaskSegment> taskSegments = new ArrayList<TaskSegment>( tasks.size() );
95  
96          TaskSegment currentSegment = null;
97  
98          for ( String task : tasks )
99          {
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 }