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