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