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.StringTokenizer;
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  
42  /**
43   * <p>
44   * Calculates the task segments in the build
45   * </p>
46   * <strong>NOTE:</strong> This class is not part of any public api and can be changed or deleted without prior notice.
47   *
48   * @since 3.0
49   */
50  @Named
51  @Singleton
52  public class DefaultLifecycleTaskSegmentCalculator implements LifecycleTaskSegmentCalculator {
53      private final MojoDescriptorCreator mojoDescriptorCreator;
54  
55      private final LifecyclePluginResolver lifecyclePluginResolver;
56  
57      @Inject
58      public DefaultLifecycleTaskSegmentCalculator(
59              MojoDescriptorCreator mojoDescriptorCreator, LifecyclePluginResolver lifecyclePluginResolver) {
60          this.mojoDescriptorCreator = mojoDescriptorCreator;
61          this.lifecyclePluginResolver = lifecyclePluginResolver;
62      }
63  
64      public List<TaskSegment> calculateTaskSegments(MavenSession session)
65              throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
66                      MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
67                      PluginVersionResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException {
68  
69          MavenProject rootProject = session.getTopLevelProject();
70  
71          List<String> tasks = session.getGoals();
72  
73          if ((tasks == null || tasks.isEmpty())
74                  && (rootProject.getDefaultGoal() != null
75                          && !rootProject.getDefaultGoal().isEmpty())) {
76              StringTokenizer tokenizer = new StringTokenizer(rootProject.getDefaultGoal());
77              tasks = new ArrayList<>();
78              while (tokenizer.hasMoreTokens()) {
79                  tasks.add(tokenizer.nextToken());
80              }
81          }
82  
83          return calculateTaskSegments(session, tasks);
84      }
85  
86      public List<TaskSegment> calculateTaskSegments(MavenSession session, List<String> tasks)
87              throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
88                      MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
89                      PluginVersionResolutionException {
90          List<TaskSegment> taskSegments = new ArrayList<>(tasks.size());
91  
92          TaskSegment currentSegment = null;
93  
94          for (String task : tasks) {
95              if (isGoalSpecification(task)) {
96                  // "pluginPrefix[:version]:goal" or "groupId:artifactId[:version]:goal"
97  
98                  lifecyclePluginResolver.resolveMissingPluginVersions(session.getTopLevelProject(), session);
99  
100                 MojoDescriptor mojoDescriptor =
101                         mojoDescriptorCreator.getMojoDescriptor(task, session, session.getTopLevelProject());
102 
103                 boolean aggregating = mojoDescriptor.isAggregator() || !mojoDescriptor.isProjectRequired();
104 
105                 if (currentSegment == null || currentSegment.isAggregating() != aggregating) {
106                     currentSegment = new TaskSegment(aggregating);
107                     taskSegments.add(currentSegment);
108                 }
109 
110                 currentSegment.getTasks().add(new GoalTask(task));
111             } else {
112                 // lifecycle phase
113 
114                 if (currentSegment == null || currentSegment.isAggregating()) {
115                     currentSegment = new TaskSegment(false);
116                     taskSegments.add(currentSegment);
117                 }
118 
119                 currentSegment.getTasks().add(new LifecycleTask(task));
120             }
121         }
122 
123         return taskSegments;
124     }
125 
126     public boolean requiresProject(MavenSession session) {
127         List<String> goals = session.getGoals();
128         if (goals != null) {
129             for (String goal : goals) {
130                 if (!isGoalSpecification(goal)) {
131                     return true;
132                 }
133             }
134         }
135         return false;
136     }
137 
138     private boolean isGoalSpecification(String task) {
139         return task.indexOf(':') >= 0;
140     }
141 }