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