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 org.apache.maven.model.ActivationFile;
23 import org.apache.maven.settings.merge.MavenSettingsMerger;
24
25 import java.util.List;
26
27
28
29
30
31
32 public final class SettingsUtils
33 {
34
35 private SettingsUtils()
36 {
37
38 }
39
40
41
42
43
44
45 public static void merge( Settings dominant, Settings recessive, String recessiveSourceLevel )
46 {
47 new MavenSettingsMerger().merge( dominant, recessive, recessiveSourceLevel );
48 }
49
50
51
52
53
54 public static Profile convertToSettingsProfile( org.apache.maven.model.Profile modelProfile )
55 {
56 Profile profile = new Profile();
57
58 profile.setId( modelProfile.getId() );
59
60 org.apache.maven.model.Activation modelActivation = modelProfile.getActivation();
61
62 if ( modelActivation != null )
63 {
64 Activation activation = new Activation();
65
66 activation.setActiveByDefault( modelActivation.isActiveByDefault() );
67
68 activation.setJdk( modelActivation.getJdk() );
69
70 org.apache.maven.model.ActivationProperty modelProp = modelActivation.getProperty();
71
72 if ( modelProp != null )
73 {
74 ActivationProperty prop = new ActivationProperty();
75 prop.setName( modelProp.getName() );
76 prop.setValue( modelProp.getValue() );
77 activation.setProperty( prop );
78 }
79
80 org.apache.maven.model.ActivationOS modelOs = modelActivation.getOs();
81
82 if ( modelOs != null )
83 {
84 ActivationOS os = new ActivationOS();
85
86 os.setArch( modelOs.getArch() );
87 os.setFamily( modelOs.getFamily() );
88 os.setName( modelOs.getName() );
89 os.setVersion( modelOs.getVersion() );
90
91 activation.setOs( os );
92 }
93
94 ActivationFile modelFile = modelActivation.getFile();
95
96 if ( modelFile != null )
97 {
98 org.apache.maven.settings.ActivationFile file = new org.apache.maven.settings.ActivationFile();
99
100 file.setExists( modelFile.getExists() );
101 file.setMissing( modelFile.getMissing() );
102
103 activation.setFile( file );
104 }
105
106 profile.setActivation( activation );
107 }
108
109 profile.setProperties( modelProfile.getProperties() );
110
111 List<org.apache.maven.model.Repository> repos = modelProfile.getRepositories();
112 if ( repos != null )
113 {
114 for ( org.apache.maven.model.Repository repo : repos )
115 {
116 profile.addRepository( convertToSettingsRepository( repo ) );
117 }
118 }
119
120 List<org.apache.maven.model.Repository> pluginRepos = modelProfile.getPluginRepositories();
121 if ( pluginRepos != null )
122 {
123 for ( org.apache.maven.model.Repository pluginRepo : pluginRepos )
124 {
125 profile.addPluginRepository( convertToSettingsRepository( pluginRepo ) );
126 }
127 }
128
129 return profile;
130 }
131
132
133
134
135
136 public static org.apache.maven.model.Profile convertFromSettingsProfile( Profile settingsProfile )
137 {
138 org.apache.maven.model.Profile profile = new org.apache.maven.model.Profile();
139
140 profile.setId( settingsProfile.getId() );
141
142 profile.setSource( "settings.xml" );
143
144 Activation settingsActivation = settingsProfile.getActivation();
145
146 if ( settingsActivation != null )
147 {
148 org.apache.maven.model.Activation activation = new org.apache.maven.model.Activation();
149
150 activation.setActiveByDefault( settingsActivation.isActiveByDefault() );
151
152 activation.setJdk( settingsActivation.getJdk() );
153
154 ActivationProperty settingsProp = settingsActivation.getProperty();
155
156 if ( settingsProp != null )
157 {
158 org.apache.maven.model.ActivationProperty prop = new org.apache.maven.model.ActivationProperty();
159
160 prop.setName( settingsProp.getName() );
161 prop.setValue( settingsProp.getValue() );
162
163 activation.setProperty( prop );
164 }
165
166 ActivationOS settingsOs = settingsActivation.getOs();
167
168 if ( settingsOs != null )
169 {
170 org.apache.maven.model.ActivationOS os = new org.apache.maven.model.ActivationOS();
171
172 os.setArch( settingsOs.getArch() );
173 os.setFamily( settingsOs.getFamily() );
174 os.setName( settingsOs.getName() );
175 os.setVersion( settingsOs.getVersion() );
176
177 activation.setOs( os );
178 }
179
180 org.apache.maven.settings.ActivationFile settingsFile = settingsActivation.getFile();
181
182 if ( settingsFile != null )
183 {
184 ActivationFile file = new ActivationFile();
185
186 file.setExists( settingsFile.getExists() );
187 file.setMissing( settingsFile.getMissing() );
188
189 activation.setFile( file );
190 }
191
192 profile.setActivation( activation );
193 }
194
195 profile.setProperties( settingsProfile.getProperties() );
196
197 List<Repository> repos = settingsProfile.getRepositories();
198 if ( repos != null )
199 {
200 for ( Repository repo : repos )
201 {
202 profile.addRepository( convertFromSettingsRepository( repo ) );
203 }
204 }
205
206 List<Repository> pluginRepos = settingsProfile.getPluginRepositories();
207 if ( pluginRepos != null )
208 {
209 for ( Repository pluginRepo : pluginRepos )
210 {
211 profile.addPluginRepository( convertFromSettingsRepository( pluginRepo ) );
212 }
213 }
214
215 return profile;
216 }
217
218
219
220
221
222 private static org.apache.maven.model.Repository convertFromSettingsRepository( Repository settingsRepo )
223 {
224 org.apache.maven.model.Repository repo = new org.apache.maven.model.Repository();
225
226 repo.setId( settingsRepo.getId() );
227 repo.setLayout( settingsRepo.getLayout() );
228 repo.setName( settingsRepo.getName() );
229 repo.setUrl( settingsRepo.getUrl() );
230
231 if ( settingsRepo.getSnapshots() != null )
232 {
233 repo.setSnapshots( convertRepositoryPolicy( settingsRepo.getSnapshots() ) );
234 }
235 if ( settingsRepo.getReleases() != null )
236 {
237 repo.setReleases( convertRepositoryPolicy( settingsRepo.getReleases() ) );
238 }
239
240 return repo;
241 }
242
243
244
245
246
247 private static org.apache.maven.model.RepositoryPolicy convertRepositoryPolicy( RepositoryPolicy settingsPolicy )
248 {
249 org.apache.maven.model.RepositoryPolicy policy = new org.apache.maven.model.RepositoryPolicy();
250 policy.setEnabled( settingsPolicy.isEnabled() );
251 policy.setUpdatePolicy( settingsPolicy.getUpdatePolicy() );
252 policy.setChecksumPolicy( settingsPolicy.getChecksumPolicy() );
253 return policy;
254 }
255
256
257
258
259
260 private static Repository convertToSettingsRepository( org.apache.maven.model.Repository modelRepo )
261 {
262 Repository repo = new Repository();
263
264 repo.setId( modelRepo.getId() );
265 repo.setLayout( modelRepo.getLayout() );
266 repo.setName( modelRepo.getName() );
267 repo.setUrl( modelRepo.getUrl() );
268
269 if ( modelRepo.getSnapshots() != null )
270 {
271 repo.setSnapshots( convertRepositoryPolicy( modelRepo.getSnapshots() ) );
272 }
273 if ( modelRepo.getReleases() != null )
274 {
275 repo.setReleases( convertRepositoryPolicy( modelRepo.getReleases() ) );
276 }
277
278 return repo;
279 }
280
281
282
283
284
285 private static RepositoryPolicy convertRepositoryPolicy( org.apache.maven.model.RepositoryPolicy modelPolicy )
286 {
287 RepositoryPolicy policy = new RepositoryPolicy();
288 policy.setEnabled( modelPolicy.isEnabled() );
289 policy.setUpdatePolicy( modelPolicy.getUpdatePolicy() );
290 policy.setChecksumPolicy( modelPolicy.getChecksumPolicy() );
291 return policy;
292 }
293
294
295
296
297
298 public static Settings copySettings( Settings settings )
299 {
300 if ( settings == null )
301 {
302 return null;
303 }
304
305 Settings clone = new Settings();
306 clone.setActiveProfiles( settings.getActiveProfiles() );
307 clone.setInteractiveMode( settings.isInteractiveMode() );
308 clone.setLocalRepository( settings.getLocalRepository() );
309 clone.setMirrors( settings.getMirrors() );
310 clone.setModelEncoding( settings.getModelEncoding() );
311 clone.setOffline( settings.isOffline() );
312 clone.setPluginGroups( settings.getPluginGroups() );
313 clone.setProfiles( settings.getProfiles() );
314 clone.setProxies( settings.getProxies() );
315 clone.setServers( settings.getServers() );
316 clone.setSourceLevel( settings.getSourceLevel() );
317 clone.setUsePluginRegistry( settings.isUsePluginRegistry() );
318
319 return clone;
320 }
321 }