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.List;
27  import java.util.stream.Collectors;
28  import java.util.stream.Stream;
29  
30  import org.apache.maven.execution.MavenSession;
31  import org.apache.maven.lifecycle.LifecycleNotFoundException;
32  import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
33  import org.apache.maven.plugin.InvalidPluginDescriptorException;
34  import org.apache.maven.plugin.MojoNotFoundException;
35  import org.apache.maven.plugin.PluginDescriptorParsingException;
36  import org.apache.maven.plugin.PluginNotFoundException;
37  import org.apache.maven.plugin.PluginResolutionException;
38  import org.apache.maven.plugin.descriptor.MojoDescriptor;
39  import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
40  import org.apache.maven.plugin.version.PluginVersionResolutionException;
41  import org.apache.maven.project.MavenProject;
42  
43  import static java.util.Objects.requireNonNull;
44  
45  /**
46   * <p>
47   * Calculates the task segments in the build
48   * </p>
49   * <strong>NOTE:</strong> This class is not part of any public api and can be changed or deleted without prior notice.
50   *
51   * @since 3.0
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      @Override
68      public List<TaskSegment> calculateTaskSegments(MavenSession session)
69              throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
70                      MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
71                      PluginVersionResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException {
72  
73          MavenProject rootProject = session.getTopLevelProject();
74  
75          List<String> tasks = requireNonNull(session.getGoals()); // session never returns null, but empty list
76  
77          if (tasks.isEmpty()
78                  && (rootProject.getDefaultGoal() != null
79                          && !rootProject.getDefaultGoal().isEmpty())) {
80              tasks = Stream.of(rootProject.getDefaultGoal().split("\\s+"))
81                      .filter(g -> !g.isEmpty())
82                      .collect(Collectors.toList());
83          }
84  
85          return calculateTaskSegments(session, tasks);
86      }
87  
88      @Override
89      public List<TaskSegment> calculateTaskSegments(MavenSession session, List<String> tasks)
90              throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
91                      MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
92                      PluginVersionResolutionException {
93          List<TaskSegment> taskSegments = new ArrayList<>(tasks.size());
94  
95          TaskSegment currentSegment = null;
96  
97          for (String task : tasks) {
98              if (isGoalSpecification(task)) {
99                  // "pluginPrefix[:version]:goal" or "groupId:artifactId[:version]:goal"
100 
101                 lifecyclePluginResolver.resolveMissingPluginVersions(session.getTopLevelProject(), session);
102 
103                 MojoDescriptor mojoDescriptor =
104                         mojoDescriptorCreator.getMojoDescriptor(task, session, session.getTopLevelProject());
105 
106                 boolean aggregating = mojoDescriptor.isAggregator() || !mojoDescriptor.isProjectRequired();
107 
108                 if (currentSegment == null || currentSegment.isAggregating() != aggregating) {
109                     currentSegment = new TaskSegment(aggregating);
110                     taskSegments.add(currentSegment);
111                 }
112 
113                 currentSegment.getTasks().add(new GoalTask(task));
114             } else {
115                 // lifecycle phase
116 
117                 if (currentSegment == null || currentSegment.isAggregating()) {
118                     currentSegment = new TaskSegment(false);
119                     taskSegments.add(currentSegment);
120                 }
121 
122                 currentSegment.getTasks().add(new LifecycleTask(task));
123             }
124         }
125 
126         return taskSegments;
127     }
128 
129     @Override
130     public boolean requiresProject(MavenSession session) {
131         List<String> goals = session.getGoals();
132         if (goals != null) {
133             for (String goal : goals) {
134                 if (!isGoalSpecification(goal)) {
135                     return true;
136                 }
137             }
138         }
139         return false;
140     }
141 
142     private boolean isGoalSpecification(String task) {
143         return task.indexOf(':') >= 0;
144     }
145 }