1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl.model;
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
28 import org.apache.maven.api.di.Named;
29 import org.apache.maven.api.di.Singleton;
30 import org.apache.maven.api.model.Build;
31 import org.apache.maven.api.model.Dependency;
32 import org.apache.maven.api.model.Model;
33 import org.apache.maven.api.model.Plugin;
34 import org.apache.maven.api.services.ModelBuilderRequest;
35 import org.apache.maven.api.services.ModelProblemCollector;
36 import org.apache.maven.api.services.model.ModelNormalizer;
37
38
39
40
41
42 @Named
43 @Singleton
44 public class DefaultModelNormalizer implements ModelNormalizer {
45
46 private DuplicateMerger merger = new DuplicateMerger();
47
48 @Override
49 public Model mergeDuplicates(Model model, ModelBuilderRequest request, ModelProblemCollector problems) {
50 Model.Builder builder = Model.newBuilder(model);
51
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 plugin = merger.mergePlugin(plugin, first);
62 }
63 normalized.put(key, plugin);
64 }
65
66 if (plugins.size() != normalized.size()) {
67 builder.build(
68 Build.newBuilder(build).plugins(normalized.values()).build());
69 }
70 }
71
72
73
74
75
76
77
78
79 List<Dependency> dependencies = model.getDependencies();
80 Map<String, Dependency> normalized = new LinkedHashMap<>(dependencies.size() * 2);
81
82 for (Dependency dependency : dependencies) {
83 normalized.put(dependency.getManagementKey(), dependency);
84 }
85
86 if (dependencies.size() != normalized.size()) {
87 builder.dependencies(normalized.values());
88 }
89
90 return builder.build();
91 }
92
93
94
95
96 protected static class DuplicateMerger extends MavenModelMerger {
97
98 public Plugin mergePlugin(Plugin target, Plugin source) {
99 return super.mergePlugin(target, source, false, Collections.emptyMap());
100 }
101 }
102
103 @Override
104 public Model injectDefaultValues(Model model, ModelBuilderRequest request, ModelProblemCollector problems) {
105 Model.Builder builder = Model.newBuilder(model);
106
107 builder.dependencies(injectList(model.getDependencies(), this::injectDependency));
108 Build build = model.getBuild();
109 if (build != null) {
110 Build newBuild = Build.newBuilder(build)
111 .plugins(injectList(build.getPlugins(), this::injectPlugin))
112 .build();
113 builder.build(newBuild != build ? newBuild : null);
114 }
115
116 return builder.build();
117 }
118
119 private Plugin injectPlugin(Plugin p) {
120 return Plugin.newBuilder(p)
121 .dependencies(injectList(p.getDependencies(), this::injectDependency))
122 .build();
123 }
124
125 private Dependency injectDependency(Dependency d) {
126
127 return (d.getScope() == null || d.getScope().isEmpty()) ? d.withScope("compile") : d;
128 }
129
130
131
132
133 private <T> List<T> injectList(List<T> list, Function<T, T> modifer) {
134 List<T> newList = null;
135 for (int i = 0; i < list.size(); i++) {
136 T oldT = list.get(i);
137 T newT = modifer.apply(oldT);
138 if (newT != oldT) {
139 if (newList == null) {
140 newList = new ArrayList<>(list);
141 }
142 newList.set(i, newT);
143 }
144 }
145 return newList;
146 }
147 }