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 import javax.inject.Inject;
24 import javax.inject.Named;
25 import javax.inject.Singleton;
26 import org.apache.maven.execution.MavenExecutionRequest;
27 import org.apache.maven.properties.internal.SystemProperties;
28 import org.apache.maven.settings.building.DefaultSettingsBuildingRequest;
29 import org.apache.maven.settings.building.SettingsBuilder;
30 import org.apache.maven.settings.building.SettingsBuildingException;
31 import org.apache.maven.settings.building.SettingsBuildingRequest;
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 @Named
40 @Singleton
41 public class DefaultMavenSettingsBuilder extends AbstractLogEnabled implements MavenSettingsBuilder {
42
43 private final SettingsBuilder settingsBuilder;
44
45 @Inject
46 public DefaultMavenSettingsBuilder(SettingsBuilder settingsBuilder) {
47 this.settingsBuilder = settingsBuilder;
48 }
49
50 public Settings buildSettings() throws IOException, XmlPullParserException {
51 File userSettingsFile = getFile(
52 "${user.home}/.m2/settings.xml", "user.home", MavenSettingsBuilder.ALT_USER_SETTINGS_XML_LOCATION);
53
54 return buildSettings(userSettingsFile);
55 }
56
57 public Settings buildSettings(boolean useCachedSettings) throws IOException, XmlPullParserException {
58 return buildSettings();
59 }
60
61 public Settings buildSettings(File userSettingsFile) throws IOException, XmlPullParserException {
62 File globalSettingsFile = getFile(
63 "${maven.conf}/settings.xml", "maven.conf", MavenSettingsBuilder.ALT_GLOBAL_SETTINGS_XML_LOCATION);
64
65 SettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
66 request.setUserSettingsFile(userSettingsFile);
67 request.setGlobalSettingsFile(globalSettingsFile);
68 request.setSystemProperties(SystemProperties.getSystemProperties());
69 return build(request);
70 }
71
72 public Settings buildSettings(File userSettingsFile, boolean useCachedSettings)
73 throws IOException, XmlPullParserException {
74 return buildSettings(userSettingsFile);
75 }
76
77 private Settings build(SettingsBuildingRequest request) throws IOException, XmlPullParserException {
78 try {
79 return settingsBuilder.build(request).getEffectiveSettings();
80 } catch (SettingsBuildingException e) {
81 throw new IOException(e.getMessage(), e);
82 }
83 }
84
85
86 public Settings buildSettings(MavenExecutionRequest request) throws IOException, XmlPullParserException {
87 SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
88 settingsRequest.setUserSettingsFile(request.getUserSettingsFile());
89 settingsRequest.setGlobalSettingsFile(request.getGlobalSettingsFile());
90 settingsRequest.setUserProperties(request.getUserProperties());
91 settingsRequest.setSystemProperties(request.getSystemProperties());
92
93 return build(settingsRequest);
94 }
95
96 private File getFile(String pathPattern, String basedirSysProp, String altLocationSysProp) {
97
98
99
100
101
102
103
104
105
106
107
108
109
110 String path = System.getProperty(altLocationSysProp);
111
112 if (StringUtils.isEmpty(path)) {
113
114
115 String basedir = System.getProperty(basedirSysProp);
116 if (basedir == null) {
117 basedir = System.getProperty("user.dir");
118 }
119
120 basedir = basedir.replaceAll("\\\\", "/");
121 basedir = basedir.replaceAll("\\$", "\\\\\\$");
122
123 path = pathPattern.replaceAll("\\$\\{" + basedirSysProp + "\\}", basedir);
124 path = path.replaceAll("\\\\", "/");
125
126
127
128
129
130
131
132 }
133 return new File(path).getAbsoluteFile();
134 }
135 }