1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.settings;
20
21 import java.io.File;
22 import java.io.IOException;
23
24 import org.apache.maven.execution.MavenExecutionRequest;
25 import org.apache.maven.properties.internal.SystemProperties;
26 import org.apache.maven.settings.building.DefaultSettingsBuildingRequest;
27 import org.apache.maven.settings.building.SettingsBuilder;
28 import org.apache.maven.settings.building.SettingsBuildingException;
29 import org.apache.maven.settings.building.SettingsBuildingRequest;
30 import org.codehaus.plexus.component.annotations.Component;
31 import org.codehaus.plexus.component.annotations.Requirement;
32 import org.codehaus.plexus.logging.AbstractLogEnabled;
33 import org.codehaus.plexus.util.StringUtils;
34 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
35
36
37
38
39 @Component(role = MavenSettingsBuilder.class)
40 public class DefaultMavenSettingsBuilder extends AbstractLogEnabled implements MavenSettingsBuilder {
41
42 @Requirement
43 private SettingsBuilder settingsBuilder;
44
45 public Settings buildSettings() throws IOException, XmlPullParserException {
46 File userSettingsFile = getFile(
47 "${user.home}/.m2/settings.xml", "user.home", MavenSettingsBuilder.ALT_USER_SETTINGS_XML_LOCATION);
48
49 return buildSettings(userSettingsFile);
50 }
51
52 public Settings buildSettings(boolean useCachedSettings) throws IOException, XmlPullParserException {
53 return buildSettings();
54 }
55
56 public Settings buildSettings(File userSettingsFile) throws IOException, XmlPullParserException {
57 File globalSettingsFile = getFile(
58 "${maven.conf}/settings.xml", "maven.conf", MavenSettingsBuilder.ALT_GLOBAL_SETTINGS_XML_LOCATION);
59
60 SettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
61 request.setUserSettingsFile(userSettingsFile);
62 request.setGlobalSettingsFile(globalSettingsFile);
63 request.setSystemProperties(SystemProperties.getSystemProperties());
64 return build(request);
65 }
66
67 public Settings buildSettings(File userSettingsFile, boolean useCachedSettings)
68 throws IOException, XmlPullParserException {
69 return buildSettings(userSettingsFile);
70 }
71
72 private Settings build(SettingsBuildingRequest request) throws IOException, XmlPullParserException {
73 try {
74 return settingsBuilder.build(request).getEffectiveSettings();
75 } catch (SettingsBuildingException e) {
76 throw (IOException) new IOException(e.getMessage()).initCause(e);
77 }
78 }
79
80
81 public Settings buildSettings(MavenExecutionRequest request) throws IOException, XmlPullParserException {
82 SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
83 settingsRequest.setUserSettingsFile(request.getUserSettingsFile());
84 settingsRequest.setGlobalSettingsFile(request.getGlobalSettingsFile());
85 settingsRequest.setUserProperties(request.getUserProperties());
86 settingsRequest.setSystemProperties(request.getSystemProperties());
87
88 return build(settingsRequest);
89 }
90
91 private File getFile(String pathPattern, String basedirSysProp, String altLocationSysProp) {
92
93
94
95
96
97
98
99
100
101
102
103
104
105 String path = System.getProperty(altLocationSysProp);
106
107 if (StringUtils.isEmpty(path)) {
108
109
110 String basedir = System.getProperty(basedirSysProp);
111 if (basedir == null) {
112 basedir = System.getProperty("user.dir");
113 }
114
115 basedir = basedir.replaceAll("\\\\", "/");
116 basedir = basedir.replaceAll("\\$", "\\\\\\$");
117
118 path = pathPattern.replaceAll("\\$\\{" + basedirSysProp + "\\}", basedir);
119 path = path.replaceAll("\\\\", "/");
120
121
122
123
124
125
126
127 }
128 return new File(path).getAbsoluteFile();
129 }
130 }