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.xml.pull.XmlPullParserException;
36
37
38
39
40 @Named
41 @Singleton
42 public class DefaultMavenSettingsBuilder extends AbstractLogEnabled implements MavenSettingsBuilder {
43
44 private final SettingsBuilder settingsBuilder;
45
46 @Inject
47 public DefaultMavenSettingsBuilder(SettingsBuilder settingsBuilder) {
48 this.settingsBuilder = settingsBuilder;
49 }
50
51 public Settings buildSettings() throws IOException, XmlPullParserException {
52 File userSettingsFile = getFile(
53 "${user.home}/.m2/settings.xml", "user.home", MavenSettingsBuilder.ALT_USER_SETTINGS_XML_LOCATION);
54
55 return buildSettings(userSettingsFile);
56 }
57
58 public Settings buildSettings(boolean useCachedSettings) throws IOException, XmlPullParserException {
59 return buildSettings();
60 }
61
62 public Settings buildSettings(File userSettingsFile) throws IOException, XmlPullParserException {
63 File globalSettingsFile = getFile(
64 "${maven.conf}/settings.xml", "maven.conf", MavenSettingsBuilder.ALT_GLOBAL_SETTINGS_XML_LOCATION);
65
66 SettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
67 request.setUserSettingsFile(userSettingsFile);
68 request.setGlobalSettingsFile(globalSettingsFile);
69 request.setSystemProperties(SystemProperties.getSystemProperties());
70 return build(request);
71 }
72
73 public Settings buildSettings(File userSettingsFile, boolean useCachedSettings)
74 throws IOException, XmlPullParserException {
75 return buildSettings(userSettingsFile);
76 }
77
78 private Settings build(SettingsBuildingRequest request) throws IOException, XmlPullParserException {
79 try {
80 return settingsBuilder.build(request).getEffectiveSettings();
81 } catch (SettingsBuildingException e) {
82 throw new IOException(e.getMessage(), e);
83 }
84 }
85
86
87 public Settings buildSettings(MavenExecutionRequest request) throws IOException, XmlPullParserException {
88 SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
89 settingsRequest.setUserSettingsFile(request.getUserSettingsFile());
90 settingsRequest.setProjectSettingsFile(request.getProjectSettingsFile());
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 (path == null || path.isEmpty()) {
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 }