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.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   * SettingsXmlConfigurationProcessor
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         //  <proxies>
171         //    <proxy>
172         //      <active>true</active>
173         //      <protocol>http</protocol>
174         //      <host>proxy.somewhere.com</host>
175         //      <port>8080</port>
176         //      <username>proxyuser</username>
177         //      <password>somepassword</password>
178         //      <nonProxyHosts>www.google.com|*.somewhere.com</nonProxyHosts>
179         //    </proxy>
180         //  </proxies>
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         // <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             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                         // do nothing for now
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                         // do nothing for now
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 }