1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.settings;
20
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.stream.Collectors;
24 import org.apache.maven.api.model.ActivationFile;
25 import org.apache.maven.api.settings.Activation;
26 import org.apache.maven.api.settings.ActivationOS;
27 import org.apache.maven.api.settings.ActivationProperty;
28 import org.apache.maven.api.settings.Profile;
29 import org.apache.maven.api.settings.Repository;
30 import org.apache.maven.api.settings.RepositoryPolicy;
31 import org.apache.maven.api.settings.Settings;
32 import org.apache.maven.settings.merge.MavenSettingsMerger;
33
34
35
36
37
38
39 public final class SettingsUtils {
40
41 private SettingsUtils() {
42
43 }
44
45
46
47
48
49
50 public static Settings merge(Settings dominant, Settings recessive, String recessiveSourceLevel) {
51 return new MavenSettingsMerger().merge(dominant, recessive, recessiveSourceLevel);
52 }
53
54
55
56
57
58 public static Profile convertToSettingsProfile(org.apache.maven.model.Profile modelProfile) {
59 Profile.Builder profile = Profile.newBuilder();
60
61 profile.id(modelProfile.getId());
62
63 org.apache.maven.model.Activation modelActivation = modelProfile.getActivation();
64
65 if (modelActivation != null) {
66 Activation.Builder activation = Activation.newBuilder();
67
68 activation.activeByDefault(modelActivation.isActiveByDefault());
69
70 activation.jdk(modelActivation.getJdk());
71
72 org.apache.maven.model.ActivationProperty modelProp = modelActivation.getProperty();
73
74 if (modelProp != null) {
75 ActivationProperty prop = ActivationProperty.newBuilder()
76 .name(modelProp.getName())
77 .value(modelProp.getValue())
78 .build();
79 activation.property(prop);
80 }
81
82 org.apache.maven.model.ActivationOS modelOs = modelActivation.getOs();
83
84 if (modelOs != null) {
85 ActivationOS os = ActivationOS.newBuilder()
86 .arch(modelOs.getArch())
87 .family(modelOs.getFamily())
88 .name(modelOs.getName())
89 .version(modelOs.getVersion())
90 .build();
91
92 activation.os(os);
93 }
94
95 org.apache.maven.model.ActivationFile modelFile = modelActivation.getFile();
96
97 if (modelFile != null) {
98 org.apache.maven.api.settings.ActivationFile file =
99 org.apache.maven.api.settings.ActivationFile.newBuilder()
100 .exists(modelFile.getExists())
101 .missing(modelFile.getMissing())
102 .build();
103
104 activation.file(file);
105 }
106
107 profile.activation(activation.build());
108 }
109
110 profile.properties(modelProfile.getProperties().entrySet().stream()
111 .collect(Collectors.toMap(
112 e -> e.getKey().toString(), e -> e.getValue().toString())));
113
114 List<org.apache.maven.model.Repository> repos = modelProfile.getRepositories();
115 if (repos != null) {
116 List<Repository> repositories = new ArrayList<>();
117 for (org.apache.maven.model.Repository repo : repos) {
118 repositories.add(convertToSettingsRepository(repo));
119 }
120 profile.repositories(repositories);
121 }
122
123 List<org.apache.maven.model.Repository> pluginRepos = modelProfile.getPluginRepositories();
124 if (pluginRepos != null) {
125 List<Repository> repositories = new ArrayList<>();
126 for (org.apache.maven.model.Repository pluginRepo : pluginRepos) {
127 repositories.add(convertToSettingsRepository(pluginRepo));
128 }
129 profile.pluginRepositories(repositories);
130 }
131
132 return profile.build();
133 }
134
135
136
137
138
139 public static org.apache.maven.model.Profile convertFromSettingsProfile(Profile settingsProfile) {
140 org.apache.maven.api.model.Profile.Builder profile = org.apache.maven.api.model.Profile.newBuilder();
141
142 profile.id(settingsProfile.getId());
143
144 Activation settingsActivation = settingsProfile.getActivation();
145
146 if (settingsActivation != null) {
147 org.apache.maven.api.model.Activation.Builder activation =
148 org.apache.maven.api.model.Activation.newBuilder();
149
150 activation.activeByDefault(settingsActivation.isActiveByDefault());
151
152 activation.jdk(settingsActivation.getJdk());
153
154 ActivationProperty settingsProp = settingsActivation.getProperty();
155 if (settingsProp != null) {
156 activation.property(org.apache.maven.api.model.ActivationProperty.newBuilder()
157 .name(settingsProp.getName())
158 .value(settingsProp.getValue())
159 .build());
160 }
161
162 ActivationOS settingsOs = settingsActivation.getOs();
163 if (settingsOs != null) {
164 activation.os(org.apache.maven.api.model.ActivationOS.newBuilder()
165 .arch(settingsOs.getArch())
166 .family(settingsOs.getFamily())
167 .name(settingsOs.getName())
168 .version(settingsOs.getVersion())
169 .build());
170 }
171
172 org.apache.maven.api.settings.ActivationFile settingsFile = settingsActivation.getFile();
173 if (settingsFile != null) {
174 activation.file(ActivationFile.newBuilder()
175 .exists(settingsFile.getExists())
176 .missing(settingsFile.getMissing())
177 .build());
178 }
179
180 profile.activation(activation.build());
181 }
182
183 profile.properties(settingsProfile.getProperties());
184
185 List<Repository> repos = settingsProfile.getRepositories();
186 if (repos != null) {
187 profile.repositories(repos.stream()
188 .map(SettingsUtils::convertFromSettingsRepository)
189 .collect(Collectors.toList()));
190 }
191
192 List<Repository> pluginRepos = settingsProfile.getPluginRepositories();
193 if (pluginRepos != null) {
194 profile.pluginRepositories(pluginRepos.stream()
195 .map(SettingsUtils::convertFromSettingsRepository)
196 .collect(Collectors.toList()));
197 }
198
199 org.apache.maven.model.Profile value = new org.apache.maven.model.Profile(profile.build());
200 value.setSource("settings.xml");
201 return value;
202 }
203
204
205
206
207
208 private static org.apache.maven.api.model.Repository convertFromSettingsRepository(Repository settingsRepo) {
209 org.apache.maven.api.model.Repository.Builder repo = org.apache.maven.api.model.Repository.newBuilder();
210
211 repo.id(settingsRepo.getId());
212 repo.layout(settingsRepo.getLayout());
213 repo.name(settingsRepo.getName());
214 repo.url(settingsRepo.getUrl());
215
216 if (settingsRepo.getSnapshots() != null) {
217 repo.snapshots(convertRepositoryPolicy(settingsRepo.getSnapshots()));
218 }
219 if (settingsRepo.getReleases() != null) {
220 repo.releases(convertRepositoryPolicy(settingsRepo.getReleases()));
221 }
222
223 return repo.build();
224 }
225
226
227
228
229
230 private static org.apache.maven.api.model.RepositoryPolicy convertRepositoryPolicy(
231 RepositoryPolicy settingsPolicy) {
232 org.apache.maven.api.model.RepositoryPolicy policy = org.apache.maven.api.model.RepositoryPolicy.newBuilder()
233 .enabled(Boolean.toString(settingsPolicy.isEnabled()))
234 .updatePolicy(settingsPolicy.getUpdatePolicy())
235 .checksumPolicy(settingsPolicy.getChecksumPolicy())
236 .build();
237 return policy;
238 }
239
240
241
242
243
244 private static Repository convertToSettingsRepository(org.apache.maven.model.Repository modelRepo) {
245 Repository repo = Repository.newBuilder()
246 .id(modelRepo.getId())
247 .layout(modelRepo.getLayout())
248 .name(modelRepo.getName())
249 .url(modelRepo.getUrl())
250 .snapshots(modelRepo.getSnapshots() != null ? convertRepositoryPolicy(modelRepo.getSnapshots()) : null)
251 .releases(modelRepo.getReleases() != null ? convertRepositoryPolicy(modelRepo.getReleases()) : null)
252 .build();
253
254 return repo;
255 }
256
257
258
259
260
261 private static RepositoryPolicy convertRepositoryPolicy(org.apache.maven.model.RepositoryPolicy modelPolicy) {
262 RepositoryPolicy policy = RepositoryPolicy.newBuilder()
263 .enabled(modelPolicy.isEnabled())
264 .updatePolicy(modelPolicy.getUpdatePolicy())
265 .checksumPolicy(modelPolicy.getUpdatePolicy())
266 .build();
267 return policy;
268 }
269
270
271
272
273
274 public static org.apache.maven.settings.Settings copySettings(org.apache.maven.settings.Settings settings) {
275 if (settings == null) {
276 return null;
277 }
278 return new org.apache.maven.settings.Settings(settings.getDelegate());
279 }
280 }