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