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