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