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