View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.settings;
20  
21  import java.util.List;
22  
23  import org.apache.maven.model.ActivationFile;
24  import org.apache.maven.settings.merge.MavenSettingsMerger;
25  
26  /**
27   * Several convenience methods to handle settings
28   *
29   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
30   */
31  public final class SettingsUtils {
32  
33      private SettingsUtils() {
34          // don't allow construction.
35      }
36  
37      /**
38       * @param dominant
39       * @param recessive
40       * @param recessiveSourceLevel
41       */
42      public static void merge(Settings dominant, Settings recessive, String recessiveSourceLevel) {
43          new MavenSettingsMerger().merge(dominant, recessive, recessiveSourceLevel);
44      }
45  
46      /**
47       * @param modelProfile
48       * @return a profile
49       */
50      public static Profile convertToSettingsProfile(org.apache.maven.model.Profile modelProfile) {
51          Profile profile = new Profile();
52  
53          profile.setId(modelProfile.getId());
54  
55          org.apache.maven.model.Activation modelActivation = modelProfile.getActivation();
56  
57          if (modelActivation != null) {
58              Activation activation = new Activation();
59  
60              activation.setActiveByDefault(modelActivation.isActiveByDefault());
61  
62              activation.setJdk(modelActivation.getJdk());
63  
64              org.apache.maven.model.ActivationProperty modelProp = modelActivation.getProperty();
65  
66              if (modelProp != null) {
67                  ActivationProperty prop = new ActivationProperty();
68                  prop.setName(modelProp.getName());
69                  prop.setValue(modelProp.getValue());
70                  activation.setProperty(prop);
71              }
72  
73              org.apache.maven.model.ActivationOS modelOs = modelActivation.getOs();
74  
75              if (modelOs != null) {
76                  ActivationOS os = new ActivationOS();
77  
78                  os.setArch(modelOs.getArch());
79                  os.setFamily(modelOs.getFamily());
80                  os.setName(modelOs.getName());
81                  os.setVersion(modelOs.getVersion());
82  
83                  activation.setOs(os);
84              }
85  
86              ActivationFile modelFile = modelActivation.getFile();
87  
88              if (modelFile != null) {
89                  org.apache.maven.settings.ActivationFile file = new org.apache.maven.settings.ActivationFile();
90  
91                  file.setExists(modelFile.getExists());
92                  file.setMissing(modelFile.getMissing());
93  
94                  activation.setFile(file);
95              }
96  
97              profile.setActivation(activation);
98          }
99  
100         profile.setProperties(modelProfile.getProperties());
101 
102         List<org.apache.maven.model.Repository> repos = modelProfile.getRepositories();
103         if (repos != null) {
104             for (org.apache.maven.model.Repository repo : repos) {
105                 profile.addRepository(convertToSettingsRepository(repo));
106             }
107         }
108 
109         List<org.apache.maven.model.Repository> pluginRepos = modelProfile.getPluginRepositories();
110         if (pluginRepos != null) {
111             for (org.apache.maven.model.Repository pluginRepo : pluginRepos) {
112                 profile.addPluginRepository(convertToSettingsRepository(pluginRepo));
113             }
114         }
115 
116         return profile;
117     }
118 
119     /**
120      * @param settingsProfile
121      * @return a profile
122      */
123     public static org.apache.maven.model.Profile convertFromSettingsProfile(Profile settingsProfile) {
124         org.apache.maven.model.Profile profile = new org.apache.maven.model.Profile();
125 
126         profile.setId(settingsProfile.getId());
127 
128         profile.setSource("settings.xml");
129 
130         Activation settingsActivation = settingsProfile.getActivation();
131 
132         if (settingsActivation != null) {
133             org.apache.maven.model.Activation activation = new org.apache.maven.model.Activation();
134 
135             activation.setActiveByDefault(settingsActivation.isActiveByDefault());
136 
137             activation.setJdk(settingsActivation.getJdk());
138 
139             ActivationProperty settingsProp = settingsActivation.getProperty();
140 
141             if (settingsProp != null) {
142                 org.apache.maven.model.ActivationProperty prop = new org.apache.maven.model.ActivationProperty();
143 
144                 prop.setName(settingsProp.getName());
145                 prop.setValue(settingsProp.getValue());
146 
147                 activation.setProperty(prop);
148             }
149 
150             ActivationOS settingsOs = settingsActivation.getOs();
151 
152             if (settingsOs != null) {
153                 org.apache.maven.model.ActivationOS os = new org.apache.maven.model.ActivationOS();
154 
155                 os.setArch(settingsOs.getArch());
156                 os.setFamily(settingsOs.getFamily());
157                 os.setName(settingsOs.getName());
158                 os.setVersion(settingsOs.getVersion());
159 
160                 activation.setOs(os);
161             }
162 
163             org.apache.maven.settings.ActivationFile settingsFile = settingsActivation.getFile();
164 
165             if (settingsFile != null) {
166                 ActivationFile file = new ActivationFile();
167 
168                 file.setExists(settingsFile.getExists());
169                 file.setMissing(settingsFile.getMissing());
170 
171                 activation.setFile(file);
172             }
173 
174             profile.setActivation(activation);
175         }
176 
177         profile.setProperties(settingsProfile.getProperties());
178 
179         List<Repository> repos = settingsProfile.getRepositories();
180         if (repos != null) {
181             for (Repository repo : repos) {
182                 profile.addRepository(convertFromSettingsRepository(repo));
183             }
184         }
185 
186         List<Repository> pluginRepos = settingsProfile.getPluginRepositories();
187         if (pluginRepos != null) {
188             for (Repository pluginRepo : pluginRepos) {
189                 profile.addPluginRepository(convertFromSettingsRepository(pluginRepo));
190             }
191         }
192 
193         return profile;
194     }
195 
196     /**
197      * @param settingsRepo
198      * @return a repository
199      */
200     private static org.apache.maven.model.Repository convertFromSettingsRepository(Repository settingsRepo) {
201         org.apache.maven.model.Repository repo = new org.apache.maven.model.Repository();
202 
203         repo.setId(settingsRepo.getId());
204         repo.setLayout(settingsRepo.getLayout());
205         repo.setName(settingsRepo.getName());
206         repo.setUrl(settingsRepo.getUrl());
207 
208         if (settingsRepo.getSnapshots() != null) {
209             repo.setSnapshots(convertRepositoryPolicy(settingsRepo.getSnapshots()));
210         }
211         if (settingsRepo.getReleases() != null) {
212             repo.setReleases(convertRepositoryPolicy(settingsRepo.getReleases()));
213         }
214 
215         return repo;
216     }
217 
218     /**
219      * @param settingsPolicy
220      * @return a RepositoryPolicy
221      */
222     private static org.apache.maven.model.RepositoryPolicy convertRepositoryPolicy(RepositoryPolicy settingsPolicy) {
223         org.apache.maven.model.RepositoryPolicy policy = new org.apache.maven.model.RepositoryPolicy();
224         policy.setEnabled(settingsPolicy.isEnabled());
225         policy.setUpdatePolicy(settingsPolicy.getUpdatePolicy());
226         policy.setChecksumPolicy(settingsPolicy.getChecksumPolicy());
227         return policy;
228     }
229 
230     /**
231      * @param modelRepo
232      * @return a repository
233      */
234     private static Repository convertToSettingsRepository(org.apache.maven.model.Repository modelRepo) {
235         Repository repo = new Repository();
236 
237         repo.setId(modelRepo.getId());
238         repo.setLayout(modelRepo.getLayout());
239         repo.setName(modelRepo.getName());
240         repo.setUrl(modelRepo.getUrl());
241 
242         if (modelRepo.getSnapshots() != null) {
243             repo.setSnapshots(convertRepositoryPolicy(modelRepo.getSnapshots()));
244         }
245         if (modelRepo.getReleases() != null) {
246             repo.setReleases(convertRepositoryPolicy(modelRepo.getReleases()));
247         }
248 
249         return repo;
250     }
251 
252     /**
253      * @param modelPolicy
254      * @return a RepositoryPolicy
255      */
256     private static RepositoryPolicy convertRepositoryPolicy(org.apache.maven.model.RepositoryPolicy modelPolicy) {
257         RepositoryPolicy policy = new RepositoryPolicy();
258         policy.setEnabled(modelPolicy.isEnabled());
259         policy.setUpdatePolicy(modelPolicy.getUpdatePolicy());
260         policy.setChecksumPolicy(modelPolicy.getChecksumPolicy());
261         return policy;
262     }
263 
264     /**
265      * @param settings could be null
266      * @return a new instance of settings or null if settings was null.
267      */
268     public static Settings copySettings(Settings settings) {
269         if (settings == null) {
270             return null;
271         }
272 
273         Settings clone = new Settings();
274         clone.setActiveProfiles(settings.getActiveProfiles());
275         clone.setInteractiveMode(settings.isInteractiveMode());
276         clone.setLocalRepository(settings.getLocalRepository());
277         clone.setMirrors(settings.getMirrors());
278         clone.setModelEncoding(settings.getModelEncoding());
279         clone.setOffline(settings.isOffline());
280         clone.setPluginGroups(settings.getPluginGroups());
281         clone.setProfiles(settings.getProfiles());
282         clone.setProxies(settings.getProxies());
283         clone.setServers(settings.getServers());
284         clone.setSourceLevel(settings.getSourceLevel());
285         clone.setUsePluginRegistry(settings.isUsePluginRegistry());
286 
287         return clone;
288     }
289 }