1 package org.apache.maven.settings; 2 3 18 19 import org.apache.maven.model.ActivationFile; 20 import org.codehaus.plexus.util.StringUtils; 21 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 public final class SettingsUtils 29 { 30 private SettingsUtils() 31 { 32 } 34 35 public static void merge( Settings dominant, Settings recessive, String recessiveSourceLevel ) 36 { 37 if ( dominant == null || recessive == null ) 38 { 39 return; 40 } 41 42 recessive.setSourceLevel( recessiveSourceLevel ); 43 44 List dominantActiveProfiles = dominant.getActiveProfiles(); 45 List recessiveActiveProfiles = recessive.getActiveProfiles(); 46 47 if ( recessiveActiveProfiles != null ) 48 { 49 if ( dominantActiveProfiles == null ) 50 { 51 dominantActiveProfiles = new ArrayList (); 52 dominant.setActiveProfiles( dominantActiveProfiles ); 53 } 54 55 for ( Iterator it = recessiveActiveProfiles.iterator(); it.hasNext(); ) 56 { 57 String profileId = (String ) it.next(); 58 59 if ( !dominantActiveProfiles.contains( profileId ) ) 60 { 61 dominantActiveProfiles.add( profileId ); 62 63 dominant.getRuntimeInfo().setActiveProfileSourceLevel( profileId, recessiveSourceLevel ); 64 } 65 } 66 } 67 68 List dominantPluginGroupIds = dominant.getPluginGroups(); 69 List recessivePluginGroupIds = recessive.getPluginGroups(); 70 71 if ( recessivePluginGroupIds != null ) 72 { 73 if ( dominantPluginGroupIds == null ) 74 { 75 dominantPluginGroupIds = new ArrayList (); 76 dominant.setPluginGroups( dominantPluginGroupIds ); 77 } 78 79 for ( Iterator it = recessivePluginGroupIds.iterator(); it.hasNext(); ) 80 { 81 String pluginGroupId = (String ) it.next(); 82 83 if ( !dominantPluginGroupIds.contains( pluginGroupId ) ) 84 { 85 dominantPluginGroupIds.add( pluginGroupId ); 86 87 dominant.getRuntimeInfo().setPluginGroupIdSourceLevel( pluginGroupId, recessiveSourceLevel ); 88 } 89 } 90 } 91 92 if ( StringUtils.isEmpty( dominant.getLocalRepository() ) ) 93 { 94 dominant.setLocalRepository( recessive.getLocalRepository() ); 95 96 dominant.getRuntimeInfo().setLocalRepositorySourceLevel( recessiveSourceLevel ); 97 } 98 99 shallowMergeById( dominant.getMirrors(), recessive.getMirrors(), recessiveSourceLevel ); 100 shallowMergeById( dominant.getServers(), recessive.getServers(), recessiveSourceLevel ); 101 shallowMergeById( dominant.getProxies(), recessive.getProxies(), recessiveSourceLevel ); 102 shallowMergeById( dominant.getProfiles(), recessive.getProfiles(), recessiveSourceLevel ); 103 104 } 105 106 private static void shallowMergeById( List dominant, List recessive, String recessiveSourceLevel ) 107 { 108 Map dominantById = mapById( dominant ); 109 110 for ( Iterator it = recessive.iterator(); it.hasNext(); ) 111 { 112 IdentifiableBase identifiable = (IdentifiableBase) it.next(); 113 114 if ( !dominantById.containsKey( identifiable.getId() ) ) 115 { 116 identifiable.setSourceLevel( recessiveSourceLevel ); 117 118 dominant.add( identifiable ); 119 } 120 } 121 } 122 123 private static Map mapById( List identifiables ) 124 { 125 Map byId = new HashMap (); 126 127 for ( Iterator it = identifiables.iterator(); it.hasNext(); ) 128 { 129 IdentifiableBase identifiable = (IdentifiableBase) it.next(); 130 131 byId.put( identifiable.getId(), identifiable ); 132 } 133 134 return byId; 135 } 136 137 public static org.apache.maven.model.Profile convertFromSettingsProfile( Profile settingsProfile ) 138 { 139 org.apache.maven.model.Profile profile = new org.apache.maven.model.Profile(); 140 141 profile.setId( settingsProfile.getId() ); 142 143 profile.setSource( "settings.xml" ); 144 145 Activation settingsActivation = settingsProfile.getActivation(); 146 147 if ( settingsActivation != null ) 148 { 149 org.apache.maven.model.Activation activation = new org.apache.maven.model.Activation(); 150 151 activation.setActiveByDefault( settingsActivation.isActiveByDefault() ); 152 153 activation.setJdk( settingsActivation.getJdk() ); 154 155 ActivationProperty settingsProp = settingsActivation.getProperty(); 156 157 if ( settingsProp != null ) 158 { 159 org.apache.maven.model.ActivationProperty prop = new org.apache.maven.model.ActivationProperty(); 160 161 prop.setName( settingsProp.getName() ); 162 prop.setValue( settingsProp.getValue() ); 163 164 activation.setProperty( prop ); 165 } 166 167 ActivationOS settingsOs = settingsActivation.getOs(); 168 169 if ( settingsOs != null ) 170 { 171 org.apache.maven.model.ActivationOS os = new org.apache.maven.model.ActivationOS(); 172 173 os.setArch( settingsOs.getArch() ); 174 os.setFamily( settingsOs.getFamily() ); 175 os.setName( settingsOs.getName() ); 176 os.setVersion( settingsOs.getVersion() ); 177 178 activation.setOs( os ); 179 } 180 181 org.apache.maven.settings.ActivationFile settingsFile = settingsActivation.getFile(); 182 183 if ( settingsFile != null ) 184 { 185 ActivationFile file = new ActivationFile(); 186 187 file.setExists( settingsFile.getExists() ); 188 file.setMissing( settingsFile.getMissing() ); 189 190 activation.setFile( file ); 191 } 192 193 profile.setActivation( activation ); 194 } 195 196 profile.setProperties( settingsProfile.getProperties() ); 197 198 List repos = settingsProfile.getRepositories(); 199 if ( repos != null ) 200 { 201 for ( Iterator it = repos.iterator(); it.hasNext(); ) 202 { 203 profile.addRepository( convertFromSettingsRepository( (Repository) it.next() ) ); 204 } 205 } 206 207 List pluginRepos = settingsProfile.getPluginRepositories(); 208 if ( pluginRepos != null ) 209 { 210 for ( Iterator it = pluginRepos.iterator(); it.hasNext(); ) 211 { 212 profile.addPluginRepository( convertFromSettingsRepository( (Repository) it.next() ) ); 213 } 214 } 215 216 return profile; 217 } 218 219 private static org.apache.maven.model.Repository convertFromSettingsRepository( Repository settingsRepo ) 220 { 221 org.apache.maven.model.Repository repo = new org.apache.maven.model.Repository(); 222 223 repo.setId( settingsRepo.getId() ); 224 repo.setLayout( settingsRepo.getLayout() ); 225 repo.setName( settingsRepo.getName() ); 226 repo.setUrl( settingsRepo.getUrl() ); 227 228 if ( settingsRepo.getSnapshots() != null ) 229 { 230 repo.setSnapshots( convertRepositoryPolicy( settingsRepo.getSnapshots() ) ); 231 } 232 if ( settingsRepo.getReleases() != null ) 233 { 234 repo.setReleases( convertRepositoryPolicy( settingsRepo.getReleases() ) ); 235 } 236 237 return repo; 238 } 239 240 private static org.apache.maven.model.RepositoryPolicy convertRepositoryPolicy( RepositoryPolicy settingsPolicy ) 241 { 242 org.apache.maven.model.RepositoryPolicy policy = new org.apache.maven.model.RepositoryPolicy(); 243 policy.setEnabled( settingsPolicy.isEnabled() ); 244 policy.setUpdatePolicy( settingsPolicy.getUpdatePolicy() ); 245 policy.setChecksumPolicy( settingsPolicy.getChecksumPolicy() ); 246 return policy; 247 } 248 249 } 250 | Popular Tags |