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.api.model.Build;
31 import org.apache.maven.api.model.Model;
32 import org.apache.maven.api.model.Plugin;
33 import org.apache.maven.api.model.PluginContainer;
34 import org.apache.maven.api.model.PluginExecution;
35 import org.apache.maven.api.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(
54 org.apache.maven.model.Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
55 model.update(merger.mergeManagedBuildPlugins(model.getDelegate()));
56 }
57
58
59
60
61 protected static class ManagementModelMerger extends MavenModelMerger {
62
63 public Model mergeManagedBuildPlugins(Model model) {
64 Build build = model.getBuild();
65 if (build != null) {
66 PluginManagement pluginManagement = build.getPluginManagement();
67 if (pluginManagement != null) {
68 return model.withBuild(mergePluginContainerPlugins(build, pluginManagement));
69 }
70 }
71 return model;
72 }
73
74 private Build mergePluginContainerPlugins(Build target, PluginContainer source) {
75 List<Plugin> src = source.getPlugins();
76 if (!src.isEmpty()) {
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().apply(element);
83 managedPlugins.put(key, element);
84 }
85
86 List<Plugin> newPlugins = new ArrayList<>();
87 for (Plugin element : target.getPlugins()) {
88 Object key = getPluginKey().apply(element);
89 Plugin managedPlugin = managedPlugins.get(key);
90 if (managedPlugin != null) {
91 element = mergePlugin(element, managedPlugin, false, context);
92 }
93 newPlugins.add(element);
94 }
95 return target.withPlugins(newPlugins);
96 }
97 return target;
98 }
99
100 @Override
101 protected void mergePlugin_Executions(
102 Plugin.Builder builder,
103 Plugin target,
104 Plugin source,
105 boolean sourceDominant,
106 Map<Object, Object> context) {
107 List<PluginExecution> src = source.getExecutions();
108 if (!src.isEmpty()) {
109 List<PluginExecution> tgt = target.getExecutions();
110
111 Map<Object, PluginExecution> merged = new LinkedHashMap<>((src.size() + tgt.size()) * 2);
112
113 for (PluginExecution element : src) {
114 Object key = getPluginExecutionKey().apply(element);
115 merged.put(key, element);
116 }
117
118 for (PluginExecution element : tgt) {
119 Object key = getPluginExecutionKey().apply(element);
120 PluginExecution existing = merged.get(key);
121 if (existing != null) {
122 element = mergePluginExecution(element, existing, sourceDominant, context);
123 }
124 merged.put(key, element);
125 }
126
127 builder.executions(merged.values());
128 }
129 }
130 }
131 }