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 | 1 | public class DefaultMavenSettingsBuilder |
45 | |
extends AbstractLogEnabled |
46 | |
implements MavenSettingsBuilder, Initializable |
47 | |
{ |
48 | 1 | 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 | 0 | userSettingsFile = |
73 | |
getFile( userSettingsPath, "user.home", MavenSettingsBuilder.ALT_USER_SETTINGS_XML_LOCATION ); |
74 | |
|
75 | 0 | globalSettingsFile = |
76 | |
getFile( globalSettingsPath, "maven.home", MavenSettingsBuilder.ALT_GLOBAL_SETTINGS_XML_LOCATION ); |
77 | |
|
78 | 0 | getLogger().debug( |
79 | |
"Building Maven global-level settings from: '" + globalSettingsFile.getAbsolutePath() + "'" ); |
80 | 0 | getLogger().debug( "Building Maven user-level settings from: '" + userSettingsFile.getAbsolutePath() + "'" ); |
81 | 0 | } |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
private Settings readSettings( File settingsFile ) |
88 | |
throws IOException, XmlPullParserException |
89 | |
{ |
90 | 6 | Settings settings = null; |
91 | |
|
92 | 6 | if ( settingsFile != null && settingsFile.exists() && settingsFile.isFile() ) |
93 | |
{ |
94 | 3 | Reader reader = null; |
95 | |
try |
96 | |
{ |
97 | 3 | reader = ReaderFactory.newXmlReader( settingsFile ); |
98 | 3 | StringWriter sWriter = new StringWriter(); |
99 | |
|
100 | 3 | IOUtil.copy( reader, sWriter ); |
101 | |
|
102 | 3 | String rawInput = sWriter.toString(); |
103 | |
|
104 | |
try |
105 | |
{ |
106 | 3 | RegexBasedInterpolator interpolator = new RegexBasedInterpolator(); |
107 | 3 | interpolator.addValueSource( new EnvarBasedValueSource() ); |
108 | |
|
109 | 3 | rawInput = interpolator.interpolate( rawInput, "settings" ); |
110 | |
} |
111 | 0 | catch ( Exception e ) |
112 | |
{ |
113 | 0 | getLogger().warn( |
114 | |
"Failed to initialize environment variable resolver. Skipping environment substitution in settings." ); |
115 | 0 | getLogger().debug( "Failed to initialize envar resolver. Skipping resolution.", e ); |
116 | 3 | } |
117 | |
|
118 | 3 | StringReader sReader = new StringReader( rawInput ); |
119 | |
|
120 | 3 | SettingsXpp3Reader modelReader = new SettingsXpp3Reader(); |
121 | |
|
122 | 3 | settings = modelReader.read( sReader, true ); |
123 | |
|
124 | 3 | RuntimeInfo rtInfo = new RuntimeInfo( settings ); |
125 | |
|
126 | 3 | rtInfo.setFile( settingsFile ); |
127 | |
|
128 | 3 | settings.setRuntimeInfo( rtInfo ); |
129 | |
} |
130 | |
finally |
131 | |
{ |
132 | 3 | IOUtil.close( reader ); |
133 | 3 | } |
134 | |
} |
135 | |
|
136 | 6 | return settings; |
137 | |
} |
138 | |
|
139 | |
public Settings buildSettings() |
140 | |
throws IOException, XmlPullParserException |
141 | |
{ |
142 | 0 | return buildSettings( userSettingsFile ); |
143 | |
} |
144 | |
|
145 | |
public Settings buildSettings( boolean useCachedSettings ) |
146 | |
throws IOException, XmlPullParserException |
147 | |
{ |
148 | 0 | return buildSettings( userSettingsFile, useCachedSettings ); |
149 | |
} |
150 | |
|
151 | |
public Settings buildSettings( File userSettingsFile ) |
152 | |
throws IOException, XmlPullParserException |
153 | |
{ |
154 | 0 | return buildSettings( userSettingsFile, true ); |
155 | |
} |
156 | |
|
157 | |
public Settings buildSettings( File userSettingsFile, boolean useCachedSettings ) |
158 | |
throws IOException, XmlPullParserException |
159 | |
{ |
160 | 3 | if ( !useCachedSettings || loadedSettings == null ) |
161 | |
{ |
162 | 3 | Settings globalSettings = readSettings( globalSettingsFile ); |
163 | 3 | Settings userSettings = readSettings( userSettingsFile ); |
164 | |
|
165 | 3 | if ( globalSettings == null ) |
166 | |
{ |
167 | 3 | globalSettings = new Settings(); |
168 | |
} |
169 | |
|
170 | 3 | if ( userSettings == null ) |
171 | |
{ |
172 | 0 | userSettings = new Settings(); |
173 | 0 | userSettings.setRuntimeInfo( new RuntimeInfo( userSettings ) ); |
174 | |
} |
175 | |
|
176 | 3 | SettingsUtils.merge( userSettings, globalSettings, TrackableBase.GLOBAL_LEVEL ); |
177 | |
|
178 | 3 | activateDefaultProfiles( userSettings ); |
179 | |
|
180 | 3 | setLocalRepository( userSettings ); |
181 | |
|
182 | 3 | loadedSettings = userSettings; |
183 | |
} |
184 | |
|
185 | 3 | return loadedSettings; |
186 | |
} |
187 | |
|
188 | |
private void activateDefaultProfiles( Settings settings ) |
189 | |
{ |
190 | 3 | List activeProfiles = settings.getActiveProfiles(); |
191 | |
|
192 | 3 | for ( Iterator profiles = settings.getProfiles().iterator(); profiles.hasNext(); ) |
193 | |
{ |
194 | 0 | Profile profile = (Profile) profiles.next(); |
195 | 0 | if ( profile.getActivation() != null && profile.getActivation().isActiveByDefault() |
196 | |
&& !activeProfiles.contains( profile.getId() ) ) |
197 | |
{ |
198 | 0 | settings.addActiveProfile( profile.getId() ); |
199 | |
} |
200 | |
} |
201 | 3 | } |
202 | |
|
203 | |
private void setLocalRepository( Settings userSettings ) |
204 | |
{ |
205 | |
|
206 | 3 | String localRepository = System.getProperty( MavenSettingsBuilder.ALT_LOCAL_REPOSITORY_LOCATION ); |
207 | |
|
208 | |
|
209 | 3 | if ( localRepository == null || localRepository.length() < 1 ) |
210 | |
{ |
211 | 3 | localRepository = userSettings.getLocalRepository(); |
212 | |
} |
213 | |
|
214 | |
|
215 | 3 | if ( localRepository == null || localRepository.length() < 1 ) |
216 | |
{ |
217 | 3 | File mavenUserConfigurationDirectory = new File( userHome, ".m2" ); |
218 | 3 | if ( !mavenUserConfigurationDirectory.exists() ) |
219 | |
{ |
220 | 0 | if ( !mavenUserConfigurationDirectory.mkdirs() ) |
221 | |
{ |
222 | |
|
223 | |
} |
224 | |
} |
225 | |
|
226 | 3 | localRepository = new File( mavenUserConfigurationDirectory, "repository" ).getAbsolutePath(); |
227 | |
} |
228 | |
|
229 | |
|
230 | 3 | File file = new File( localRepository ); |
231 | 3 | if ( !file.isAbsolute() && file.getPath().startsWith( File.separator ) ) |
232 | |
{ |
233 | 0 | localRepository = file.getAbsolutePath(); |
234 | |
} |
235 | |
|
236 | 3 | userSettings.setLocalRepository( localRepository ); |
237 | 3 | } |
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 | 0 | String path = System.getProperty( altLocationSysProp ); |
255 | |
|
256 | 0 | if ( StringUtils.isEmpty( path ) ) |
257 | |
{ |
258 | |
|
259 | |
|
260 | 0 | String basedir = System.getProperty( basedirSysProp ); |
261 | 0 | if ( basedir == null ) |
262 | |
{ |
263 | 0 | basedir = System.getProperty( "user.dir" ); |
264 | |
} |
265 | |
|
266 | 0 | basedir = basedir.replaceAll( "\\\\", "/" ); |
267 | 0 | basedir = basedir.replaceAll( "\\$", "\\\\\\$" ); |
268 | |
|
269 | 0 | path = pathPattern.replaceAll( "\\$\\{" + basedirSysProp + "\\}", basedir ); |
270 | 0 | path = path.replaceAll( "\\\\", "/" ); |
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | 0 | return new File( path ).getAbsoluteFile(); |
279 | |
} |
280 | |
else |
281 | |
{ |
282 | 0 | return new File( path ).getAbsoluteFile(); |
283 | |
} |
284 | |
} |
285 | |
|
286 | |
} |