1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.model.profile;
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.BuildBase;
32 import org.apache.maven.model.Model;
33 import org.apache.maven.model.ModelBase;
34 import org.apache.maven.model.Plugin;
35 import org.apache.maven.model.PluginContainer;
36 import org.apache.maven.model.PluginExecution;
37 import org.apache.maven.model.Profile;
38 import org.apache.maven.model.ReportPlugin;
39 import org.apache.maven.model.ReportSet;
40 import org.apache.maven.model.Reporting;
41 import org.apache.maven.model.building.ModelBuildingRequest;
42 import org.apache.maven.model.building.ModelProblemCollector;
43 import org.apache.maven.model.merge.MavenModelMerger;
44
45
46
47
48
49
50 @Named
51 @Singleton
52 @SuppressWarnings({"checkstyle:methodname"})
53 public class DefaultProfileInjector implements ProfileInjector {
54
55 private ProfileModelMerger merger = new ProfileModelMerger();
56
57 @Override
58 public void injectProfile(
59 Model model, Profile profile, ModelBuildingRequest request, ModelProblemCollector problems) {
60 if (profile != null) {
61 merger.mergeModelBase(model, profile);
62
63 if (profile.getBuild() != null) {
64 if (model.getBuild() == null) {
65 model.setBuild(new Build());
66 }
67 merger.mergeBuildBase(model.getBuild(), profile.getBuild());
68 }
69 }
70 }
71
72
73
74
75 protected static class ProfileModelMerger extends MavenModelMerger {
76
77 public void mergeModelBase(ModelBase target, ModelBase source) {
78 mergeModelBase(target, source, true, Collections.emptyMap());
79 }
80
81 public void mergeBuildBase(BuildBase target, BuildBase source) {
82 mergeBuildBase(target, source, true, Collections.emptyMap());
83 }
84
85 @Override
86 protected void mergePluginContainer_Plugins(
87 PluginContainer target, PluginContainer source, boolean sourceDominant, Map<Object, Object> context) {
88 List<Plugin> src = source.getPlugins();
89 if (!src.isEmpty()) {
90 List<Plugin> tgt = target.getPlugins();
91 Map<Object, Plugin> master = new LinkedHashMap<>(tgt.size() * 2);
92
93 for (Plugin element : tgt) {
94 Object key = getPluginKey(element);
95 master.put(key, element);
96 }
97
98 Map<Object, List<Plugin>> predecessors = new LinkedHashMap<>();
99 List<Plugin> pending = new ArrayList<>();
100 for (Plugin element : src) {
101 Object key = getPluginKey(element);
102 Plugin existing = master.get(key);
103 if (existing != null) {
104 mergePlugin(existing, element, sourceDominant, context);
105
106 if (!pending.isEmpty()) {
107 predecessors.put(key, pending);
108 pending = new ArrayList<>();
109 }
110 } else {
111 pending.add(element.clone());
112 }
113 }
114
115 List<Plugin> result = new ArrayList<>(src.size() + tgt.size());
116 for (Map.Entry<Object, Plugin> entry : master.entrySet()) {
117 List<Plugin> pre = predecessors.get(entry.getKey());
118 if (pre != null) {
119 result.addAll(pre);
120 }
121 result.add(entry.getValue());
122 }
123 result.addAll(pending);
124
125 target.setPlugins(result);
126 }
127 }
128
129 @Override
130 protected void mergePlugin_Executions(
131 Plugin target, Plugin source, boolean sourceDominant, Map<Object, Object> context) {
132 List<PluginExecution> src = source.getExecutions();
133 if (!src.isEmpty()) {
134 List<PluginExecution> tgt = target.getExecutions();
135 Map<Object, PluginExecution> merged = new LinkedHashMap<>((src.size() + tgt.size()) * 2);
136
137 for (PluginExecution element : tgt) {
138 Object key = getPluginExecutionKey(element);
139 merged.put(key, element);
140 }
141
142 for (PluginExecution element : src) {
143 Object key = getPluginExecutionKey(element);
144 PluginExecution existing = merged.get(key);
145 if (existing != null) {
146 mergePluginExecution(existing, element, sourceDominant, context);
147 } else {
148 merged.put(key, element.clone());
149 }
150 }
151
152 target.setExecutions(new ArrayList<>(merged.values()));
153 }
154 }
155
156 @Override
157 protected void mergeReporting_Plugins(
158 Reporting target, Reporting source, boolean sourceDominant, Map<Object, Object> context) {
159 List<ReportPlugin> src = source.getPlugins();
160 if (!src.isEmpty()) {
161 List<ReportPlugin> tgt = target.getPlugins();
162 Map<Object, ReportPlugin> merged = new LinkedHashMap<>((src.size() + tgt.size()) * 2);
163
164 for (ReportPlugin element : tgt) {
165 Object key = getReportPluginKey(element);
166 merged.put(key, element);
167 }
168
169 for (ReportPlugin element : src) {
170 Object key = getReportPluginKey(element);
171 ReportPlugin existing = merged.get(key);
172 if (existing == null) {
173 merged.put(key, element.clone());
174 } else {
175 mergeReportPlugin(existing, element, sourceDominant, context);
176 }
177 }
178
179 target.setPlugins(new ArrayList<>(merged.values()));
180 }
181 }
182
183 @Override
184 protected void mergeReportPlugin_ReportSets(
185 ReportPlugin target, ReportPlugin source, boolean sourceDominant, Map<Object, Object> context) {
186 List<ReportSet> src = source.getReportSets();
187 if (!src.isEmpty()) {
188 List<ReportSet> tgt = target.getReportSets();
189 Map<Object, ReportSet> merged = new LinkedHashMap<>((src.size() + tgt.size()) * 2);
190
191 for (ReportSet element : tgt) {
192 Object key = getReportSetKey(element);
193 merged.put(key, element);
194 }
195
196 for (ReportSet element : src) {
197 Object key = getReportSetKey(element);
198 ReportSet existing = merged.get(key);
199 if (existing != null) {
200 mergeReportSet(existing, element, sourceDominant, context);
201 } else {
202 merged.put(key, element.clone());
203 }
204 }
205
206 target.setReportSets(new ArrayList<>(merged.values()));
207 }
208 }
209 }
210 }