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 import javax.inject.Inject;
25 import javax.inject.Named;
26 import javax.inject.Singleton;
27 import org.apache.maven.execution.MavenSession;
28 import org.apache.maven.lifecycle.LifecycleNotFoundException;
29 import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
30 import org.apache.maven.plugin.InvalidPluginDescriptorException;
31 import org.apache.maven.plugin.MojoNotFoundException;
32 import org.apache.maven.plugin.PluginDescriptorParsingException;
33 import org.apache.maven.plugin.PluginNotFoundException;
34 import org.apache.maven.plugin.PluginResolutionException;
35 import org.apache.maven.plugin.descriptor.MojoDescriptor;
36 import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
37 import org.apache.maven.plugin.version.PluginVersionResolutionException;
38 import org.apache.maven.project.MavenProject;
39 import org.codehaus.plexus.util.StringUtils;
40
41
42
43
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 public List<TaskSegment> calculateTaskSegments(MavenSession session)
68 throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
69 MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
70 PluginVersionResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException {
71
72 MavenProject rootProject = session.getTopLevelProject();
73
74 List<String> tasks = session.getGoals();
75
76 if ((tasks == null || tasks.isEmpty()) && !StringUtils.isEmpty(rootProject.getDefaultGoal())) {
77 tasks = Arrays.asList(StringUtils.split(rootProject.getDefaultGoal()));
78 }
79
80 return calculateTaskSegments(session, tasks);
81 }
82
83 public List<TaskSegment> calculateTaskSegments(MavenSession session, List<String> tasks)
84 throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
85 MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
86 PluginVersionResolutionException {
87 List<TaskSegment> taskSegments = new ArrayList<>(tasks.size());
88
89 TaskSegment currentSegment = null;
90
91 for (String task : tasks) {
92 if (isGoalSpecification(task)) {
93
94
95 lifecyclePluginResolver.resolveMissingPluginVersions(session.getTopLevelProject(), session);
96
97 MojoDescriptor mojoDescriptor =
98 mojoDescriptorCreator.getMojoDescriptor(task, session, session.getTopLevelProject());
99
100 boolean aggregating = mojoDescriptor.isAggregator() || !mojoDescriptor.isProjectRequired();
101
102 if (currentSegment == null || currentSegment.isAggregating() != aggregating) {
103 currentSegment = new TaskSegment(aggregating);
104 taskSegments.add(currentSegment);
105 }
106
107 currentSegment.getTasks().add(new GoalTask(task));
108 } else {
109
110
111 if (currentSegment == null || currentSegment.isAggregating()) {
112 currentSegment = new TaskSegment(false);
113 taskSegments.add(currentSegment);
114 }
115
116 currentSegment.getTasks().add(new LifecycleTask(task));
117 }
118 }
119
120 return taskSegments;
121 }
122
123 public boolean requiresProject(MavenSession session) {
124 List<String> goals = session.getGoals();
125 if (goals != null) {
126 for (String goal : goals) {
127 if (!isGoalSpecification(goal)) {
128 return true;
129 }
130 }
131 }
132 return false;
133 }
134
135 private boolean isGoalSpecification(String task) {
136 return task.indexOf(':') >= 0;
137 }
138 }