1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
43
44
45
46
47
48
49
50
51
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
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
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 }