1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.lifecycle;
20
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.LinkedHashMap;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30
31 import org.apache.maven.lifecycle.internal.ExecutionPlanItem;
32 import org.apache.maven.model.Plugin;
33 import org.apache.maven.plugin.MojoExecution;
34 import org.apache.maven.plugin.descriptor.MojoDescriptor;
35
36
37
38
39
40
41
42
43
44
45
46
47 public class MavenExecutionPlan implements Iterable<ExecutionPlanItem> {
48
49
50
51
52
53
54
55
56
57
58 private final List<ExecutionPlanItem> planItem;
59
60 private final Map<String, ExecutionPlanItem> lastMojoExecutionForAllPhases;
61
62 final List<String> phasesInExecutionPlan;
63
64 public MavenExecutionPlan(List<ExecutionPlanItem> planItem, DefaultLifecycles defaultLifecycles) {
65 this.planItem = planItem;
66
67 lastMojoExecutionForAllPhases = new LinkedHashMap<>();
68
69 LinkedHashSet<String> totalPhaseSet = new LinkedHashSet<>();
70 if (defaultLifecycles != null) {
71 for (String phase : getDistinctPhasesInOrderOfExecutionPlanAppearance(planItem)) {
72 final Lifecycle lifecycle = defaultLifecycles.get(phase);
73 if (lifecycle != null) {
74 totalPhaseSet.addAll(lifecycle.getPhases());
75 }
76 }
77 }
78 this.phasesInExecutionPlan = new ArrayList<>(totalPhaseSet);
79
80 Map<String, ExecutionPlanItem> lastInExistingPhases = new HashMap<>();
81 for (ExecutionPlanItem executionPlanItem : getExecutionPlanItems()) {
82 lastInExistingPhases.put(executionPlanItem.getLifecyclePhase(), executionPlanItem);
83 }
84
85 ExecutionPlanItem lastSeenExecutionPlanItem = null;
86
87 for (String phase : totalPhaseSet) {
88 ExecutionPlanItem forThisPhase = lastInExistingPhases.get(phase);
89 if (forThisPhase != null) {
90 lastSeenExecutionPlanItem = forThisPhase;
91 }
92
93 lastMojoExecutionForAllPhases.put(phase, lastSeenExecutionPlanItem);
94 }
95 }
96
97 @Override
98 public Iterator<ExecutionPlanItem> iterator() {
99 return getExecutionPlanItems().iterator();
100 }
101
102
103
104
105
106
107
108
109
110 public ExecutionPlanItem findLastInPhase(String requestedPhase) {
111 return lastMojoExecutionForAllPhases.get(requestedPhase);
112 }
113
114 private List<ExecutionPlanItem> getExecutionPlanItems() {
115 return planItem;
116 }
117
118 private static Iterable<String> getDistinctPhasesInOrderOfExecutionPlanAppearance(
119 List<ExecutionPlanItem> planItems) {
120 LinkedHashSet<String> result = new LinkedHashSet<>();
121 for (ExecutionPlanItem executionPlanItem : planItems) {
122 result.add(executionPlanItem.getLifecyclePhase());
123 }
124 return result;
125 }
126
127 public List<MojoExecution> getMojoExecutions() {
128 List<MojoExecution> result = new ArrayList<>();
129 for (ExecutionPlanItem executionPlanItem : planItem) {
130 result.add(executionPlanItem.getMojoExecution());
131 }
132 return result;
133 }
134
135
136
137
138
139
140 public Set<Plugin> getNonThreadSafePlugins() {
141 Set<Plugin> plugins = new HashSet<>();
142 for (ExecutionPlanItem executionPlanItem : planItem) {
143 final MojoExecution mojoExecution = executionPlanItem.getMojoExecution();
144 if (!mojoExecution.getMojoDescriptor().isThreadSafe()) {
145 plugins.add(mojoExecution.getPlugin());
146 }
147 }
148 return plugins;
149 }
150
151
152
153
154
155
156 public Set<MojoDescriptor> getNonThreadSafeMojos() {
157 Set<MojoDescriptor> mojos = new HashSet<>();
158 for (ExecutionPlanItem executionPlanItem : planItem) {
159 final MojoExecution mojoExecution = executionPlanItem.getMojoExecution();
160 if (!mojoExecution.getMojoDescriptor().isThreadSafe()) {
161 mojos.add(mojoExecution.getMojoDescriptor());
162 }
163 }
164 return mojos;
165 }
166
167
168 @Deprecated
169 public List<MojoExecution> getExecutions() {
170 return getMojoExecutions();
171 }
172
173 public int size() {
174 return planItem.size();
175 }
176 }