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