1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
35
36
37 public class MavenSettingsMerger {
38
39
40
41
42
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
86
87
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
105
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 }