View Javadoc

1   package org.apache.maven.settings;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Several convenience methods to handle settings
29   *
30   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
31   */
32  public final class SettingsUtils
33  {
34  
35      private SettingsUtils()
36      {
37          // don't allow construction.
38      }
39  
40      /**
41       * @param dominant
42       * @param recessive
43       * @param recessiveSourceLevel
44       */
45      public static void merge( Settings dominant, Settings recessive, String recessiveSourceLevel )
46      {
47          new MavenSettingsMerger().merge( dominant, recessive, recessiveSourceLevel );
48      }
49  
50      /**
51       * @param modelProfile
52       * @return a profile
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      * @param settingsProfile
134      * @return a profile
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      * @param settingsRepo
220      * @return a repository
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      * @param settingsPolicy
245      * @return a RepositoryPolicy
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      * @param modelRepo
258      * @return a repository
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      * @param modelPolicy
283      * @return a RepositoryPolicy
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      * @param settings could be null
296      * @return a new instance of settings or null if settings was null.
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 }