001 package org.apache.maven.settings; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022 import org.apache.maven.model.ActivationFile; 023 import org.apache.maven.settings.merge.MavenSettingsMerger; 024 025 import java.util.List; 026 027 /** 028 * Several convenience methods to handle settings 029 * 030 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a> 031 */ 032 public final class SettingsUtils 033 { 034 035 private SettingsUtils() 036 { 037 // don't allow construction. 038 } 039 040 /** 041 * @param dominant 042 * @param recessive 043 * @param recessiveSourceLevel 044 */ 045 public static void merge( Settings dominant, Settings recessive, String recessiveSourceLevel ) 046 { 047 new MavenSettingsMerger().merge( dominant, recessive, recessiveSourceLevel ); 048 } 049 050 /** 051 * @param modelProfile 052 * @return a profile 053 */ 054 public static Profile convertToSettingsProfile( org.apache.maven.model.Profile modelProfile ) 055 { 056 Profile profile = new Profile(); 057 058 profile.setId( modelProfile.getId() ); 059 060 org.apache.maven.model.Activation modelActivation = modelProfile.getActivation(); 061 062 if ( modelActivation != null ) 063 { 064 Activation activation = new Activation(); 065 066 activation.setActiveByDefault( modelActivation.isActiveByDefault() ); 067 068 activation.setJdk( modelActivation.getJdk() ); 069 070 org.apache.maven.model.ActivationProperty modelProp = modelActivation.getProperty(); 071 072 if ( modelProp != null ) 073 { 074 ActivationProperty prop = new ActivationProperty(); 075 prop.setName( modelProp.getName() ); 076 prop.setValue( modelProp.getValue() ); 077 activation.setProperty( prop ); 078 } 079 080 org.apache.maven.model.ActivationOS modelOs = modelActivation.getOs(); 081 082 if ( modelOs != null ) 083 { 084 ActivationOS os = new ActivationOS(); 085 086 os.setArch( modelOs.getArch() ); 087 os.setFamily( modelOs.getFamily() ); 088 os.setName( modelOs.getName() ); 089 os.setVersion( modelOs.getVersion() ); 090 091 activation.setOs( os ); 092 } 093 094 ActivationFile modelFile = modelActivation.getFile(); 095 096 if ( modelFile != null ) 097 { 098 org.apache.maven.settings.ActivationFile file = new org.apache.maven.settings.ActivationFile(); 099 100 file.setExists( modelFile.getExists() ); 101 file.setMissing( modelFile.getMissing() ); 102 103 activation.setFile( file ); 104 } 105 106 profile.setActivation( activation ); 107 } 108 109 profile.setProperties( modelProfile.getProperties() ); 110 111 List<org.apache.maven.model.Repository> repos = modelProfile.getRepositories(); 112 if ( repos != null ) 113 { 114 for ( org.apache.maven.model.Repository repo : repos ) 115 { 116 profile.addRepository( convertToSettingsRepository( repo ) ); 117 } 118 } 119 120 List<org.apache.maven.model.Repository> pluginRepos = modelProfile.getPluginRepositories(); 121 if ( pluginRepos != null ) 122 { 123 for ( org.apache.maven.model.Repository pluginRepo : pluginRepos ) 124 { 125 profile.addPluginRepository( convertToSettingsRepository( pluginRepo ) ); 126 } 127 } 128 129 return profile; 130 } 131 132 /** 133 * @param settingsProfile 134 * @return a profile 135 */ 136 public static org.apache.maven.model.Profile convertFromSettingsProfile( Profile settingsProfile ) 137 { 138 org.apache.maven.model.Profile profile = new org.apache.maven.model.Profile(); 139 140 profile.setId( settingsProfile.getId() ); 141 142 profile.setSource( "settings.xml" ); 143 144 Activation settingsActivation = settingsProfile.getActivation(); 145 146 if ( settingsActivation != null ) 147 { 148 org.apache.maven.model.Activation activation = new org.apache.maven.model.Activation(); 149 150 activation.setActiveByDefault( settingsActivation.isActiveByDefault() ); 151 152 activation.setJdk( settingsActivation.getJdk() ); 153 154 ActivationProperty settingsProp = settingsActivation.getProperty(); 155 156 if ( settingsProp != null ) 157 { 158 org.apache.maven.model.ActivationProperty prop = new org.apache.maven.model.ActivationProperty(); 159 160 prop.setName( settingsProp.getName() ); 161 prop.setValue( settingsProp.getValue() ); 162 163 activation.setProperty( prop ); 164 } 165 166 ActivationOS settingsOs = settingsActivation.getOs(); 167 168 if ( settingsOs != null ) 169 { 170 org.apache.maven.model.ActivationOS os = new org.apache.maven.model.ActivationOS(); 171 172 os.setArch( settingsOs.getArch() ); 173 os.setFamily( settingsOs.getFamily() ); 174 os.setName( settingsOs.getName() ); 175 os.setVersion( settingsOs.getVersion() ); 176 177 activation.setOs( os ); 178 } 179 180 org.apache.maven.settings.ActivationFile settingsFile = settingsActivation.getFile(); 181 182 if ( settingsFile != null ) 183 { 184 ActivationFile file = new ActivationFile(); 185 186 file.setExists( settingsFile.getExists() ); 187 file.setMissing( settingsFile.getMissing() ); 188 189 activation.setFile( file ); 190 } 191 192 profile.setActivation( activation ); 193 } 194 195 profile.setProperties( settingsProfile.getProperties() ); 196 197 List<Repository> repos = settingsProfile.getRepositories(); 198 if ( repos != null ) 199 { 200 for ( Repository repo : repos ) 201 { 202 profile.addRepository( convertFromSettingsRepository( repo ) ); 203 } 204 } 205 206 List<Repository> pluginRepos = settingsProfile.getPluginRepositories(); 207 if ( pluginRepos != null ) 208 { 209 for ( Repository pluginRepo : pluginRepos ) 210 { 211 profile.addPluginRepository( convertFromSettingsRepository( pluginRepo ) ); 212 } 213 } 214 215 return profile; 216 } 217 218 /** 219 * @param settingsRepo 220 * @return a repository 221 */ 222 private static org.apache.maven.model.Repository convertFromSettingsRepository( Repository settingsRepo ) 223 { 224 org.apache.maven.model.Repository repo = new org.apache.maven.model.Repository(); 225 226 repo.setId( settingsRepo.getId() ); 227 repo.setLayout( settingsRepo.getLayout() ); 228 repo.setName( settingsRepo.getName() ); 229 repo.setUrl( settingsRepo.getUrl() ); 230 231 if ( settingsRepo.getSnapshots() != null ) 232 { 233 repo.setSnapshots( convertRepositoryPolicy( settingsRepo.getSnapshots() ) ); 234 } 235 if ( settingsRepo.getReleases() != null ) 236 { 237 repo.setReleases( convertRepositoryPolicy( settingsRepo.getReleases() ) ); 238 } 239 240 return repo; 241 } 242 243 /** 244 * @param settingsPolicy 245 * @return a RepositoryPolicy 246 */ 247 private static org.apache.maven.model.RepositoryPolicy convertRepositoryPolicy( RepositoryPolicy settingsPolicy ) 248 { 249 org.apache.maven.model.RepositoryPolicy policy = new org.apache.maven.model.RepositoryPolicy(); 250 policy.setEnabled( settingsPolicy.isEnabled() ); 251 policy.setUpdatePolicy( settingsPolicy.getUpdatePolicy() ); 252 policy.setChecksumPolicy( settingsPolicy.getChecksumPolicy() ); 253 return policy; 254 } 255 256 /** 257 * @param modelRepo 258 * @return a repository 259 */ 260 private static Repository convertToSettingsRepository( org.apache.maven.model.Repository modelRepo ) 261 { 262 Repository repo = new Repository(); 263 264 repo.setId( modelRepo.getId() ); 265 repo.setLayout( modelRepo.getLayout() ); 266 repo.setName( modelRepo.getName() ); 267 repo.setUrl( modelRepo.getUrl() ); 268 269 if ( modelRepo.getSnapshots() != null ) 270 { 271 repo.setSnapshots( convertRepositoryPolicy( modelRepo.getSnapshots() ) ); 272 } 273 if ( modelRepo.getReleases() != null ) 274 { 275 repo.setReleases( convertRepositoryPolicy( modelRepo.getReleases() ) ); 276 } 277 278 return repo; 279 } 280 281 /** 282 * @param modelPolicy 283 * @return a RepositoryPolicy 284 */ 285 private static RepositoryPolicy convertRepositoryPolicy( org.apache.maven.model.RepositoryPolicy modelPolicy ) 286 { 287 RepositoryPolicy policy = new RepositoryPolicy(); 288 policy.setEnabled( modelPolicy.isEnabled() ); 289 policy.setUpdatePolicy( modelPolicy.getUpdatePolicy() ); 290 policy.setChecksumPolicy( modelPolicy.getChecksumPolicy() ); 291 return policy; 292 } 293 294 /** 295 * @param settings could be null 296 * @return a new instance of settings or null if settings was null. 297 */ 298 public static Settings copySettings( Settings settings ) 299 { 300 if ( settings == null ) 301 { 302 return null; 303 } 304 305 Settings clone = new Settings(); 306 clone.setActiveProfiles( settings.getActiveProfiles() ); 307 clone.setInteractiveMode( settings.isInteractiveMode() ); 308 clone.setLocalRepository( settings.getLocalRepository() ); 309 clone.setMirrors( settings.getMirrors() ); 310 clone.setModelEncoding( settings.getModelEncoding() ); 311 clone.setOffline( settings.isOffline() ); 312 clone.setPluginGroups( settings.getPluginGroups() ); 313 clone.setProfiles( settings.getProfiles() ); 314 clone.setProxies( settings.getProxies() ); 315 clone.setServers( settings.getServers() ); 316 clone.setSourceLevel( settings.getSourceLevel() ); 317 clone.setUsePluginRegistry( settings.isUsePluginRegistry() ); 318 319 return clone; 320 } 321 }