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