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.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.maven.settings.IdentifiableBase;
27 import org.apache.maven.settings.Settings;
28 import org.codehaus.plexus.util.StringUtils;
29
30
31
32
33
34 public class MavenSettingsMerger {
35
36
37
38
39
40
41 public void merge(Settings dominant, Settings recessive, String recessiveSourceLevel) {
42 if (dominant == null || recessive == null) {
43 return;
44 }
45
46 recessive.setSourceLevel(recessiveSourceLevel);
47
48 List<String> dominantActiveProfiles = dominant.getActiveProfiles();
49 List<String> recessiveActiveProfiles = recessive.getActiveProfiles();
50
51 if (recessiveActiveProfiles != null) {
52 if (dominantActiveProfiles == null) {
53 dominantActiveProfiles = new ArrayList<>();
54 dominant.setActiveProfiles(dominantActiveProfiles);
55 }
56
57 for (String profileId : recessiveActiveProfiles) {
58 if (!dominantActiveProfiles.contains(profileId)) {
59 dominantActiveProfiles.add(profileId);
60 }
61 }
62 }
63
64 List<String> dominantPluginGroupIds = dominant.getPluginGroups();
65
66 List<String> recessivePluginGroupIds = recessive.getPluginGroups();
67
68 if (recessivePluginGroupIds != null) {
69 if (dominantPluginGroupIds == null) {
70 dominantPluginGroupIds = new ArrayList<>();
71 dominant.setPluginGroups(dominantPluginGroupIds);
72 }
73
74 for (String pluginGroupId : recessivePluginGroupIds) {
75 if (!dominantPluginGroupIds.contains(pluginGroupId)) {
76 dominantPluginGroupIds.add(pluginGroupId);
77 }
78 }
79 }
80
81 if (StringUtils.isEmpty(dominant.getLocalRepository())) {
82 dominant.setLocalRepository(recessive.getLocalRepository());
83 }
84
85 shallowMergeById(dominant.getMirrors(), recessive.getMirrors(), recessiveSourceLevel);
86 shallowMergeById(dominant.getServers(), recessive.getServers(), recessiveSourceLevel);
87 shallowMergeById(dominant.getProxies(), recessive.getProxies(), recessiveSourceLevel);
88 shallowMergeById(dominant.getProfiles(), recessive.getProfiles(), recessiveSourceLevel);
89 }
90
91
92
93
94
95
96 private static <T extends IdentifiableBase> void shallowMergeById(
97 List<T> dominant, List<T> recessive, String recessiveSourceLevel) {
98 Map<String, T> dominantById = mapById(dominant);
99
100 for (T identifiable : recessive) {
101 if (!dominantById.containsKey(identifiable.getId())) {
102 identifiable.setSourceLevel(recessiveSourceLevel);
103
104 dominant.add(identifiable);
105 }
106 }
107 }
108
109
110
111
112
113 private static <T extends IdentifiableBase> Map<String, T> mapById(List<T> identifiables) {
114 Map<String, T> byId = new HashMap<>();
115
116 for (T identifiable : identifiables) {
117 byId.put(identifiable.getId(), identifiable);
118 }
119
120 return byId;
121 }
122 }