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