1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.project;
20
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.TreeMap;
27
28 import org.apache.maven.model.Dependency;
29 import org.apache.maven.model.Plugin;
30 import org.apache.maven.model.PluginContainer;
31 import org.apache.maven.model.PluginExecution;
32 import org.apache.maven.model.Repository;
33 import org.codehaus.plexus.util.xml.Xpp3Dom;
34
35
36 @Deprecated
37 public final class ModelUtils {
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public static void mergePluginLists(
53 PluginContainer childContainer, PluginContainer parentContainer, boolean handleAsInheritance) {
54 if ((childContainer == null) || (parentContainer == null)) {
55
56 return;
57 }
58
59 List<Plugin> parentPlugins = parentContainer.getPlugins();
60
61 if ((parentPlugins != null) && !parentPlugins.isEmpty()) {
62 parentPlugins = new ArrayList<>(parentPlugins);
63
64
65
66 if (handleAsInheritance) {
67 for (Iterator<Plugin> it = parentPlugins.iterator(); it.hasNext(); ) {
68 Plugin plugin = it.next();
69
70 String inherited = plugin.getInherited();
71
72 if ((inherited != null) && !Boolean.parseBoolean(inherited)) {
73 it.remove();
74 }
75 }
76 }
77
78 List<Plugin> assembledPlugins = new ArrayList<>();
79
80 Map<String, Plugin> childPlugins = childContainer.getPluginsAsMap();
81
82 for (Plugin parentPlugin : parentPlugins) {
83 String parentInherited = parentPlugin.getInherited();
84
85
86
87
88
89
90 if (!handleAsInheritance || (parentInherited == null) || Boolean.parseBoolean(parentInherited)) {
91 Plugin childPlugin = childPlugins.get(parentPlugin.getKey());
92
93 if ((childPlugin != null) && !assembledPlugins.contains(childPlugin)) {
94 Plugin assembledPlugin = childPlugin;
95
96 mergePluginDefinitions(childPlugin, parentPlugin, handleAsInheritance);
97
98
99 assembledPlugins.add(assembledPlugin);
100 }
101
102
103
104
105 if (handleAsInheritance && (parentInherited == null)) {
106 parentPlugin.unsetInheritanceApplied();
107 }
108 }
109
110
111
112 List<Plugin> results =
113 ModelUtils.orderAfterMerge(assembledPlugins, parentPlugins, childContainer.getPlugins());
114
115 childContainer.setPlugins(results);
116
117 childContainer.flushPluginMap();
118 }
119 }
120 }
121
122 public static List<Plugin> orderAfterMerge(
123 List<Plugin> merged, List<Plugin> highPrioritySource, List<Plugin> lowPrioritySource) {
124 List<Plugin> results = new ArrayList<>();
125
126 if (!merged.isEmpty()) {
127 results.addAll(merged);
128 }
129
130 List<Plugin> missingFromResults = new ArrayList<>();
131
132 List<List<Plugin>> sources = new ArrayList<>();
133
134 sources.add(highPrioritySource);
135 sources.add(lowPrioritySource);
136
137 for (List<Plugin> source : sources) {
138 for (Plugin item : source) {
139 if (results.contains(item)) {
140 if (!missingFromResults.isEmpty()) {
141 int idx = results.indexOf(item);
142
143 if (idx < 0) {
144 idx = 0;
145 }
146
147 results.addAll(idx, missingFromResults);
148
149 missingFromResults.clear();
150 }
151 } else {
152 missingFromResults.add(item);
153 }
154 }
155
156 if (!missingFromResults.isEmpty()) {
157 results.addAll(missingFromResults);
158
159 missingFromResults.clear();
160 }
161 }
162
163 return results;
164 }
165
166 public static void mergePluginDefinitions(Plugin child, Plugin parent, boolean handleAsInheritance) {
167 if ((child == null) || (parent == null)) {
168
169 return;
170 }
171
172 if (parent.isExtensions()) {
173 child.setExtensions(true);
174 }
175
176 if ((child.getVersion() == null) && (parent.getVersion() != null)) {
177 child.setVersion(parent.getVersion());
178 }
179
180 Xpp3Dom childConfiguration = (Xpp3Dom) child.getConfiguration();
181 Xpp3Dom parentConfiguration = (Xpp3Dom) parent.getConfiguration();
182
183 childConfiguration = Xpp3Dom.mergeXpp3Dom(childConfiguration, parentConfiguration);
184
185 child.setConfiguration(childConfiguration);
186
187 child.setDependencies(mergeDependencyList(child.getDependencies(), parent.getDependencies()));
188
189
190 String parentInherited = parent.getInherited();
191
192 boolean parentIsInherited = (parentInherited == null) || Boolean.parseBoolean(parentInherited);
193
194 List<PluginExecution> parentExecutions = parent.getExecutions();
195
196 if ((parentExecutions != null) && !parentExecutions.isEmpty()) {
197 List<PluginExecution> mergedExecutions = new ArrayList<>();
198
199 Map<String, PluginExecution> assembledExecutions = new TreeMap<>();
200
201 Map<String, PluginExecution> childExecutions = child.getExecutionsAsMap();
202
203 for (PluginExecution parentExecution : parentExecutions) {
204 String inherited = parentExecution.getInherited();
205
206 boolean parentExecInherited =
207 parentIsInherited && ((inherited == null) || Boolean.parseBoolean(inherited));
208
209 if (!handleAsInheritance || parentExecInherited) {
210 PluginExecution assembled = parentExecution;
211
212 PluginExecution childExecution = childExecutions.get(parentExecution.getId());
213
214 if (childExecution != null) {
215 mergePluginExecutionDefinitions(childExecution, parentExecution);
216
217 assembled = childExecution;
218 } else if (handleAsInheritance && (parentInherited == null)) {
219 parentExecution.unsetInheritanceApplied();
220 }
221
222 assembledExecutions.put(assembled.getId(), assembled);
223 mergedExecutions.add(assembled);
224 }
225 }
226
227 for (PluginExecution childExecution : child.getExecutions()) {
228 if (!assembledExecutions.containsKey(childExecution.getId())) {
229 mergedExecutions.add(childExecution);
230 }
231 }
232
233 child.setExecutions(mergedExecutions);
234
235 child.flushExecutionMap();
236 }
237 }
238
239 private static void mergePluginExecutionDefinitions(PluginExecution child, PluginExecution parent) {
240 if (child.getPhase() == null) {
241 child.setPhase(parent.getPhase());
242 }
243
244 List<String> parentGoals = parent.getGoals();
245 List<String> childGoals = child.getGoals();
246
247 List<String> goals = new ArrayList<>();
248
249 if ((childGoals != null) && !childGoals.isEmpty()) {
250 goals.addAll(childGoals);
251 }
252
253 if (parentGoals != null) {
254 for (String goal : parentGoals) {
255 if (!goals.contains(goal)) {
256 goals.add(goal);
257 }
258 }
259 }
260
261 child.setGoals(goals);
262
263 Xpp3Dom childConfiguration = (Xpp3Dom) child.getConfiguration();
264 Xpp3Dom parentConfiguration = (Xpp3Dom) parent.getConfiguration();
265
266 childConfiguration = Xpp3Dom.mergeXpp3Dom(childConfiguration, parentConfiguration);
267
268 child.setConfiguration(childConfiguration);
269 }
270
271 public static List<Repository> mergeRepositoryLists(List<Repository> dominant, List<Repository> recessive) {
272
273 List<Repository> repositories = new ArrayList<>(dominant);
274
275 for (Repository repository : recessive) {
276 if (!repositories.contains(repository)) {
277 repositories.add(repository);
278 }
279 }
280
281 return repositories;
282 }
283
284 public static void mergeFilterLists(List<String> childFilters, List<String> parentFilters) {
285 for (String f : parentFilters) {
286 if (!childFilters.contains(f)) {
287 childFilters.add(f);
288 }
289 }
290 }
291
292 private static List<Dependency> mergeDependencyList(List<Dependency> child, List<Dependency> parent) {
293 Map<String, Dependency> depsMap = new LinkedHashMap<>();
294
295 if (parent != null) {
296 for (Dependency dependency : parent) {
297 depsMap.put(dependency.getManagementKey(), dependency);
298 }
299 }
300
301 if (child != null) {
302 for (Dependency dependency : child) {
303 depsMap.put(dependency.getManagementKey(), dependency);
304 }
305 }
306
307 return new ArrayList<>(depsMap.values());
308 }
309 }