View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.lifecycle.internal;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.ArrayList;
26  import java.util.Arrays;
27  import java.util.List;
28  
29  import org.apache.maven.execution.MavenSession;
30  import org.apache.maven.lifecycle.LifecycleNotFoundException;
31  import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
32  import org.apache.maven.plugin.InvalidPluginDescriptorException;
33  import org.apache.maven.plugin.MojoNotFoundException;
34  import org.apache.maven.plugin.PluginDescriptorParsingException;
35  import org.apache.maven.plugin.PluginNotFoundException;
36  import org.apache.maven.plugin.PluginResolutionException;
37  import org.apache.maven.plugin.descriptor.MojoDescriptor;
38  import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
39  import org.apache.maven.plugin.version.PluginVersionResolutionException;
40  import org.apache.maven.project.MavenProject;
41  import org.codehaus.plexus.util.StringUtils;
42  
43  /**
44   * <p>
45   * Calculates the task segments in the build
46   * </p>
47   * <strong>NOTE:</strong> This class is not part of any public api and can be changed or deleted without prior notice.
48   *
49   * @since 3.0
50   * @author Benjamin Bentmann
51   * @author Jason van Zyl
52   * @author jdcasey
53   * @author Kristian Rosenvold (extracted class)
54   */
55  @Singleton
56  @Named
57  public class DefaultLifecycleTaskSegmentCalculator implements LifecycleTaskSegmentCalculator {
58      @Inject
59      private MojoDescriptorCreator mojoDescriptorCreator;
60  
61      @Inject
62      private LifecyclePluginResolver lifecyclePluginResolver;
63  
64      public DefaultLifecycleTaskSegmentCalculator() {}
65  
66      public List<TaskSegment> calculateTaskSegments(MavenSession session)
67              throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
68                      MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
69                      PluginVersionResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException {
70  
71          MavenProject rootProject = session.getTopLevelProject();
72  
73          List<String> tasks = session.getGoals();
74  
75          if ((tasks == null || tasks.isEmpty()) && !StringUtils.isEmpty(rootProject.getDefaultGoal())) {
76              tasks = Arrays.asList(StringUtils.split(rootProject.getDefaultGoal()));
77          }
78  
79          return calculateTaskSegments(session, tasks);
80      }
81  
82      public List<TaskSegment> calculateTaskSegments(MavenSession session, List<String> tasks)
83              throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
84                      MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
85                      PluginVersionResolutionException {
86          List<TaskSegment> taskSegments = new ArrayList<>(tasks.size());
87  
88          TaskSegment currentSegment = null;
89  
90          for (String task : tasks) {
91              if (isGoalSpecification(task)) {
92                  // "pluginPrefix[:version]:goal" or "groupId:artifactId[:version]:goal"
93  
94                  lifecyclePluginResolver.resolveMissingPluginVersions(session.getTopLevelProject(), session);
95  
96                  MojoDescriptor mojoDescriptor =
97                          mojoDescriptorCreator.getMojoDescriptor(task, session, session.getTopLevelProject());
98  
99                  boolean aggregating = mojoDescriptor.isAggregator() || !mojoDescriptor.isProjectRequired();
100 
101                 if (currentSegment == null || currentSegment.isAggregating() != aggregating) {
102                     currentSegment = new TaskSegment(aggregating);
103                     taskSegments.add(currentSegment);
104                 }
105 
106                 currentSegment.getTasks().add(new GoalTask(task));
107             } else {
108                 // lifecycle phase
109 
110                 if (currentSegment == null || currentSegment.isAggregating()) {
111                     currentSegment = new TaskSegment(false);
112                     taskSegments.add(currentSegment);
113                 }
114 
115                 currentSegment.getTasks().add(new LifecycleTask(task));
116             }
117         }
118 
119         return taskSegments;
120     }
121 
122     public boolean requiresProject(MavenSession session) {
123         List<String> goals = session.getGoals();
124         if (goals != null) {
125             for (String goal : goals) {
126                 if (!isGoalSpecification(goal)) {
127                     return true;
128                 }
129             }
130         }
131         return false;
132     }
133 
134     private boolean isGoalSpecification(String task) {
135         return task.indexOf(':') >= 0;
136     }
137 }