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 java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.function.Function;
27 import javax.inject.Named;
28 import javax.inject.Singleton;
29 import org.apache.maven.api.model.Build;
30 import org.apache.maven.api.model.Dependency;
31 import org.apache.maven.api.model.Model;
32 import org.apache.maven.api.model.Plugin;
33 import org.apache.maven.model.building.ModelBuildingRequest;
34 import org.apache.maven.model.building.ModelProblemCollector;
35 import org.apache.maven.model.merge.MavenModelMerger;
36 import org.codehaus.plexus.util.StringUtils;
37
38
39
40
41
42
43 @Named
44 @Singleton
45 public class DefaultModelNormalizer implements ModelNormalizer {
46
47 private DuplicateMerger merger = new DuplicateMerger();
48
49 @Override
50 public Model mergeDuplicates(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
51 Model.Builder builder = Model.newBuilder(model);
52
53 Build build = model.getBuild();
54 if (build != null) {
55 List<Plugin> plugins = build.getPlugins();
56 Map<Object, Plugin> normalized = new LinkedHashMap<>(plugins.size() * 2);
57
58 for (Plugin plugin : plugins) {
59 Object key = plugin.getKey();
60 Plugin first = normalized.get(key);
61 if (first != null) {
62 plugin = merger.mergePlugin(plugin, first);
63 }
64 normalized.put(key, plugin);
65 }
66
67 if (plugins.size() != normalized.size()) {
68 builder.build(
69 Build.newBuilder(build).plugins(normalized.values()).build());
70 }
71 }
72
73
74
75
76
77
78
79
80 List<Dependency> dependencies = model.getDependencies();
81 Map<String, Dependency> normalized = new LinkedHashMap<>(dependencies.size() * 2);
82
83 for (Dependency dependency : dependencies) {
84 normalized.put(dependency.getManagementKey(), dependency);
85 }
86
87 if (dependencies.size() != normalized.size()) {
88 builder.dependencies(normalized.values());
89 }
90
91 return builder.build();
92 }
93
94
95
96
97 protected static class DuplicateMerger extends MavenModelMerger {
98
99 public Plugin mergePlugin(Plugin target, Plugin source) {
100 return super.mergePlugin(target, source, false, Collections.emptyMap());
101 }
102 }
103
104 @Override
105 public Model injectDefaultValues(Model model, ModelBuildingRequest request, ModelProblemCollector problems) {
106 Model.Builder builder = Model.newBuilder(model);
107
108 builder.dependencies(injectList(model.getDependencies(), this::injectDependency));
109 Build build = model.getBuild();
110 if (build != null) {
111 Build newBuild = Build.newBuilder(build)
112 .plugins(injectList(build.getPlugins(), this::injectPlugin))
113 .build();
114 builder.build(newBuild != build ? newBuild : null);
115 }
116
117 return builder.build();
118 }
119
120 private Plugin injectPlugin(Plugin p) {
121 return Plugin.newBuilder(p)
122 .dependencies(injectList(p.getDependencies(), this::injectDependency))
123 .build();
124 }
125
126 private Dependency injectDependency(Dependency d) {
127
128 return StringUtils.isEmpty(d.getScope())
129 ? Dependency.newBuilder(d).scope("compile").build()
130 : d;
131 }
132
133
134
135
136 private <T> List<T> injectList(List<T> list, Function<T, T> modifer) {
137 List<T> newList = null;
138 for (int i = 0; i < list.size(); i++) {
139 T oldT = list.get(i);
140 T newT = modifer.apply(oldT);
141 if (newT != oldT) {
142 if (newList == null) {
143 newList = new ArrayList<>(list);
144 }
145 newList.set(i, newT);
146 }
147 }
148 return newList;
149 }
150 }