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 @Named
56 @Singleton
57 public class DefaultLifecycleTaskSegmentCalculator implements LifecycleTaskSegmentCalculator {
58 private final MojoDescriptorCreator mojoDescriptorCreator;
59
60 private final LifecyclePluginResolver lifecyclePluginResolver;
61
62 @Inject
63 public DefaultLifecycleTaskSegmentCalculator(
64 MojoDescriptorCreator mojoDescriptorCreator, LifecyclePluginResolver lifecyclePluginResolver) {
65 this.mojoDescriptorCreator = mojoDescriptorCreator;
66 this.lifecyclePluginResolver = lifecyclePluginResolver;
67 }
68
69 public List<TaskSegment> calculateTaskSegments(MavenSession session)
70 throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
71 MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
72 PluginVersionResolutionException, LifecyclePhaseNotFoundException, LifecycleNotFoundException {
73
74 MavenProject rootProject = session.getTopLevelProject();
75
76 List<String> tasks = session.getGoals();
77
78 if ((tasks == null || tasks.isEmpty()) && !StringUtils.isEmpty(rootProject.getDefaultGoal())) {
79 tasks = Arrays.asList(StringUtils.split(rootProject.getDefaultGoal()));
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 }