1 package org.apache.maven.settings.merge;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
38
39 public class MavenSettingsMerger
40 {
41
42
43
44
45
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
92
93
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
114
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 }