1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.management;
20
21 import javax.inject.Named;
22 import javax.inject.Singleton;
23
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.apache.maven.model.Build;
31 import org.apache.maven.model.Model;
32 import org.apache.maven.model.Plugin;
33 import org.apache.maven.model.PluginContainer;
34 import org.apache.maven.model.PluginExecution;
35 import org.apache.maven.model.PluginManagement;
36 import org.apache.maven.model.building.ModelBuildingRequest;
37 import org.apache.maven.model.building.ModelProblemCollector;
38 import org.apache.maven.model.merge.MavenModelMerger;
39
40
41
42
43
44
45 @SuppressWarnings({"checkstyle:methodname"})
46 @Named
47 @Singleton
48 public class DefaultPluginManagementInjector implements PluginManagementInjector {
49
50 private ManagementModelMerger merger = new ManagementModelMerger();
51
52 @Override
53 public void injectManagement(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
54 merger.mergeManagedBuildPlugins(model);
55 }
56
57
58
59
60 protected static class ManagementModelMerger extends MavenModelMerger {
61
62 public void mergeManagedBuildPlugins(Model model) {
63 Build build = model.getBuild();
64 if (build != null) {
65 PluginManagement pluginManagement = build.getPluginManagement();
66 if (pluginManagement != null) {
67 mergePluginContainerPlugins(build, pluginManagement);
68 }
69 }
70 }
71
72 private void mergePluginContainerPlugins(PluginContainer target, PluginContainer source) {
73 List<Plugin> src = source.getPlugins();
74 if (!src.isEmpty()) {
75 List<Plugin> tgt = target.getPlugins();
76
77 Map<Object, Plugin> managedPlugins = new LinkedHashMap<>(src.size() * 2);
78
79 Map<Object, Object> context = Collections.emptyMap();
80
81 for (Plugin element : src) {
82 Object key = getPluginKey(element);
83 managedPlugins.put(key, element);
84 }
85
86 for (Plugin element : tgt) {
87 Object key = getPluginKey(element);
88 Plugin managedPlugin = managedPlugins.get(key);
89 if (managedPlugin != null) {
90 mergePlugin(element, managedPlugin, false, context);
91 }
92 }
93 }
94 }
95
96 @Override
97 protected void mergePlugin_Executions(
98 Plugin target, Plugin source, boolean sourceDominant, Map<Object, Object> context) {
99 List<PluginExecution> src = source.getExecutions();
100 if (!src.isEmpty()) {
101 List<PluginExecution> tgt = target.getExecutions();
102
103 Map<Object, PluginExecution> merged = new LinkedHashMap<>((src.size() + tgt.size()) * 2);
104
105 for (PluginExecution element : src) {
106 Object key = getPluginExecutionKey(element);
107 merged.put(key, element.clone());
108 }
109
110 for (PluginExecution element : tgt) {
111 Object key = getPluginExecutionKey(element);
112 PluginExecution existing = merged.get(key);
113 if (existing != null) {
114 mergePluginExecution(element, existing, sourceDominant, context);
115 }
116 merged.put(key, element);
117 }
118
119 target.setExecutions(new ArrayList<>(merged.values()));
120 }
121 }
122 }
123 }