1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.impl;
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.stream.Collectors;
45
46 import org.apache.maven.api.model.ActivationFile;
47 import org.apache.maven.api.model.InputLocation;
48 import org.apache.maven.api.settings.Activation;
49 import org.apache.maven.api.settings.ActivationOS;
50 import org.apache.maven.api.settings.ActivationProperty;
51 import org.apache.maven.api.settings.Profile;
52 import org.apache.maven.api.settings.Repository;
53 import org.apache.maven.api.settings.RepositoryPolicy;
54 import org.apache.maven.api.settings.Settings;
55 import org.apache.maven.settings.v4.SettingsMerger;
56
57
58
59
60
61 public final class SettingsUtilsV4 {
62
63 private SettingsUtilsV4() {
64
65 }
66
67
68
69
70
71 public static Settings merge(Settings dominant, Settings recessive) {
72 return new SettingsMerger().merge(dominant, recessive, true, Collections.emptyMap());
73 }
74
75
76
77
78
79 public static Profile convertToSettingsProfile(org.apache.maven.api.model.Profile modelProfile) {
80 Profile.Builder profile = Profile.newBuilder();
81
82 profile.location("", toLocation(modelProfile.getLocation("")));
83
84 profile.id(modelProfile.getId());
85 profile.location("id", toLocation(modelProfile.getLocation("id")));
86
87 org.apache.maven.api.model.Activation modelActivation = modelProfile.getActivation();
88
89 if (modelActivation != null) {
90 Activation.Builder activation = Activation.newBuilder();
91
92 activation.location("", toLocation(modelActivation.getLocation("")));
93
94 activation.activeByDefault(modelActivation.isActiveByDefault());
95 activation.location("activeByDefault", toLocation(modelActivation.getLocation("activeByDefault")));
96
97 activation.jdk(modelActivation.getJdk());
98 activation.location("jdk", toLocation(modelActivation.getLocation("jdk")));
99
100 org.apache.maven.api.model.ActivationProperty modelProp = modelActivation.getProperty();
101
102 if (modelProp != null) {
103 ActivationProperty prop = ActivationProperty.newBuilder()
104 .name(modelProp.getName())
105 .value(modelProp.getValue())
106 .location("", toLocation(modelProp.getLocation("")))
107 .location("name", toLocation(modelProp.getLocation("name")))
108 .location("value", toLocation(modelProp.getLocation("value")))
109 .build();
110 activation.property(prop);
111 }
112
113 org.apache.maven.api.model.ActivationOS modelOs = modelActivation.getOs();
114
115 if (modelOs != null) {
116 ActivationOS os = ActivationOS.newBuilder()
117 .arch(modelOs.getArch())
118 .family(modelOs.getFamily())
119 .name(modelOs.getName())
120 .version(modelOs.getVersion())
121 .location("", toLocation(modelOs.getLocation("")))
122 .location("arch", toLocation(modelOs.getLocation("arch")))
123 .location("family", toLocation(modelOs.getLocation("family")))
124 .location("name", toLocation(modelOs.getLocation("name")))
125 .location("version", toLocation(modelOs.getLocation("version")))
126 .build();
127
128 activation.os(os);
129 }
130
131 org.apache.maven.api.model.ActivationFile modelFile = modelActivation.getFile();
132
133 if (modelFile != null) {
134 org.apache.maven.api.settings.ActivationFile file =
135 org.apache.maven.api.settings.ActivationFile.newBuilder()
136 .exists(modelFile.getExists())
137 .missing(modelFile.getMissing())
138 .location("", toLocation(modelFile.getLocation("")))
139 .location("exists", toLocation(modelFile.getLocation("exists")))
140 .location("missing", toLocation(modelFile.getLocation("missing")))
141 .build();
142
143 activation.file(file);
144 }
145
146 activation.packaging(modelActivation.getPackaging());
147 activation.location("packaging", toLocation(modelActivation.getLocation("packaging")));
148
149 activation.condition(modelActivation.getCondition());
150 activation.location("condition", toLocation(modelActivation.getLocation("condition")));
151
152 profile.activation(activation.build());
153 }
154
155 profile.properties(modelProfile.getProperties().entrySet().stream()
156 .collect(Collectors.toMap(
157 e -> e.getKey().toString(), e -> e.getValue().toString())));
158 profile.location("properties", toLocation(modelProfile.getLocation("properties")));
159
160 List<org.apache.maven.api.model.Repository> repos = modelProfile.getRepositories();
161 if (repos != null) {
162 List<Repository> repositories = new ArrayList<>();
163 for (org.apache.maven.api.model.Repository repo : repos) {
164 repositories.add(convertToSettingsRepository(repo));
165 }
166 profile.repositories(repositories);
167 profile.location("repositories", toLocation(modelProfile.getLocation("repositories")));
168 }
169
170 List<org.apache.maven.api.model.Repository> pluginRepos = modelProfile.getPluginRepositories();
171 if (pluginRepos != null) {
172 List<Repository> repositories = new ArrayList<>();
173 for (org.apache.maven.api.model.Repository pluginRepo : pluginRepos) {
174 repositories.add(convertToSettingsRepository(pluginRepo));
175 }
176 profile.pluginRepositories(repositories);
177 profile.location("pluginRepositories", toLocation(modelProfile.getLocation("pluginRepositories")));
178 }
179
180 return profile.build();
181 }
182
183
184
185
186
187 public static org.apache.maven.api.model.Profile convertFromSettingsProfile(Profile settingsProfile) {
188 org.apache.maven.api.model.Profile.Builder profile = org.apache.maven.api.model.Profile.newBuilder();
189
190 profile.location("", toLocation(settingsProfile.getLocation("")));
191
192 profile.id(settingsProfile.getId());
193 profile.location("id", toLocation(settingsProfile.getLocation("id")));
194
195 Activation settingsActivation = settingsProfile.getActivation();
196
197 if (settingsActivation != null) {
198 org.apache.maven.api.model.Activation.Builder activation =
199 org.apache.maven.api.model.Activation.newBuilder();
200
201 activation.activeByDefault(settingsActivation.isActiveByDefault());
202 activation.location("activeByDefault", toLocation(settingsActivation.getLocation("activeByDefault")));
203
204 activation.jdk(settingsActivation.getJdk());
205 activation.location("jdk", toLocation(settingsActivation.getLocation("jdk")));
206
207 ActivationProperty settingsProp = settingsActivation.getProperty();
208 if (settingsProp != null) {
209 activation.property(org.apache.maven.api.model.ActivationProperty.newBuilder()
210 .name(settingsProp.getName())
211 .value(settingsProp.getValue())
212 .location("", toLocation(settingsProp.getLocation("")))
213 .location("name", toLocation(settingsProp.getLocation("name")))
214 .location("value", toLocation(settingsProp.getLocation("value")))
215 .build());
216 }
217
218 ActivationOS settingsOs = settingsActivation.getOs();
219 if (settingsOs != null) {
220 activation.os(org.apache.maven.api.model.ActivationOS.newBuilder()
221 .arch(settingsOs.getArch())
222 .family(settingsOs.getFamily())
223 .name(settingsOs.getName())
224 .version(settingsOs.getVersion())
225 .location("", toLocation(settingsOs.getLocation("")))
226 .location("arch", toLocation(settingsOs.getLocation("arch")))
227 .location("family", toLocation(settingsOs.getLocation("family")))
228 .location("name", toLocation(settingsOs.getLocation("name")))
229 .location("version", toLocation(settingsOs.getLocation("version")))
230 .build());
231 }
232
233 org.apache.maven.api.settings.ActivationFile settingsFile = settingsActivation.getFile();
234 if (settingsFile != null) {
235 activation.file(ActivationFile.newBuilder()
236 .exists(settingsFile.getExists())
237 .missing(settingsFile.getMissing())
238 .location("", toLocation(settingsFile.getLocation("")))
239 .location("exists", toLocation(settingsFile.getLocation("exists")))
240 .location("missing", toLocation(settingsFile.getLocation("missing")))
241 .build());
242 }
243
244 activation.packaging(settingsActivation.getPackaging());
245 activation.location("packaging", toLocation(settingsActivation.getLocation("packaging")));
246
247 activation.condition(settingsActivation.getCondition());
248 activation.location("condition", toLocation(settingsActivation.getLocation("condition")));
249
250 profile.activation(activation.build());
251 }
252
253 profile.properties(settingsProfile.getProperties());
254 profile.location("properties", toLocation(settingsProfile.getLocation("properties")));
255
256 List<Repository> repos = settingsProfile.getRepositories();
257 if (repos != null) {
258 profile.repositories(repos.stream()
259 .map(SettingsUtilsV4::convertFromSettingsRepository)
260 .collect(Collectors.toList()));
261 }
262
263 List<Repository> pluginRepos = settingsProfile.getPluginRepositories();
264 if (pluginRepos != null) {
265 profile.pluginRepositories(pluginRepos.stream()
266 .map(SettingsUtilsV4::convertFromSettingsRepository)
267 .collect(Collectors.toList()));
268 }
269
270 org.apache.maven.api.model.Profile value = profile.build();
271 value.setSource("settings.xml");
272 return value;
273 }
274
275
276
277
278
279 private static org.apache.maven.api.model.Repository convertFromSettingsRepository(Repository settingsRepo) {
280 org.apache.maven.api.model.Repository.Builder repo = org.apache.maven.api.model.Repository.newBuilder();
281
282 repo.id(settingsRepo.getId());
283 repo.layout(settingsRepo.getLayout());
284 repo.name(settingsRepo.getName());
285 repo.url(settingsRepo.getUrl());
286
287 repo.location("id", toLocation(settingsRepo.getLocation("id")));
288 repo.location("layout", toLocation(settingsRepo.getLocation("layout")));
289 repo.location("name", toLocation(settingsRepo.getLocation("name")));
290 repo.location("url", toLocation(settingsRepo.getLocation("url")));
291
292 if (settingsRepo.getSnapshots() != null) {
293 repo.snapshots(convertRepositoryPolicy(settingsRepo.getSnapshots()));
294 }
295 if (settingsRepo.getReleases() != null) {
296 repo.releases(convertRepositoryPolicy(settingsRepo.getReleases()));
297 }
298
299 return repo.build();
300 }
301
302
303
304
305
306 private static org.apache.maven.api.model.RepositoryPolicy convertRepositoryPolicy(
307 RepositoryPolicy settingsPolicy) {
308 org.apache.maven.api.model.RepositoryPolicy policy = org.apache.maven.api.model.RepositoryPolicy.newBuilder()
309 .enabled(Boolean.toString(settingsPolicy.isEnabled()))
310 .updatePolicy(settingsPolicy.getUpdatePolicy())
311 .checksumPolicy(settingsPolicy.getChecksumPolicy())
312 .location("enabled", toLocation(settingsPolicy.getLocation("enabled")))
313 .location("updatePolicy", toLocation(settingsPolicy.getLocation("updatePolicy")))
314 .location("checksumPolicy", toLocation(settingsPolicy.getLocation("checksumPolicy")))
315 .build();
316 return policy;
317 }
318
319
320
321
322
323 private static Repository convertToSettingsRepository(org.apache.maven.api.model.Repository modelRepo) {
324 Repository repo = Repository.newBuilder()
325 .id(modelRepo.getId())
326 .layout(modelRepo.getLayout())
327 .name(modelRepo.getName())
328 .url(modelRepo.getUrl())
329 .snapshots(modelRepo.getSnapshots() != null ? convertRepositoryPolicy(modelRepo.getSnapshots()) : null)
330 .releases(modelRepo.getReleases() != null ? convertRepositoryPolicy(modelRepo.getReleases()) : null)
331 .location("", toLocation(modelRepo.getLocation("")))
332 .location("id", toLocation(modelRepo.getLocation("id")))
333 .location("layout", toLocation(modelRepo.getLocation("layout")))
334 .location("name", toLocation(modelRepo.getLocation("name")))
335 .location("url", toLocation(modelRepo.getLocation("url")))
336 .build();
337
338 return repo;
339 }
340
341
342
343
344
345 private static RepositoryPolicy convertRepositoryPolicy(org.apache.maven.api.model.RepositoryPolicy modelPolicy) {
346 RepositoryPolicy policy = RepositoryPolicy.newBuilder()
347 .enabled(modelPolicy.isEnabled())
348 .updatePolicy(modelPolicy.getUpdatePolicy())
349 .checksumPolicy(modelPolicy.getChecksumPolicy())
350 .location("", toLocation(modelPolicy.getLocation("")))
351 .location("enabled", toLocation(modelPolicy.getLocation("enabled")))
352 .location("updatePolicy", toLocation(modelPolicy.getLocation("updatePolicy")))
353 .location("checksumPolicy", toLocation(modelPolicy.getLocation("checksumPolicy")))
354 .build();
355 return policy;
356 }
357
358 private static org.apache.maven.api.settings.InputLocation toLocation(
359 org.apache.maven.api.model.InputLocation location) {
360 if (location != null) {
361 org.apache.maven.api.model.InputSource source = location.getSource();
362 Map<Object, org.apache.maven.api.settings.InputLocation> locs = location.getLocations().entrySet().stream()
363 .collect(Collectors.toMap(Map.Entry::getKey, e -> toLocation(e.getValue())));
364 return new org.apache.maven.api.settings.InputLocation(
365 location.getLineNumber(),
366 location.getColumnNumber(),
367 source != null ? new org.apache.maven.api.settings.InputSource(source.getLocation()) : null,
368 locs);
369 } else {
370 return null;
371 }
372 }
373
374 private static org.apache.maven.api.model.InputLocation toLocation(
375 org.apache.maven.api.settings.InputLocation location) {
376 if (location != null) {
377 org.apache.maven.api.settings.InputSource source = location.getSource();
378 Map<Object, InputLocation> locs = location.getLocations().entrySet().stream()
379 .collect(Collectors.toMap(Map.Entry::getKey, e -> toLocation(e.getValue())));
380 return new org.apache.maven.api.model.InputLocation(
381 location.getLineNumber(),
382 location.getColumnNumber(),
383 source != null ? new org.apache.maven.api.model.InputSource("", source.getLocation()) : null,
384 locs);
385 } else {
386 return null;
387 }
388 }
389 }