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