1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.normalization;
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.Dependency;
32 import org.apache.maven.model.Model;
33 import org.apache.maven.model.Plugin;
34 import org.apache.maven.model.building.ModelBuildingRequest;
35 import org.apache.maven.model.building.ModelProblemCollector;
36 import org.apache.maven.model.merge.MavenModelMerger;
37 import org.codehaus.plexus.util.StringUtils;
38
39
40
41
42
43
44 @Named
45 @Singleton
46 public class DefaultModelNormalizer implements ModelNormalizer {
47
48 private DuplicateMerger merger = new DuplicateMerger();
49
50 @Override
51 public void mergeDuplicates(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
52 Build build = model.getBuild();
53 if (build != null) {
54 List<Plugin> plugins = build.getPlugins();
55 Map<Object, Plugin> normalized = new LinkedHashMap<>(plugins.size() * 2);
56
57 for (Plugin plugin : plugins) {
58 Object key = plugin.getKey();
59 Plugin first = normalized.get(key);
60 if (first != null) {
61 merger.mergePlugin(plugin, first);
62 }
63 normalized.put(key, plugin);
64 }
65
66 if (plugins.size() != normalized.size()) {
67 build.setPlugins(new ArrayList<>(normalized.values()));
68 }
69 }
70
71
72
73
74
75
76
77
78 List<Dependency> dependencies = model.getDependencies();
79 Map<String, Dependency> normalized = new LinkedHashMap<>(dependencies.size() * 2);
80
81 for (Dependency dependency : dependencies) {
82 normalized.put(dependency.getManagementKey(), dependency);
83 }
84
85 if (dependencies.size() != normalized.size()) {
86 model.setDependencies(new ArrayList<>(normalized.values()));
87 }
88 }
89
90
91
92
93 protected static class DuplicateMerger extends MavenModelMerger {
94
95 public void mergePlugin(Plugin target, Plugin source) {
96 super.mergePlugin(target, source, false, Collections.emptyMap());
97 }
98 }
99
100 @Override
101 public void injectDefaultValues(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
102 injectDependencyDefaults(model.getDependencies());
103
104 Build build = model.getBuild();
105 if (build != null) {
106 for (Plugin plugin : build.getPlugins()) {
107 injectDependencyDefaults(plugin.getDependencies());
108 }
109 }
110 }
111
112 private void injectDependencyDefaults(List<Dependency> dependencies) {
113 for (Dependency dependency : dependencies) {
114 if (StringUtils.isEmpty(dependency.getScope())) {
115
116 dependency.setScope("compile");
117 }
118 }
119 }
120 }