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