1 package org.apache.maven.settings; 2 3 18 19 import org.apache.maven.settings.io.xpp3.SettingsXpp3Reader; 20 import org.codehaus.plexus.logging.AbstractLogEnabled; 21 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; 22 import org.codehaus.plexus.util.IOUtil; 23 import org.codehaus.plexus.util.StringUtils; 24 import org.codehaus.plexus.util.interpolation.EnvarBasedValueSource; 25 import org.codehaus.plexus.util.interpolation.RegexBasedInterpolator; 26 import org.codehaus.plexus.util.xml.pull.XmlPullParserException; 27 28 import java.io.File ; 29 import java.io.FileReader ; 30 import java.io.IOException ; 31 import java.io.StringReader ; 32 import java.io.StringWriter ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 36 40 public class DefaultMavenSettingsBuilder 41 extends AbstractLogEnabled 42 implements MavenSettingsBuilder, Initializable 43 { 44 public static final String userHome = System.getProperty( "user.home" ); 45 46 49 private String userSettingsPath; 50 51 54 private String globalSettingsPath; 55 56 private File userSettingsFile; 57 58 private File globalSettingsFile; 59 60 private Settings loadedSettings; 61 62 66 public void initialize() 67 { 68 userSettingsFile = 69 getFile( userSettingsPath, "user.home", MavenSettingsBuilder.ALT_USER_SETTINGS_XML_LOCATION ); 70 71 globalSettingsFile = 72 getFile( globalSettingsPath, "maven.home", MavenSettingsBuilder.ALT_GLOBAL_SETTINGS_XML_LOCATION ); 73 74 getLogger().debug( 75 "Building Maven global-level settings from: '" + globalSettingsFile.getAbsolutePath() + "'" ); 76 getLogger().debug( "Building Maven user-level settings from: '" + userSettingsFile.getAbsolutePath() + "'" ); 77 } 78 79 83 private Settings readSettings( File settingsFile ) 84 throws IOException , XmlPullParserException 85 { 86 Settings settings = null; 87 88 if ( settingsFile.exists() && settingsFile.isFile() ) 89 { 90 FileReader reader = null; 91 try 92 { 93 reader = new FileReader ( settingsFile ); 94 StringWriter sWriter = new StringWriter (); 95 96 IOUtil.copy( reader, sWriter ); 97 98 String rawInput = sWriter.toString(); 99 100 try 101 { 102 RegexBasedInterpolator interpolator = new RegexBasedInterpolator(); 103 interpolator.addValueSource( new EnvarBasedValueSource() ); 104 105 rawInput = interpolator.interpolate( rawInput, "settings" ); 106 } 107 catch ( Exception e ) 108 { 109 getLogger().warn( 110 "Failed to initialize environment variable resolver. Skipping environment substitution in settings." ); 111 getLogger().debug( "Failed to initialize envar resolver. Skipping resolution.", e ); 112 } 113 114 StringReader sReader = new StringReader ( rawInput ); 115 116 SettingsXpp3Reader modelReader = new SettingsXpp3Reader(); 117 118 settings = modelReader.read( sReader ); 119 120 RuntimeInfo rtInfo = new RuntimeInfo( settings ); 121 122 rtInfo.setFile( settingsFile ); 123 124 settings.setRuntimeInfo( rtInfo ); 125 } 126 finally 127 { 128 IOUtil.close( reader ); 129 } 130 } 131 132 return settings; 133 } 134 135 public Settings buildSettings() 136 throws IOException , XmlPullParserException 137 { 138 return buildSettings( userSettingsFile ); 139 } 140 141 public Settings buildSettings( File userSettingsFile ) 142 throws IOException , XmlPullParserException 143 { 144 if ( loadedSettings == null ) 145 { 146 Settings globalSettings = readSettings( globalSettingsFile ); 147 Settings userSettings = readSettings( userSettingsFile ); 148 149 if ( globalSettings == null ) 150 { 151 globalSettings = new Settings(); 152 } 153 154 if ( userSettings == null ) 155 { 156 userSettings = new Settings(); 157 userSettings.setRuntimeInfo( new RuntimeInfo( userSettings ) ); 158 } 159 160 SettingsUtils.merge( userSettings, globalSettings, TrackableBase.GLOBAL_LEVEL ); 161 162 activateDefaultProfiles( userSettings ); 163 164 setLocalRepository( userSettings ); 165 166 loadedSettings = userSettings; 167 } 168 169 return loadedSettings; 170 } 171 172 private void activateDefaultProfiles( Settings settings ) 173 { 174 List activeProfiles = settings.getActiveProfiles(); 175 176 for ( Iterator profiles = settings.getProfiles().iterator(); profiles.hasNext(); ) 177 { 178 Profile profile = (Profile) profiles.next(); 179 if ( profile.getActivation() != null && profile.getActivation().isActiveByDefault() ) 180 { 181 if ( !activeProfiles.contains( profile.getId() ) ) 182 { 183 settings.addActiveProfile( profile.getId() ); 184 } 185 } 186 } 187 } 188 189 private void setLocalRepository( Settings userSettings ) 190 { 191 String localRepository = System.getProperty( MavenSettingsBuilder.ALT_LOCAL_REPOSITORY_LOCATION ); 193 194 if ( localRepository == null || localRepository.length() < 1 ) 196 { 197 localRepository = userSettings.getLocalRepository(); 198 } 199 200 if ( localRepository == null || localRepository.length() < 1 ) 202 { 203 File mavenUserConfigurationDirectory = new File ( userHome, ".m2" ); 204 if ( !mavenUserConfigurationDirectory.exists() ) 205 { 206 if ( !mavenUserConfigurationDirectory.mkdirs() ) 207 { 208 } 210 } 211 212 localRepository = new File ( mavenUserConfigurationDirectory, "repository" ).getAbsolutePath(); 213 } 214 215 userSettings.setLocalRepository( localRepository ); 216 } 217 218 private File getFile( String pathPattern, String basedirSysProp, String altLocationSysProp ) 219 { 220 233 String path = System.getProperty( altLocationSysProp ); 234 235 if ( StringUtils.isEmpty( path ) ) 236 { 237 String basedir = System.getProperty( basedirSysProp ); 240 if ( basedir == null ) 241 { 242 basedir = System.getProperty( "user.dir" ); 243 } 244 245 basedir = basedir.replaceAll( "\\\\", "/" ); 246 basedir = basedir.replaceAll( "\\$", "\\\\\\$" ); 247 248 path = pathPattern.replaceAll( "\\$\\{" + basedirSysProp + "\\}", basedir ); 249 path = path.replaceAll( "\\\\", "/" ); 250 path = path.replaceAll( "//", "/" ); 251 252 return new File ( path ).getAbsoluteFile(); 253 } 254 else 255 { 256 return new File ( path ).getAbsoluteFile(); 257 } 258 } 259 260 } 261 | Popular Tags |