View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  import java.util.Properties;
29  
30  import org.apache.commons.cli.CommandLine;
31  import org.apache.maven.artifact.InvalidRepositoryException;
32  import org.apache.maven.bridge.MavenRepositorySystem;
33  import org.apache.maven.building.Source;
34  import org.apache.maven.cli.CLIManager;
35  import org.apache.maven.cli.CliRequest;
36  import org.apache.maven.execution.MavenExecutionRequest;
37  import org.apache.maven.execution.MavenExecutionRequestPopulationException;
38  import org.apache.maven.settings.Mirror;
39  import org.apache.maven.settings.Profile;
40  import org.apache.maven.settings.Proxy;
41  import org.apache.maven.settings.Repository;
42  import org.apache.maven.settings.Server;
43  import org.apache.maven.settings.Settings;
44  import org.apache.maven.settings.SettingsUtils;
45  import org.apache.maven.settings.building.DefaultSettingsBuildingRequest;
46  import org.apache.maven.settings.building.SettingsBuilder;
47  import org.apache.maven.settings.building.SettingsBuildingRequest;
48  import org.apache.maven.settings.building.SettingsBuildingResult;
49  import org.apache.maven.settings.building.SettingsProblem;
50  import org.apache.maven.settings.crypto.SettingsDecrypter;
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  
54  import static org.apache.maven.cli.ResolveFile.resolveFile;
55  
56  /**
57   * SettingsXmlConfigurationProcessor
58   */
59  @Named(SettingsXmlConfigurationProcessor.HINT)
60  @Singleton
61  @Deprecated
62  public class SettingsXmlConfigurationProcessor implements ConfigurationProcessor {
63      public static final String HINT = "settings";
64  
65      public static final String USER_HOME = System.getProperty("user.home");
66  
67      public static final File USER_MAVEN_CONFIGURATION_HOME = new File(USER_HOME, ".m2");
68  
69      public static final File DEFAULT_USER_SETTINGS_FILE = new File(USER_MAVEN_CONFIGURATION_HOME, "settings.xml");
70  
71      public static final File DEFAULT_PROJECT_SETTINGS_FILE = new File(".mvn", "settings.xml");
72  
73      public static final File DEFAULT_INSTALLATION_SETTINGS_FILE =
74              new File(System.getProperty("maven.conf"), "settings.xml");
75  
76      private static final Logger LOGGER = LoggerFactory.getLogger(SettingsXmlConfigurationProcessor.class);
77  
78      private final SettingsBuilder settingsBuilder;
79      private final SettingsDecrypter settingsDecrypter;
80  
81      @Inject
82      public SettingsXmlConfigurationProcessor(SettingsBuilder settingsBuilder, SettingsDecrypter settingsDecrypter) {
83          this.settingsBuilder = settingsBuilder;
84          this.settingsDecrypter = settingsDecrypter;
85      }
86  
87      @Override
88      public void process(CliRequest cliRequest) throws Exception {
89          CommandLine commandLine = cliRequest.getCommandLine();
90          String workingDirectory = cliRequest.getWorkingDirectory();
91          MavenExecutionRequest request = cliRequest.getRequest();
92  
93          File userSettingsFile;
94  
95          if (commandLine.hasOption(CLIManager.ALTERNATE_USER_SETTINGS)) {
96              userSettingsFile = new File(commandLine.getOptionValue(CLIManager.ALTERNATE_USER_SETTINGS));
97              userSettingsFile = resolveFile(userSettingsFile, workingDirectory);
98  
99              if (!userSettingsFile.isFile()) {
100                 throw new FileNotFoundException("The specified user settings file does not exist: " + userSettingsFile);
101             }
102         } else {
103             userSettingsFile = DEFAULT_USER_SETTINGS_FILE;
104         }
105 
106         File projectSettingsFile;
107 
108         if (commandLine.hasOption(CLIManager.ALTERNATE_PROJECT_SETTINGS)) {
109             projectSettingsFile = new File(commandLine.getOptionValue(CLIManager.ALTERNATE_PROJECT_SETTINGS));
110             projectSettingsFile = resolveFile(projectSettingsFile, workingDirectory);
111 
112             if (!projectSettingsFile.isFile()) {
113                 throw new FileNotFoundException(
114                         "The specified project settings file does not exist: " + projectSettingsFile);
115             }
116         } else if (cliRequest.getRootDirectory() != null) {
117             projectSettingsFile = DEFAULT_PROJECT_SETTINGS_FILE;
118             projectSettingsFile = resolveFile(
119                     projectSettingsFile, cliRequest.getRootDirectory().toString());
120         } else {
121             projectSettingsFile = null;
122         }
123 
124         File installationSettingsFile;
125 
126         if (commandLine.hasOption(CLIManager.ALTERNATE_INSTALLATION_SETTINGS)) {
127             installationSettingsFile = new File(commandLine.getOptionValue(CLIManager.ALTERNATE_INSTALLATION_SETTINGS));
128             installationSettingsFile = resolveFile(installationSettingsFile, workingDirectory);
129 
130             if (!installationSettingsFile.isFile()) {
131                 throw new FileNotFoundException(
132                         "The specified installation settings file does not exist: " + installationSettingsFile);
133             }
134         } else if (commandLine.hasOption(CLIManager.ALTERNATE_GLOBAL_SETTINGS)) {
135             installationSettingsFile = new File(commandLine.getOptionValue(CLIManager.ALTERNATE_GLOBAL_SETTINGS));
136             installationSettingsFile = resolveFile(installationSettingsFile, workingDirectory);
137 
138             if (!installationSettingsFile.isFile()) {
139                 throw new FileNotFoundException(
140                         "The specified installation settings file does not exist: " + installationSettingsFile);
141             }
142         } else {
143             installationSettingsFile = DEFAULT_INSTALLATION_SETTINGS_FILE;
144         }
145 
146         request.setInstallationSettingsFile(installationSettingsFile);
147         request.setProjectSettingsFile(projectSettingsFile);
148         request.setUserSettingsFile(userSettingsFile);
149 
150         SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
151         settingsRequest.setGlobalSettingsFile(installationSettingsFile);
152         settingsRequest.setProjectSettingsFile(projectSettingsFile);
153         settingsRequest.setUserSettingsFile(userSettingsFile);
154         settingsRequest.setSystemProperties(cliRequest.getSystemProperties());
155         Properties props = cliRequest.getUserProperties();
156         if (cliRequest.getRootDirectory() != null) {
157             props = new Properties();
158             props.putAll(cliRequest.getUserProperties());
159             props.put("session.rootDirectory", cliRequest.getRootDirectory().toString());
160         }
161         settingsRequest.setUserProperties(props);
162 
163         if (request.getEventSpyDispatcher() != null) {
164             request.getEventSpyDispatcher().onEvent(settingsRequest);
165         }
166 
167         LOGGER.debug(
168                 "Reading installation settings from '{}'",
169                 getLocation(settingsRequest.getGlobalSettingsSource(), settingsRequest.getGlobalSettingsFile()));
170         LOGGER.debug(
171                 "Reading project settings from '{}'",
172                 getLocation(settingsRequest.getProjectSettingsSource(), settingsRequest.getProjectSettingsFile()));
173         LOGGER.debug(
174                 "Reading user settings from '{}'",
175                 getLocation(settingsRequest.getUserSettingsSource(), settingsRequest.getUserSettingsFile()));
176 
177         SettingsBuildingResult settingsResult = settingsBuilder.build(settingsRequest);
178 
179         if (request.getEventSpyDispatcher() != null) {
180             request.getEventSpyDispatcher().onEvent(settingsResult);
181         }
182 
183         populateFromSettings(request, settingsResult.getEffectiveSettings());
184 
185         if (!settingsResult.getProblems().isEmpty() && LOGGER.isWarnEnabled()) {
186             LOGGER.warn("");
187             LOGGER.warn("Some problems were encountered while building the effective settings");
188 
189             for (SettingsProblem problem : settingsResult.getProblems()) {
190                 LOGGER.warn("{} @ {}", problem.getMessage(), problem.getLocation());
191             }
192             LOGGER.warn("");
193         }
194     }
195 
196     private MavenExecutionRequest populateFromSettings(MavenExecutionRequest request, Settings settings)
197             throws MavenExecutionRequestPopulationException {
198         if (settings == null) {
199             return request;
200         }
201 
202         request.setOffline(settings.isOffline());
203 
204         request.setInteractiveMode(settings.isInteractiveMode());
205 
206         request.setPluginGroups(settings.getPluginGroups());
207 
208         request.setLocalRepositoryPath(settings.getLocalRepository());
209 
210         for (Server server : settings.getServers()) {
211             request.addServer(server);
212         }
213 
214         //  <proxies>
215         //    <proxy>
216         //      <active>true</active>
217         //      <protocol>http</protocol>
218         //      <host>proxy.somewhere.com</host>
219         //      <port>8080</port>
220         //      <username>proxyuser</username>
221         //      <password>somepassword</password>
222         //      <nonProxyHosts>www.google.com|*.somewhere.com</nonProxyHosts>
223         //    </proxy>
224         //  </proxies>
225 
226         for (Proxy proxy : settings.getProxies()) {
227             if (!proxy.isActive()) {
228                 continue;
229             }
230 
231             request.addProxy(proxy);
232         }
233 
234         // <mirrors>
235         //   <mirror>
236         //     <id>nexus</id>
237         //     <mirrorOf>*</mirrorOf>
238         //     <url>http://repository.sonatype.org/content/groups/public</url>
239         //   </mirror>
240         // </mirrors>
241 
242         for (Mirror mirror : settings.getMirrors()) {
243             request.addMirror(mirror);
244         }
245 
246         for (Repository remoteRepository : settings.getRepositories()) {
247             try {
248                 request.addRemoteRepository(MavenRepositorySystem.buildArtifactRepository(remoteRepository));
249             } catch (InvalidRepositoryException e) {
250                 // do nothing for now
251             }
252         }
253 
254         for (Repository pluginRepository : settings.getPluginRepositories()) {
255             try {
256                 request.addPluginArtifactRepository(MavenRepositorySystem.buildArtifactRepository(pluginRepository));
257             } catch (InvalidRepositoryException e) {
258                 // do nothing for now
259             }
260         }
261 
262         request.setActiveProfiles(settings.getActiveProfiles());
263 
264         for (Profile rawProfile : settings.getProfiles()) {
265             request.addProfile(SettingsUtils.convertFromSettingsProfile(rawProfile));
266 
267             if (settings.getActiveProfiles().contains(rawProfile.getId())) {
268                 List<Repository> remoteRepositories = rawProfile.getRepositories();
269                 for (Repository remoteRepository : remoteRepositories) {
270                     try {
271                         request.addRemoteRepository(MavenRepositorySystem.buildArtifactRepository(remoteRepository));
272                     } catch (InvalidRepositoryException e) {
273                         // do nothing for now
274                     }
275                 }
276 
277                 List<Repository> pluginRepositories = rawProfile.getPluginRepositories();
278                 for (Repository pluginRepository : pluginRepositories) {
279                     try {
280                         request.addPluginArtifactRepository(
281                                 MavenRepositorySystem.buildArtifactRepository(pluginRepository));
282                     } catch (InvalidRepositoryException e) {
283                         // do nothing for now
284                     }
285                 }
286             }
287         }
288         return request;
289     }
290 
291     private Object getLocation(Source source, File defaultLocation) {
292         if (source != null) {
293             return source.getLocation();
294         }
295         return defaultLocation;
296     }
297 }