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