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.internal.impl;
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.Map;
44  import java.util.stream.Collectors;
45  
46  import org.apache.maven.api.model.ActivationFile;
47  import org.apache.maven.api.model.InputLocation;
48  import org.apache.maven.api.settings.Activation;
49  import org.apache.maven.api.settings.ActivationOS;
50  import org.apache.maven.api.settings.ActivationProperty;
51  import org.apache.maven.api.settings.Profile;
52  import org.apache.maven.api.settings.Repository;
53  import org.apache.maven.api.settings.RepositoryPolicy;
54  import org.apache.maven.api.settings.Settings;
55  import org.apache.maven.settings.v4.SettingsMerger;
56  
57  /**
58   * Several convenience methods to handle settings
59   *
60   */
61  public final class SettingsUtilsV4 {
62  
63      private SettingsUtilsV4() {
64          // don't allow construction.
65      }
66  
67      /**
68       * @param dominant
69       * @param recessive
70       */
71      public static Settings merge(Settings dominant, Settings recessive) {
72          return new SettingsMerger().merge(dominant, recessive, true, Collections.emptyMap());
73      }
74  
75      /**
76       * @param modelProfile
77       * @return a profile
78       */
79      public static Profile convertToSettingsProfile(org.apache.maven.api.model.Profile modelProfile) {
80          Profile.Builder profile = Profile.newBuilder();
81  
82          profile.id(modelProfile.getId());
83  
84          org.apache.maven.api.model.Activation modelActivation = modelProfile.getActivation();
85  
86          if (modelActivation != null) {
87              Activation.Builder activation = Activation.newBuilder();
88  
89              activation.activeByDefault(modelActivation.isActiveByDefault());
90  
91              activation.jdk(modelActivation.getJdk());
92  
93              org.apache.maven.api.model.ActivationProperty modelProp = modelActivation.getProperty();
94  
95              if (modelProp != null) {
96                  ActivationProperty prop = ActivationProperty.newBuilder()
97                          .name(modelProp.getName())
98                          .value(modelProp.getValue())
99                          .build();
100                 activation.property(prop);
101             }
102 
103             org.apache.maven.api.model.ActivationOS modelOs = modelActivation.getOs();
104 
105             if (modelOs != null) {
106                 ActivationOS os = ActivationOS.newBuilder()
107                         .arch(modelOs.getArch())
108                         .family(modelOs.getFamily())
109                         .name(modelOs.getName())
110                         .version(modelOs.getVersion())
111                         .build();
112 
113                 activation.os(os);
114             }
115 
116             org.apache.maven.api.model.ActivationFile modelFile = modelActivation.getFile();
117 
118             if (modelFile != null) {
119                 org.apache.maven.api.settings.ActivationFile file =
120                         org.apache.maven.api.settings.ActivationFile.newBuilder()
121                                 .exists(modelFile.getExists())
122                                 .missing(modelFile.getMissing())
123                                 .build();
124 
125                 activation.file(file);
126             }
127 
128             activation.packaging(modelActivation.getPackaging());
129 
130             profile.activation(activation.build());
131         }
132 
133         profile.properties(modelProfile.getProperties().entrySet().stream()
134                 .collect(Collectors.toMap(
135                         e -> e.getKey().toString(), e -> e.getValue().toString())));
136 
137         List<org.apache.maven.api.model.Repository> repos = modelProfile.getRepositories();
138         if (repos != null) {
139             List<Repository> repositories = new ArrayList<>();
140             for (org.apache.maven.api.model.Repository repo : repos) {
141                 repositories.add(convertToSettingsRepository(repo));
142             }
143             profile.repositories(repositories);
144         }
145 
146         List<org.apache.maven.api.model.Repository> pluginRepos = modelProfile.getPluginRepositories();
147         if (pluginRepos != null) {
148             List<Repository> repositories = new ArrayList<>();
149             for (org.apache.maven.api.model.Repository pluginRepo : pluginRepos) {
150                 repositories.add(convertToSettingsRepository(pluginRepo));
151             }
152             profile.pluginRepositories(repositories);
153         }
154 
155         return profile.build();
156     }
157 
158     /**
159      * @param settingsProfile
160      * @return a profile
161      */
162     public static org.apache.maven.api.model.Profile convertFromSettingsProfile(Profile settingsProfile) {
163         org.apache.maven.api.model.Profile.Builder profile = org.apache.maven.api.model.Profile.newBuilder();
164 
165         profile.id(settingsProfile.getId());
166 
167         Activation settingsActivation = settingsProfile.getActivation();
168 
169         if (settingsActivation != null) {
170             org.apache.maven.api.model.Activation.Builder activation =
171                     org.apache.maven.api.model.Activation.newBuilder();
172 
173             activation.activeByDefault(settingsActivation.isActiveByDefault());
174             activation.location("activeByDefault", toLocation(settingsActivation.getLocation("activeByDefault")));
175 
176             activation.jdk(settingsActivation.getJdk());
177             activation.location("jdk", toLocation(settingsActivation.getLocation("jdk")));
178 
179             ActivationProperty settingsProp = settingsActivation.getProperty();
180             if (settingsProp != null) {
181                 activation.property(org.apache.maven.api.model.ActivationProperty.newBuilder()
182                         .name(settingsProp.getName())
183                         .value(settingsProp.getValue())
184                         .location("name", toLocation(settingsProp.getLocation("name")))
185                         .location("value", toLocation(settingsProp.getLocation("value")))
186                         .build());
187             }
188 
189             ActivationOS settingsOs = settingsActivation.getOs();
190             if (settingsOs != null) {
191                 activation.os(org.apache.maven.api.model.ActivationOS.newBuilder()
192                         .arch(settingsOs.getArch())
193                         .family(settingsOs.getFamily())
194                         .name(settingsOs.getName())
195                         .version(settingsOs.getVersion())
196                         .location("arch", toLocation(settingsOs.getLocation("arch")))
197                         .location("family", toLocation(settingsOs.getLocation("family")))
198                         .location("name", toLocation(settingsOs.getLocation("name")))
199                         .location("version", toLocation(settingsOs.getLocation("version")))
200                         .build());
201             }
202 
203             org.apache.maven.api.settings.ActivationFile settingsFile = settingsActivation.getFile();
204             if (settingsFile != null) {
205                 activation.file(ActivationFile.newBuilder()
206                         .exists(settingsFile.getExists())
207                         .missing(settingsFile.getMissing())
208                         .location("exists", toLocation(settingsFile.getLocation("exists")))
209                         .location("missing", toLocation(settingsFile.getLocation("missing")))
210                         .build());
211             }
212 
213             activation.packaging(settingsActivation.getPackaging());
214 
215             profile.activation(activation.build());
216         }
217 
218         profile.properties(settingsProfile.getProperties());
219         profile.location("properties", toLocation(settingsProfile.getLocation("properties")));
220 
221         List<Repository> repos = settingsProfile.getRepositories();
222         if (repos != null) {
223             profile.repositories(repos.stream()
224                     .map(SettingsUtilsV4::convertFromSettingsRepository)
225                     .collect(Collectors.toList()));
226         }
227 
228         List<Repository> pluginRepos = settingsProfile.getPluginRepositories();
229         if (pluginRepos != null) {
230             profile.pluginRepositories(pluginRepos.stream()
231                     .map(SettingsUtilsV4::convertFromSettingsRepository)
232                     .collect(Collectors.toList()));
233         }
234 
235         org.apache.maven.api.model.Profile value = profile.build();
236         value.setSource("settings.xml");
237         return value;
238     }
239 
240     /**
241      * @param settingsRepo
242      * @return a repository
243      */
244     private static org.apache.maven.api.model.Repository convertFromSettingsRepository(Repository settingsRepo) {
245         org.apache.maven.api.model.Repository.Builder repo = org.apache.maven.api.model.Repository.newBuilder();
246 
247         repo.id(settingsRepo.getId());
248         repo.layout(settingsRepo.getLayout());
249         repo.name(settingsRepo.getName());
250         repo.url(settingsRepo.getUrl());
251 
252         repo.location("id", toLocation(settingsRepo.getLocation("id")));
253         repo.location("layout", toLocation(settingsRepo.getLocation("layout")));
254         repo.location("name", toLocation(settingsRepo.getLocation("name")));
255         repo.location("url", toLocation(settingsRepo.getLocation("url")));
256 
257         if (settingsRepo.getSnapshots() != null) {
258             repo.snapshots(convertRepositoryPolicy(settingsRepo.getSnapshots()));
259         }
260         if (settingsRepo.getReleases() != null) {
261             repo.releases(convertRepositoryPolicy(settingsRepo.getReleases()));
262         }
263 
264         return repo.build();
265     }
266 
267     /**
268      * @param settingsPolicy
269      * @return a RepositoryPolicy
270      */
271     private static org.apache.maven.api.model.RepositoryPolicy convertRepositoryPolicy(
272             RepositoryPolicy settingsPolicy) {
273         org.apache.maven.api.model.RepositoryPolicy policy = org.apache.maven.api.model.RepositoryPolicy.newBuilder()
274                 .enabled(Boolean.toString(settingsPolicy.isEnabled()))
275                 .updatePolicy(settingsPolicy.getUpdatePolicy())
276                 .checksumPolicy(settingsPolicy.getChecksumPolicy())
277                 .location("enabled", toLocation(settingsPolicy.getLocation("enabled")))
278                 .location("updatePolicy", toLocation(settingsPolicy.getLocation("updatePolicy")))
279                 .location("checksumPolicy", toLocation(settingsPolicy.getLocation("checksumPolicy")))
280                 .build();
281         return policy;
282     }
283 
284     /**
285      * @param modelRepo
286      * @return a repository
287      */
288     private static Repository convertToSettingsRepository(org.apache.maven.api.model.Repository modelRepo) {
289         Repository repo = Repository.newBuilder()
290                 .id(modelRepo.getId())
291                 .layout(modelRepo.getLayout())
292                 .name(modelRepo.getName())
293                 .url(modelRepo.getUrl())
294                 .snapshots(modelRepo.getSnapshots() != null ? convertRepositoryPolicy(modelRepo.getSnapshots()) : null)
295                 .releases(modelRepo.getReleases() != null ? convertRepositoryPolicy(modelRepo.getReleases()) : null)
296                 .build();
297 
298         return repo;
299     }
300 
301     /**
302      * @param modelPolicy
303      * @return a RepositoryPolicy
304      */
305     private static RepositoryPolicy convertRepositoryPolicy(org.apache.maven.api.model.RepositoryPolicy modelPolicy) {
306         RepositoryPolicy policy = RepositoryPolicy.newBuilder()
307                 .enabled(modelPolicy.isEnabled())
308                 .updatePolicy(modelPolicy.getUpdatePolicy())
309                 .checksumPolicy(modelPolicy.getChecksumPolicy())
310                 .build();
311         return policy;
312     }
313 
314     private static org.apache.maven.api.model.InputLocation toLocation(
315             org.apache.maven.api.settings.InputLocation location) {
316         if (location != null) {
317             org.apache.maven.api.settings.InputSource source = location.getSource();
318             Map<Object, InputLocation> locs = location.getLocations().entrySet().stream()
319                     .collect(Collectors.toMap(Map.Entry::getKey, e -> toLocation(e.getValue())));
320             return new org.apache.maven.api.model.InputLocation(
321                     location.getLineNumber(),
322                     location.getColumnNumber(),
323                     source != null ? new org.apache.maven.api.model.InputSource("", source.getLocation()) : null,
324                     locs);
325         } else {
326             return null;
327         }
328     }
329 }