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  /*
22   * Licensed to the Apache Software Foundation (ASF) under one
23   * or more contributor license agreements.  See the NOTICE file
24   * distributed with this work for additional information
25   * regarding copyright ownership.  The ASF licenses this file
26   * to you under the Apache License, Version 2.0 (the
27   * "License"); you may not use this file except in compliance
28   * with the License.  You may obtain a copy of the License at
29   *
30   *  http://www.apache.org/licenses/LICENSE-2.0
31   *
32   * Unless required by applicable law or agreed to in writing,
33   * software distributed under the License is distributed on an
34   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
35   * KIND, either express or implied.  See the License for the
36   * specific language governing permissions and limitations
37   * under the License.
38   */
39  
40  import java.util.ArrayList;
41  import java.util.Collections;
42  import java.util.List;
43  import java.util.stream.Collectors;
44  
45  import org.apache.maven.api.model.ActivationFile;
46  import org.apache.maven.api.settings.Activation;
47  import org.apache.maven.api.settings.ActivationOS;
48  import org.apache.maven.api.settings.ActivationProperty;
49  import org.apache.maven.api.settings.Profile;
50  import org.apache.maven.api.settings.Repository;
51  import org.apache.maven.api.settings.RepositoryPolicy;
52  import org.apache.maven.api.settings.Settings;
53  import org.apache.maven.settings.v4.SettingsMerger;
54  
55  /**
56   * Several convenience methods to handle settings
57   *
58   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
59   */
60  public final class SettingsUtilsV4 {
61  
62      private SettingsUtilsV4() {
63          // don't allow construction.
64      }
65  
66      /**
67       * @param dominant
68       * @param recessive
69       */
70      public static Settings merge(Settings dominant, Settings recessive) {
71          return new SettingsMerger().merge(dominant, recessive, true, Collections.emptyMap());
72      }
73  
74      /**
75       * @param modelProfile
76       * @return a profile
77       */
78      public static Profile convertToSettingsProfile(org.apache.maven.api.model.Profile modelProfile) {
79          Profile.Builder profile = Profile.newBuilder();
80  
81          profile.id(modelProfile.getId());
82  
83          org.apache.maven.api.model.Activation modelActivation = modelProfile.getActivation();
84  
85          if (modelActivation != null) {
86              Activation.Builder activation = Activation.newBuilder();
87  
88              activation.activeByDefault(modelActivation.isActiveByDefault());
89  
90              activation.jdk(modelActivation.getJdk());
91  
92              org.apache.maven.api.model.ActivationProperty modelProp = modelActivation.getProperty();
93  
94              if (modelProp != null) {
95                  ActivationProperty prop = ActivationProperty.newBuilder()
96                          .name(modelProp.getName())
97                          .value(modelProp.getValue())
98                          .build();
99                  activation.property(prop);
100             }
101 
102             org.apache.maven.api.model.ActivationOS modelOs = modelActivation.getOs();
103 
104             if (modelOs != null) {
105                 ActivationOS os = ActivationOS.newBuilder()
106                         .arch(modelOs.getArch())
107                         .family(modelOs.getFamily())
108                         .name(modelOs.getName())
109                         .version(modelOs.getVersion())
110                         .build();
111 
112                 activation.os(os);
113             }
114 
115             org.apache.maven.api.model.ActivationFile modelFile = modelActivation.getFile();
116 
117             if (modelFile != null) {
118                 org.apache.maven.api.settings.ActivationFile file =
119                         org.apache.maven.api.settings.ActivationFile.newBuilder()
120                                 .exists(modelFile.getExists())
121                                 .missing(modelFile.getMissing())
122                                 .build();
123 
124                 activation.file(file);
125             }
126 
127             profile.activation(activation.build());
128         }
129 
130         profile.properties(modelProfile.getProperties().entrySet().stream()
131                 .collect(Collectors.toMap(
132                         e -> e.getKey().toString(), e -> e.getValue().toString())));
133 
134         List<org.apache.maven.api.model.Repository> repos = modelProfile.getRepositories();
135         if (repos != null) {
136             List<Repository> repositories = new ArrayList<>();
137             for (org.apache.maven.api.model.Repository repo : repos) {
138                 repositories.add(convertToSettingsRepository(repo));
139             }
140             profile.repositories(repositories);
141         }
142 
143         List<org.apache.maven.api.model.Repository> pluginRepos = modelProfile.getPluginRepositories();
144         if (pluginRepos != null) {
145             List<Repository> repositories = new ArrayList<>();
146             for (org.apache.maven.api.model.Repository pluginRepo : pluginRepos) {
147                 repositories.add(convertToSettingsRepository(pluginRepo));
148             }
149             profile.pluginRepositories(repositories);
150         }
151 
152         return profile.build();
153     }
154 
155     /**
156      * @param settingsProfile
157      * @return a profile
158      */
159     public static org.apache.maven.api.model.Profile convertFromSettingsProfile(Profile settingsProfile) {
160         org.apache.maven.api.model.Profile.Builder profile = org.apache.maven.api.model.Profile.newBuilder();
161 
162         profile.id(settingsProfile.getId());
163 
164         Activation settingsActivation = settingsProfile.getActivation();
165 
166         if (settingsActivation != null) {
167             org.apache.maven.api.model.Activation.Builder activation =
168                     org.apache.maven.api.model.Activation.newBuilder();
169 
170             activation.activeByDefault(settingsActivation.isActiveByDefault());
171 
172             activation.jdk(settingsActivation.getJdk());
173 
174             ActivationProperty settingsProp = settingsActivation.getProperty();
175             if (settingsProp != null) {
176                 activation.property(org.apache.maven.api.model.ActivationProperty.newBuilder()
177                         .name(settingsProp.getName())
178                         .value(settingsProp.getValue())
179                         .build());
180             }
181 
182             ActivationOS settingsOs = settingsActivation.getOs();
183             if (settingsOs != null) {
184                 activation.os(org.apache.maven.api.model.ActivationOS.newBuilder()
185                         .arch(settingsOs.getArch())
186                         .family(settingsOs.getFamily())
187                         .name(settingsOs.getName())
188                         .version(settingsOs.getVersion())
189                         .build());
190             }
191 
192             org.apache.maven.api.settings.ActivationFile settingsFile = settingsActivation.getFile();
193             if (settingsFile != null) {
194                 activation.file(ActivationFile.newBuilder()
195                         .exists(settingsFile.getExists())
196                         .missing(settingsFile.getMissing())
197                         .build());
198             }
199 
200             profile.activation(activation.build());
201         }
202 
203         profile.properties(settingsProfile.getProperties());
204 
205         List<Repository> repos = settingsProfile.getRepositories();
206         if (repos != null) {
207             profile.repositories(repos.stream()
208                     .map(SettingsUtilsV4::convertFromSettingsRepository)
209                     .collect(Collectors.toList()));
210         }
211 
212         List<Repository> pluginRepos = settingsProfile.getPluginRepositories();
213         if (pluginRepos != null) {
214             profile.pluginRepositories(pluginRepos.stream()
215                     .map(SettingsUtilsV4::convertFromSettingsRepository)
216                     .collect(Collectors.toList()));
217         }
218 
219         org.apache.maven.api.model.Profile value = profile.build();
220         value.setSource("settings.xml");
221         return value;
222     }
223 
224     /**
225      * @param settingsRepo
226      * @return a repository
227      */
228     private static org.apache.maven.api.model.Repository convertFromSettingsRepository(Repository settingsRepo) {
229         org.apache.maven.api.model.Repository.Builder repo = org.apache.maven.api.model.Repository.newBuilder();
230 
231         repo.id(settingsRepo.getId());
232         repo.layout(settingsRepo.getLayout());
233         repo.name(settingsRepo.getName());
234         repo.url(settingsRepo.getUrl());
235 
236         if (settingsRepo.getSnapshots() != null) {
237             repo.snapshots(convertRepositoryPolicy(settingsRepo.getSnapshots()));
238         }
239         if (settingsRepo.getReleases() != null) {
240             repo.releases(convertRepositoryPolicy(settingsRepo.getReleases()));
241         }
242 
243         return repo.build();
244     }
245 
246     /**
247      * @param settingsPolicy
248      * @return a RepositoryPolicy
249      */
250     private static org.apache.maven.api.model.RepositoryPolicy convertRepositoryPolicy(
251             RepositoryPolicy settingsPolicy) {
252         org.apache.maven.api.model.RepositoryPolicy policy = org.apache.maven.api.model.RepositoryPolicy.newBuilder()
253                 .enabled(Boolean.toString(settingsPolicy.isEnabled()))
254                 .updatePolicy(settingsPolicy.getUpdatePolicy())
255                 .checksumPolicy(settingsPolicy.getChecksumPolicy())
256                 .build();
257         return policy;
258     }
259 
260     /**
261      * @param modelRepo
262      * @return a repository
263      */
264     private static Repository convertToSettingsRepository(org.apache.maven.api.model.Repository modelRepo) {
265         Repository repo = Repository.newBuilder()
266                 .id(modelRepo.getId())
267                 .layout(modelRepo.getLayout())
268                 .name(modelRepo.getName())
269                 .url(modelRepo.getUrl())
270                 .snapshots(modelRepo.getSnapshots() != null ? convertRepositoryPolicy(modelRepo.getSnapshots()) : null)
271                 .releases(modelRepo.getReleases() != null ? convertRepositoryPolicy(modelRepo.getReleases()) : null)
272                 .build();
273 
274         return repo;
275     }
276 
277     /**
278      * @param modelPolicy
279      * @return a RepositoryPolicy
280      */
281     private static RepositoryPolicy convertRepositoryPolicy(org.apache.maven.api.model.RepositoryPolicy modelPolicy) {
282         RepositoryPolicy policy = RepositoryPolicy.newBuilder()
283                 .enabled(modelPolicy.isEnabled())
284                 .updatePolicy(modelPolicy.getUpdatePolicy())
285                 .checksumPolicy(modelPolicy.getChecksumPolicy())
286                 .build();
287         return policy;
288     }
289 
290     /**
291      * @param settings could be null
292      * @return a new instance of settings or null if settings was null.
293      */
294     public static org.apache.maven.settings.Settings copySettings(org.apache.maven.settings.Settings settings) {
295         if (settings == null) {
296             return null;
297         }
298         return new org.apache.maven.settings.Settings(settings.getDelegate());
299     }
300 }