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