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 java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  
25  import org.apache.maven.execution.MavenSession;
26  import org.apache.maven.lifecycle.LifecycleNotFoundException;
27  import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
28  import org.apache.maven.plugin.InvalidPluginDescriptorException;
29  import org.apache.maven.plugin.MojoNotFoundException;
30  import org.apache.maven.plugin.PluginDescriptorParsingException;
31  import org.apache.maven.plugin.PluginNotFoundException;
32  import org.apache.maven.plugin.PluginResolutionException;
33  import org.apache.maven.plugin.descriptor.MojoDescriptor;
34  import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
35  import org.apache.maven.plugin.version.PluginVersionResolutionException;
36  import org.apache.maven.project.MavenProject;
37  import org.codehaus.plexus.component.annotations.Component;
38  import org.codehaus.plexus.component.annotations.Requirement;
39  import org.codehaus.plexus.util.StringUtils;
40  
41  /**
42   * <p>
43   * Calculates the task segments in the build
44   * </p>
45   * <strong>NOTE:</strong> This class is not part of any public api and can be changed or deleted without prior notice.
46   *
47   * @since 3.0
48   * @author Benjamin Bentmann
49   * @author Jason van Zyl
50   * @author jdcasey
51   * @author Kristian Rosenvold (extracted class)
52   */
53  @Component(role = LifecycleTaskSegmentCalculator.class)
54  public class DefaultLifecycleTaskSegmentCalculator implements LifecycleTaskSegmentCalculator {
55      @Requirement
56      private MojoDescriptorCreator mojoDescriptorCreator;
57  
58      @Requirement
59      private LifecyclePluginResolver lifecyclePluginResolver;
60  
61      public DefaultLifecycleTaskSegmentCalculator() {}
62  
63      public List<TaskSegment> calculateTaskSegments(MavenSession session)
64              throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
65                      MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
66                      PluginVersionResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException {
67  
68          MavenProject rootProject = session.getTopLevelProject();
69  
70          List<String> tasks = session.getGoals();
71  
72          if ((tasks == null || tasks.isEmpty()) && !StringUtils.isEmpty(rootProject.getDefaultGoal())) {
73              tasks = Arrays.asList(StringUtils.split(rootProject.getDefaultGoal()));
74          }
75  
76          return calculateTaskSegments(session, tasks);
77      }
78  
79      public List<TaskSegment> calculateTaskSegments(MavenSession session, List<String> tasks)
80              throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
81                      MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
82                      PluginVersionResolutionException {
83          List<TaskSegment> taskSegments = new ArrayList<>(tasks.size());
84  
85          TaskSegment currentSegment = null;
86  
87          for (String task : tasks) {
88              if (isGoalSpecification(task)) {
89                  // "pluginPrefix[:version]:goal" or "groupId:artifactId[:version]:goal"
90  
91                  lifecyclePluginResolver.resolveMissingPluginVersions(session.getTopLevelProject(), session);
92  
93                  MojoDescriptor mojoDescriptor =
94                          mojoDescriptorCreator.getMojoDescriptor(task, session, session.getTopLevelProject());
95  
96                  boolean aggregating = mojoDescriptor.isAggregator() || !mojoDescriptor.isProjectRequired();
97  
98                  if (currentSegment == null || currentSegment.isAggregating() != aggregating) {
99                      currentSegment = new TaskSegment(aggregating);
100                     taskSegments.add(currentSegment);
101                 }
102 
103                 currentSegment.getTasks().add(new GoalTask(task));
104             } else {
105                 // lifecycle phase
106 
107                 if (currentSegment == null || currentSegment.isAggregating()) {
108                     currentSegment = new TaskSegment(false);
109                     taskSegments.add(currentSegment);
110                 }
111 
112                 currentSegment.getTasks().add(new LifecycleTask(task));
113             }
114         }
115 
116         return taskSegments;
117     }
118 
119     public boolean requiresProject(MavenSession session) {
120         List<String> goals = session.getGoals();
121         if (goals != null) {
122             for (String goal : goals) {
123                 if (!isGoalSpecification(goal)) {
124                     return true;
125                 }
126             }
127         }
128         return false;
129     }
130 
131     private boolean isGoalSpecification(String task) {
132         return task.indexOf(':') >= 0;
133     }
134 }