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;
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.id(modelProfile.getId());
83
84 org.apache.maven.api.model.Activation modelActivation = modelProfile.getActivation();
85
86 if (modelActivation != null) {
87 Activation.Builder activation = Activation.newBuilder();
88
89 activation.activeByDefault(modelActivation.isActiveByDefault());
90
91 activation.jdk(modelActivation.getJdk());
92
93 org.apache.maven.api.model.ActivationProperty modelProp = modelActivation.getProperty();
94
95 if (modelProp != null) {
96 ActivationProperty prop = ActivationProperty.newBuilder()
97 .name(modelProp.getName())
98 .value(modelProp.getValue())
99 .build();
100 activation.property(prop);
101 }
102
103 org.apache.maven.api.model.ActivationOS modelOs = modelActivation.getOs();
104
105 if (modelOs != null) {
106 ActivationOS os = ActivationOS.newBuilder()
107 .arch(modelOs.getArch())
108 .family(modelOs.getFamily())
109 .name(modelOs.getName())
110 .version(modelOs.getVersion())
111 .build();
112
113 activation.os(os);
114 }
115
116 org.apache.maven.api.model.ActivationFile modelFile = modelActivation.getFile();
117
118 if (modelFile != null) {
119 org.apache.maven.api.settings.ActivationFile file =
120 org.apache.maven.api.settings.ActivationFile.newBuilder()
121 .exists(modelFile.getExists())
122 .missing(modelFile.getMissing())
123 .build();
124
125 activation.file(file);
126 }
127
128 activation.packaging(modelActivation.getPackaging());
129
130 activation.condition(modelActivation.getCondition());
131
132 profile.activation(activation.build());
133 }
134
135 profile.properties(modelProfile.getProperties().entrySet().stream()
136 .collect(Collectors.toMap(
137 e -> e.getKey().toString(), e -> e.getValue().toString())));
138
139 List<org.apache.maven.api.model.Repository> repos = modelProfile.getRepositories();
140 if (repos != null) {
141 List<Repository> repositories = new ArrayList<>();
142 for (org.apache.maven.api.model.Repository repo : repos) {
143 repositories.add(convertToSettingsRepository(repo));
144 }
145 profile.repositories(repositories);
146 }
147
148 List<org.apache.maven.api.model.Repository> pluginRepos = modelProfile.getPluginRepositories();
149 if (pluginRepos != null) {
150 List<Repository> repositories = new ArrayList<>();
151 for (org.apache.maven.api.model.Repository pluginRepo : pluginRepos) {
152 repositories.add(convertToSettingsRepository(pluginRepo));
153 }
154 profile.pluginRepositories(repositories);
155 }
156
157 return profile.build();
158 }
159
160
161
162
163
164 public static org.apache.maven.api.model.Profile convertFromSettingsProfile(Profile settingsProfile) {
165 org.apache.maven.api.model.Profile.Builder profile = org.apache.maven.api.model.Profile.newBuilder();
166
167 profile.id(settingsProfile.getId());
168
169 Activation settingsActivation = settingsProfile.getActivation();
170
171 if (settingsActivation != null) {
172 org.apache.maven.api.model.Activation.Builder activation =
173 org.apache.maven.api.model.Activation.newBuilder();
174
175 activation.activeByDefault(settingsActivation.isActiveByDefault());
176 activation.location("activeByDefault", toLocation(settingsActivation.getLocation("activeByDefault")));
177
178 activation.jdk(settingsActivation.getJdk());
179 activation.location("jdk", toLocation(settingsActivation.getLocation("jdk")));
180
181 ActivationProperty settingsProp = settingsActivation.getProperty();
182 if (settingsProp != null) {
183 activation.property(org.apache.maven.api.model.ActivationProperty.newBuilder()
184 .name(settingsProp.getName())
185 .value(settingsProp.getValue())
186 .location("name", toLocation(settingsProp.getLocation("name")))
187 .location("value", toLocation(settingsProp.getLocation("value")))
188 .build());
189 }
190
191 ActivationOS settingsOs = settingsActivation.getOs();
192 if (settingsOs != null) {
193 activation.os(org.apache.maven.api.model.ActivationOS.newBuilder()
194 .arch(settingsOs.getArch())
195 .family(settingsOs.getFamily())
196 .name(settingsOs.getName())
197 .version(settingsOs.getVersion())
198 .location("arch", toLocation(settingsOs.getLocation("arch")))
199 .location("family", toLocation(settingsOs.getLocation("family")))
200 .location("name", toLocation(settingsOs.getLocation("name")))
201 .location("version", toLocation(settingsOs.getLocation("version")))
202 .build());
203 }
204
205 org.apache.maven.api.settings.ActivationFile settingsFile = settingsActivation.getFile();
206 if (settingsFile != null) {
207 activation.file(ActivationFile.newBuilder()
208 .exists(settingsFile.getExists())
209 .missing(settingsFile.getMissing())
210 .location("exists", toLocation(settingsFile.getLocation("exists")))
211 .location("missing", toLocation(settingsFile.getLocation("missing")))
212 .build());
213 }
214
215 activation.packaging(settingsActivation.getPackaging());
216
217 activation.condition(settingsActivation.getCondition());
218
219 profile.activation(activation.build());
220 }
221
222 profile.properties(settingsProfile.getProperties());
223 profile.location("properties", toLocation(settingsProfile.getLocation("properties")));
224
225 List<Repository> repos = settingsProfile.getRepositories();
226 if (repos != null) {
227 profile.repositories(repos.stream()
228 .map(SettingsUtilsV4::convertFromSettingsRepository)
229 .collect(Collectors.toList()));
230 }
231
232 List<Repository> pluginRepos = settingsProfile.getPluginRepositories();
233 if (pluginRepos != null) {
234 profile.pluginRepositories(pluginRepos.stream()
235 .map(SettingsUtilsV4::convertFromSettingsRepository)
236 .collect(Collectors.toList()));
237 }
238
239 org.apache.maven.api.model.Profile value = profile.build();
240 value.setSource("settings.xml");
241 return value;
242 }
243
244
245
246
247
248 private static org.apache.maven.api.model.Repository convertFromSettingsRepository(Repository settingsRepo) {
249 org.apache.maven.api.model.Repository.Builder repo = org.apache.maven.api.model.Repository.newBuilder();
250
251 repo.id(settingsRepo.getId());
252 repo.layout(settingsRepo.getLayout());
253 repo.name(settingsRepo.getName());
254 repo.url(settingsRepo.getUrl());
255
256 repo.location("id", toLocation(settingsRepo.getLocation("id")));
257 repo.location("layout", toLocation(settingsRepo.getLocation("layout")));
258 repo.location("name", toLocation(settingsRepo.getLocation("name")));
259 repo.location("url", toLocation(settingsRepo.getLocation("url")));
260
261 if (settingsRepo.getSnapshots() != null) {
262 repo.snapshots(convertRepositoryPolicy(settingsRepo.getSnapshots()));
263 }
264 if (settingsRepo.getReleases() != null) {
265 repo.releases(convertRepositoryPolicy(settingsRepo.getReleases()));
266 }
267
268 return repo.build();
269 }
270
271
272
273
274
275 private static org.apache.maven.api.model.RepositoryPolicy convertRepositoryPolicy(
276 RepositoryPolicy settingsPolicy) {
277 org.apache.maven.api.model.RepositoryPolicy policy = org.apache.maven.api.model.RepositoryPolicy.newBuilder()
278 .enabled(Boolean.toString(settingsPolicy.isEnabled()))
279 .updatePolicy(settingsPolicy.getUpdatePolicy())
280 .checksumPolicy(settingsPolicy.getChecksumPolicy())
281 .location("enabled", toLocation(settingsPolicy.getLocation("enabled")))
282 .location("updatePolicy", toLocation(settingsPolicy.getLocation("updatePolicy")))
283 .location("checksumPolicy", toLocation(settingsPolicy.getLocation("checksumPolicy")))
284 .build();
285 return policy;
286 }
287
288
289
290
291
292 private static Repository convertToSettingsRepository(org.apache.maven.api.model.Repository modelRepo) {
293 Repository repo = Repository.newBuilder()
294 .id(modelRepo.getId())
295 .layout(modelRepo.getLayout())
296 .name(modelRepo.getName())
297 .url(modelRepo.getUrl())
298 .snapshots(modelRepo.getSnapshots() != null ? convertRepositoryPolicy(modelRepo.getSnapshots()) : null)
299 .releases(modelRepo.getReleases() != null ? convertRepositoryPolicy(modelRepo.getReleases()) : null)
300 .build();
301
302 return repo;
303 }
304
305
306
307
308
309 private static RepositoryPolicy convertRepositoryPolicy(org.apache.maven.api.model.RepositoryPolicy modelPolicy) {
310 RepositoryPolicy policy = RepositoryPolicy.newBuilder()
311 .enabled(modelPolicy.isEnabled())
312 .updatePolicy(modelPolicy.getUpdatePolicy())
313 .checksumPolicy(modelPolicy.getChecksumPolicy())
314 .build();
315 return policy;
316 }
317
318 private static org.apache.maven.api.model.InputLocation toLocation(
319 org.apache.maven.api.settings.InputLocation location) {
320 if (location != null) {
321 org.apache.maven.api.settings.InputSource source = location.getSource();
322 Map<Object, InputLocation> locs = location.getLocations().entrySet().stream()
323 .collect(Collectors.toMap(Map.Entry::getKey, e -> toLocation(e.getValue())));
324 return new org.apache.maven.api.model.InputLocation(
325 location.getLineNumber(),
326 location.getColumnNumber(),
327 source != null ? new org.apache.maven.api.model.InputSource("", source.getLocation()) : null,
328 locs);
329 } else {
330 return null;
331 }
332 }
333 }