1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.cli.configuration;
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.FileNotFoundException;
27 import java.util.List;
28
29 import org.apache.commons.cli.CommandLine;
30 import org.apache.maven.artifact.InvalidRepositoryException;
31 import org.apache.maven.bridge.MavenRepositorySystem;
32 import org.apache.maven.building.Source;
33 import org.apache.maven.cli.CLIManager;
34 import org.apache.maven.cli.CliRequest;
35 import org.apache.maven.execution.MavenExecutionRequest;
36 import org.apache.maven.execution.MavenExecutionRequestPopulationException;
37 import org.apache.maven.settings.Mirror;
38 import org.apache.maven.settings.Proxy;
39 import org.apache.maven.settings.Repository;
40 import org.apache.maven.settings.Server;
41 import org.apache.maven.settings.Settings;
42 import org.apache.maven.settings.SettingsUtils;
43 import org.apache.maven.settings.building.DefaultSettingsBuildingRequest;
44 import org.apache.maven.settings.building.SettingsBuilder;
45 import org.apache.maven.settings.building.SettingsBuildingRequest;
46 import org.apache.maven.settings.building.SettingsBuildingResult;
47 import org.apache.maven.settings.building.SettingsProblem;
48 import org.apache.maven.settings.crypto.SettingsDecrypter;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 import static org.apache.maven.cli.ResolveFile.resolveFile;
53
54
55
56
57 @Named(SettingsXmlConfigurationProcessor.HINT)
58 @Singleton
59 public class SettingsXmlConfigurationProcessor implements ConfigurationProcessor {
60 public static final String HINT = "settings";
61
62 public static final String USER_HOME = System.getProperty("user.home");
63
64 public static final File USER_MAVEN_CONFIGURATION_HOME = new File(USER_HOME, ".m2");
65
66 public static final File DEFAULT_USER_SETTINGS_FILE = new File(USER_MAVEN_CONFIGURATION_HOME, "settings.xml");
67
68 public static final File DEFAULT_GLOBAL_SETTINGS_FILE = new File(System.getProperty("maven.conf"), "settings.xml");
69
70 private final Logger logger = LoggerFactory.getLogger(SettingsXmlConfigurationProcessor.class);
71
72 @Inject
73 private SettingsBuilder settingsBuilder;
74
75 @Inject
76 private SettingsDecrypter settingsDecrypter;
77
78 @Override
79 public void process(CliRequest cliRequest) throws Exception {
80 CommandLine commandLine = cliRequest.getCommandLine();
81 String workingDirectory = cliRequest.getWorkingDirectory();
82 MavenExecutionRequest request = cliRequest.getRequest();
83
84 File userSettingsFile;
85
86 if (commandLine.hasOption(CLIManager.ALTERNATE_USER_SETTINGS)) {
87 userSettingsFile = new File(commandLine.getOptionValue(CLIManager.ALTERNATE_USER_SETTINGS));
88 userSettingsFile = resolveFile(userSettingsFile, workingDirectory);
89
90 if (!userSettingsFile.isFile()) {
91 throw new FileNotFoundException("The specified user settings file does not exist: " + userSettingsFile);
92 }
93 } else {
94 userSettingsFile = DEFAULT_USER_SETTINGS_FILE;
95 }
96
97 File globalSettingsFile;
98
99 if (commandLine.hasOption(CLIManager.ALTERNATE_GLOBAL_SETTINGS)) {
100 globalSettingsFile = new File(commandLine.getOptionValue(CLIManager.ALTERNATE_GLOBAL_SETTINGS));
101 globalSettingsFile = resolveFile(globalSettingsFile, workingDirectory);
102
103 if (!globalSettingsFile.isFile()) {
104 throw new FileNotFoundException(
105 "The specified global settings file does not exist: " + globalSettingsFile);
106 }
107 } else {
108 globalSettingsFile = DEFAULT_GLOBAL_SETTINGS_FILE;
109 }
110
111 request.setGlobalSettingsFile(globalSettingsFile);
112 request.setUserSettingsFile(userSettingsFile);
113
114 SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
115 settingsRequest.setGlobalSettingsFile(globalSettingsFile);
116 settingsRequest.setUserSettingsFile(userSettingsFile);
117 settingsRequest.setSystemProperties(cliRequest.getSystemProperties());
118 settingsRequest.setUserProperties(cliRequest.getUserProperties());
119
120 if (request.getEventSpyDispatcher() != null) {
121 request.getEventSpyDispatcher().onEvent(settingsRequest);
122 }
123
124 logger.debug(
125 "Reading global settings from {}",
126 getLocation(settingsRequest.getGlobalSettingsSource(), settingsRequest.getGlobalSettingsFile()));
127 logger.debug(
128 "Reading user settings from {}",
129 getLocation(settingsRequest.getUserSettingsSource(), settingsRequest.getUserSettingsFile()));
130
131 SettingsBuildingResult settingsResult = settingsBuilder.build(settingsRequest);
132
133 if (request.getEventSpyDispatcher() != null) {
134 request.getEventSpyDispatcher().onEvent(settingsResult);
135 }
136
137 populateFromSettings(request, settingsResult.getEffectiveSettings());
138
139 if (!settingsResult.getProblems().isEmpty() && logger.isWarnEnabled()) {
140 logger.warn("");
141 logger.warn("Some problems were encountered while building the effective settings");
142
143 for (SettingsProblem problem : settingsResult.getProblems()) {
144 logger.warn("{} @ {}", problem.getMessage(), problem.getLocation());
145 }
146 logger.warn("");
147 }
148 }
149
150 private MavenExecutionRequest populateFromSettings(MavenExecutionRequest request, Settings settings)
151 throws MavenExecutionRequestPopulationException {
152 if (settings == null) {
153 return request;
154 }
155
156 request.setOffline(settings.isOffline());
157
158 request.setInteractiveMode(settings.isInteractiveMode());
159
160 request.setPluginGroups(settings.getPluginGroups());
161
162 request.setLocalRepositoryPath(settings.getLocalRepository());
163
164 for (Server server : settings.getServers()) {
165 server = server.clone();
166
167 request.addServer(server);
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182 for (Proxy proxy : settings.getProxies()) {
183 if (!proxy.isActive()) {
184 continue;
185 }
186
187 proxy = proxy.clone();
188
189 request.addProxy(proxy);
190 }
191
192
193
194
195
196
197
198
199
200 for (Mirror mirror : settings.getMirrors()) {
201 mirror = mirror.clone();
202
203 request.addMirror(mirror);
204 }
205
206 request.setActiveProfiles(settings.getActiveProfiles());
207
208 for (org.apache.maven.settings.Profile rawProfile : settings.getProfiles()) {
209 request.addProfile(SettingsUtils.convertFromSettingsProfile(rawProfile));
210
211 if (settings.getActiveProfiles().contains(rawProfile.getId())) {
212 List<Repository> remoteRepositories = rawProfile.getRepositories();
213 for (Repository remoteRepository : remoteRepositories) {
214 try {
215 request.addRemoteRepository(MavenRepositorySystem.buildArtifactRepository(remoteRepository));
216 } catch (InvalidRepositoryException e) {
217
218 }
219 }
220
221 List<Repository> pluginRepositories = rawProfile.getPluginRepositories();
222 for (Repository pluginRepository : pluginRepositories) {
223 try {
224 request.addPluginArtifactRepository(
225 MavenRepositorySystem.buildArtifactRepository(pluginRepository));
226 } catch (InvalidRepositoryException e) {
227
228 }
229 }
230 }
231 }
232 return request;
233 }
234
235 private Object getLocation(Source source, File defaultLocation) {
236 if (source != null) {
237 return source.getLocation();
238 }
239 return defaultLocation;
240 }
241 }