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.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
47
48
49
50
51
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());
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
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
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 }