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 java.util.List;
23  
24  import org.apache.maven.model.ActivationFile;
25  import org.apache.maven.settings.merge.MavenSettingsMerger;
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 settingsProfile
52       * @return a profile
53       */
54      public static org.apache.maven.model.Profile convertFromSettingsProfile( Profile settingsProfile )
55      {
56          org.apache.maven.model.Profile profile = new org.apache.maven.model.Profile();
57  
58          profile.setId( settingsProfile.getId() );
59  
60          profile.setSource( "settings.xml" );
61  
62          Activation settingsActivation = settingsProfile.getActivation();
63  
64          if ( settingsActivation != null )
65          {
66              org.apache.maven.model.Activation activation = new org.apache.maven.model.Activation();
67  
68              activation.setActiveByDefault( settingsActivation.isActiveByDefault() );
69  
70              activation.setJdk( settingsActivation.getJdk() );
71  
72              ActivationProperty settingsProp = settingsActivation.getProperty();
73  
74              if ( settingsProp != null )
75              {
76                  org.apache.maven.model.ActivationProperty prop = new org.apache.maven.model.ActivationProperty();
77  
78                  prop.setName( settingsProp.getName() );
79                  prop.setValue( settingsProp.getValue() );
80  
81                  activation.setProperty( prop );
82              }
83  
84              ActivationOS settingsOs = settingsActivation.getOs();
85  
86              if ( settingsOs != null )
87              {
88                  org.apache.maven.model.ActivationOS os = new org.apache.maven.model.ActivationOS();
89  
90                  os.setArch( settingsOs.getArch() );
91                  os.setFamily( settingsOs.getFamily() );
92                  os.setName( settingsOs.getName() );
93                  os.setVersion( settingsOs.getVersion() );
94  
95                  activation.setOs( os );
96              }
97  
98              org.apache.maven.settings.ActivationFile settingsFile = settingsActivation.getFile();
99  
100             if ( settingsFile != null )
101             {
102                 ActivationFile file = new ActivationFile();
103 
104                 file.setExists( settingsFile.getExists() );
105                 file.setMissing( settingsFile.getMissing() );
106 
107                 activation.setFile( file );
108             }
109 
110             profile.setActivation( activation );
111         }
112 
113         profile.setProperties( settingsProfile.getProperties() );
114 
115         List<Repository> repos = settingsProfile.getRepositories();
116         if ( repos != null )
117         {
118             for ( Repository repo : repos )
119             {
120                 profile.addRepository( convertFromSettingsRepository( repo ) );
121             }
122         }
123 
124         List<Repository> pluginRepos = settingsProfile.getPluginRepositories();
125         if ( pluginRepos != null )
126         {
127             for ( Repository pluginRepo : pluginRepos )
128             {
129                 profile.addPluginRepository( convertFromSettingsRepository( pluginRepo ) );
130             }
131         }
132 
133         return profile;
134     }
135 
136     /**
137      * @param settingsRepo
138      * @return a repository
139      */
140     private static org.apache.maven.model.Repository convertFromSettingsRepository( Repository settingsRepo )
141     {
142         org.apache.maven.model.Repository repo = new org.apache.maven.model.Repository();
143 
144         repo.setId( settingsRepo.getId() );
145         repo.setLayout( settingsRepo.getLayout() );
146         repo.setName( settingsRepo.getName() );
147         repo.setUrl( settingsRepo.getUrl() );
148 
149         if ( settingsRepo.getSnapshots() != null )
150         {
151             repo.setSnapshots( convertRepositoryPolicy( settingsRepo.getSnapshots() ) );
152         }
153         if ( settingsRepo.getReleases() != null )
154         {
155             repo.setReleases( convertRepositoryPolicy( settingsRepo.getReleases() ) );
156         }
157 
158         return repo;
159     }
160 
161     /**
162      * @param settingsPolicy
163      * @return a RepositoryPolicy
164      */
165     private static org.apache.maven.model.RepositoryPolicy convertRepositoryPolicy( RepositoryPolicy settingsPolicy )
166     {
167         org.apache.maven.model.RepositoryPolicy policy = new org.apache.maven.model.RepositoryPolicy();
168         policy.setEnabled( settingsPolicy.isEnabled() );
169         policy.setUpdatePolicy( settingsPolicy.getUpdatePolicy() );
170         policy.setChecksumPolicy( settingsPolicy.getChecksumPolicy() );
171         return policy;
172     }
173 
174     /**
175      * @param settings could be null
176      * @return a new instance of settings or null if settings was null.
177      */
178     public static Settings copySettings( Settings settings )
179     {
180         if ( settings == null )
181         {
182             return null;
183         }
184 
185         Settings clone = new Settings();
186         clone.setActiveProfiles( settings.getActiveProfiles() );
187         clone.setInteractiveMode( settings.isInteractiveMode() );
188         clone.setLocalRepository( settings.getLocalRepository() );
189         clone.setMirrors( settings.getMirrors() );
190         clone.setModelEncoding( settings.getModelEncoding() );
191         clone.setOffline( settings.isOffline() );
192         clone.setPluginGroups( settings.getPluginGroups() );
193         clone.setProfiles( settings.getProfiles() );
194         clone.setProxies( settings.getProxies() );
195         clone.setServers( settings.getServers() );
196         clone.setSourceLevel( settings.getSourceLevel() );
197         clone.setUsePluginRegistry( settings.isUsePluginRegistry() );
198 
199         return clone;
200     }
201 }