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