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  import javax.inject.Inject;
25  import javax.inject.Named;
26  import javax.inject.Singleton;
27  import org.apache.maven.execution.MavenSession;
28  import org.apache.maven.lifecycle.LifecycleNotFoundException;
29  import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
30  import org.apache.maven.plugin.InvalidPluginDescriptorException;
31  import org.apache.maven.plugin.MojoNotFoundException;
32  import org.apache.maven.plugin.PluginDescriptorParsingException;
33  import org.apache.maven.plugin.PluginNotFoundException;
34  import org.apache.maven.plugin.PluginResolutionException;
35  import org.apache.maven.plugin.descriptor.MojoDescriptor;
36  import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
37  import org.apache.maven.plugin.version.PluginVersionResolutionException;
38  import org.apache.maven.project.MavenProject;
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  @Named
54  @Singleton
55  public class DefaultLifecycleTaskSegmentCalculator implements LifecycleTaskSegmentCalculator {
56      private final MojoDescriptorCreator mojoDescriptorCreator;
57  
58      private final LifecyclePluginResolver lifecyclePluginResolver;
59  
60      @Inject
61      public DefaultLifecycleTaskSegmentCalculator(
62              MojoDescriptorCreator mojoDescriptorCreator, LifecyclePluginResolver lifecyclePluginResolver) {
63          this.mojoDescriptorCreator = mojoDescriptorCreator;
64          this.lifecyclePluginResolver = lifecyclePluginResolver;
65      }
66  
67      public List<TaskSegment> calculateTaskSegments(MavenSession session)
68              throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
69                      MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
70                      PluginVersionResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException {
71  
72          MavenProject rootProject = session.getTopLevelProject();
73  
74          List<String> tasks = session.getGoals();
75  
76          if ((tasks == null || tasks.isEmpty()) && !StringUtils.isEmpty(rootProject.getDefaultGoal())) {
77              tasks = Arrays.asList(StringUtils.split(rootProject.getDefaultGoal()));
78          }
79  
80          return calculateTaskSegments(session, tasks);
81      }
82  
83      public List<TaskSegment> calculateTaskSegments(MavenSession session, List<String> tasks)
84              throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
85                      MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
86                      PluginVersionResolutionException {
87          List<TaskSegment> taskSegments = new ArrayList<>(tasks.size());
88  
89          TaskSegment currentSegment = null;
90  
91          for (String task : tasks) {
92              if (isGoalSpecification(task)) {
93                  // "pluginPrefix[:version]:goal" or "groupId:artifactId[:version]:goal"
94  
95                  lifecyclePluginResolver.resolveMissingPluginVersions(session.getTopLevelProject(), session);
96  
97                  MojoDescriptor mojoDescriptor =
98                          mojoDescriptorCreator.getMojoDescriptor(task, session, session.getTopLevelProject());
99  
100                 boolean aggregating = mojoDescriptor.isAggregator() || !mojoDescriptor.isProjectRequired();
101 
102                 if (currentSegment == null || currentSegment.isAggregating() != aggregating) {
103                     currentSegment = new TaskSegment(aggregating);
104                     taskSegments.add(currentSegment);
105                 }
106 
107                 currentSegment.getTasks().add(new GoalTask(task));
108             } else {
109                 // lifecycle phase
110 
111                 if (currentSegment == null || currentSegment.isAggregating()) {
112                     currentSegment = new TaskSegment(false);
113                     taskSegments.add(currentSegment);
114                 }
115 
116                 currentSegment.getTasks().add(new LifecycleTask(task));
117             }
118         }
119 
120         return taskSegments;
121     }
122 
123     public boolean requiresProject(MavenSession session) {
124         List<String> goals = session.getGoals();
125         if (goals != null) {
126             for (String goal : goals) {
127                 if (!isGoalSpecification(goal)) {
128                     return true;
129                 }
130             }
131         }
132         return false;
133     }
134 
135     private boolean isGoalSpecification(String task) {
136         return task.indexOf(':') >= 0;
137     }
138 }