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