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.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
44
45
46
47
48
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
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
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 }