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