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.merge;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  import java.util.stream.Collectors;
28  import java.util.stream.Stream;
29  import org.apache.maven.api.settings.IdentifiableBase;
30  import org.apache.maven.api.settings.Settings;
31  import org.codehaus.plexus.util.StringUtils;
32  
33  /**
34   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
35   * @author Benjamin Bentmann
36   */
37  public class MavenSettingsMerger {
38  
39      /**
40       * @param dominant
41       * @param recessive
42       * @param recessiveSourceLevel
43       */
44      public Settings merge(Settings dominant, Settings recessive, String recessiveSourceLevel) {
45          if (dominant == null) {
46              return recessive;
47          } else if (recessive == null) {
48              return dominant;
49          }
50  
51          recessive.setSourceLevel(recessiveSourceLevel);
52  
53          Settings.Builder merged = Settings.newBuilder(dominant);
54  
55          List<String> dominantActiveProfiles = dominant.getActiveProfiles();
56          List<String> recessiveActiveProfiles = recessive.getActiveProfiles();
57          List<String> mergedActiveProfiles = Stream.of(dominantActiveProfiles, recessiveActiveProfiles)
58                  .flatMap(Collection::stream)
59                  .distinct()
60                  .collect(Collectors.toList());
61          merged.activeProfiles(mergedActiveProfiles);
62  
63          List<String> dominantPluginGroupIds = dominant.getPluginGroups();
64          List<String> recessivePluginGroupIds = recessive.getPluginGroups();
65          List<String> mergedPluginGroupIds = Stream.of(dominantPluginGroupIds, recessivePluginGroupIds)
66                  .flatMap(Collection::stream)
67                  .distinct()
68                  .collect(Collectors.toList());
69          merged.pluginGroups(mergedPluginGroupIds);
70  
71          String localRepository = StringUtils.isEmpty(dominant.getLocalRepository())
72                  ? recessive.getLocalRepository()
73                  : dominant.getLocalRepository();
74          merged.localRepository(localRepository);
75  
76          merged.mirrors(shallowMergeById(dominant.getMirrors(), recessive.getMirrors(), recessiveSourceLevel));
77          merged.servers(shallowMergeById(dominant.getServers(), recessive.getServers(), recessiveSourceLevel));
78          merged.proxies(shallowMergeById(dominant.getProxies(), recessive.getProxies(), recessiveSourceLevel));
79          merged.profiles(shallowMergeById(dominant.getProfiles(), recessive.getProfiles(), recessiveSourceLevel));
80  
81          return merged.build();
82      }
83  
84      /**
85       * @param dominant
86       * @param recessive
87       * @param recessiveSourceLevel
88       */
89      private static <T extends IdentifiableBase> List<T> shallowMergeById(
90              List<T> dominant, List<T> recessive, String recessiveSourceLevel) {
91          Set<String> dominantIds = dominant.stream().map(IdentifiableBase::getId).collect(Collectors.toSet());
92          final List<T> merged = new ArrayList<>(dominant.size() + recessive.size());
93          merged.addAll(dominant);
94          for (T identifiable : recessive) {
95              if (!dominantIds.contains(identifiable.getId())) {
96                  identifiable.setSourceLevel(recessiveSourceLevel);
97                  merged.add(identifiable);
98              }
99          }
100         return merged;
101     }
102 
103     /**
104      * @param identifiables
105      * @return a map
106      */
107     private static <T extends IdentifiableBase> Map<String, T> mapById(List<T> identifiables) {
108         Map<String, T> byId = new HashMap<>();
109 
110         for (T identifiable : identifiables) {
111             byId.put(identifiable.getId(), identifiable);
112         }
113 
114         return byId;
115     }
116 }