1 package org.apache.maven.cli.configuration;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
54
55
56 @Named ( SettingsXmlConfigurationProcessor.HINT )
57 @Singleton
58 public class SettingsXmlConfigurationProcessor
59 implements ConfigurationProcessor
60 {
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 =
70 new File( System.getProperty( "maven.conf" ), "settings.xml" );
71
72 private final Logger logger = LoggerFactory.getLogger( SettingsXmlConfigurationProcessor.class );
73
74 @Inject
75 private SettingsBuilder settingsBuilder;
76
77 @Inject
78 private SettingsDecrypter settingsDecrypter;
79
80 @Override
81 public void process( CliRequest cliRequest )
82 throws Exception
83 {
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 {
92 userSettingsFile = new File( commandLine.getOptionValue( CLIManager.ALTERNATE_USER_SETTINGS ) );
93 userSettingsFile = resolveFile( userSettingsFile, workingDirectory );
94
95 if ( !userSettingsFile.isFile() )
96 {
97 throw new FileNotFoundException( "The specified user settings file does not exist: "
98 + userSettingsFile );
99 }
100 }
101 else
102 {
103 userSettingsFile = DEFAULT_USER_SETTINGS_FILE;
104 }
105
106 File globalSettingsFile;
107
108 if ( commandLine.hasOption( CLIManager.ALTERNATE_GLOBAL_SETTINGS ) )
109 {
110 globalSettingsFile = new File( commandLine.getOptionValue( CLIManager.ALTERNATE_GLOBAL_SETTINGS ) );
111 globalSettingsFile = resolveFile( globalSettingsFile, workingDirectory );
112
113 if ( !globalSettingsFile.isFile() )
114 {
115 throw new FileNotFoundException( "The specified global settings file does not exist: "
116 + globalSettingsFile );
117 }
118 }
119 else
120 {
121 globalSettingsFile = DEFAULT_GLOBAL_SETTINGS_FILE;
122 }
123
124 request.setGlobalSettingsFile( globalSettingsFile );
125 request.setUserSettingsFile( userSettingsFile );
126
127 SettingsBuildingRequest settingsRequest = new DefaultSettingsBuildingRequest();
128 settingsRequest.setGlobalSettingsFile( globalSettingsFile );
129 settingsRequest.setUserSettingsFile( userSettingsFile );
130 settingsRequest.setSystemProperties( cliRequest.getSystemProperties() );
131 settingsRequest.setUserProperties( cliRequest.getUserProperties() );
132
133 if ( request.getEventSpyDispatcher() != null )
134 {
135 request.getEventSpyDispatcher().onEvent( settingsRequest );
136 }
137
138 logger.debug( "Reading global settings from {}",
139 getLocation( settingsRequest.getGlobalSettingsSource(), settingsRequest.getGlobalSettingsFile() ) );
140 logger.debug( "Reading user settings from {}",
141 getLocation( settingsRequest.getUserSettingsSource(), settingsRequest.getUserSettingsFile() ) );
142
143 SettingsBuildingResult settingsResult = settingsBuilder.build( settingsRequest );
144
145 if ( request.getEventSpyDispatcher() != null )
146 {
147 request.getEventSpyDispatcher().onEvent( settingsResult );
148 }
149
150 populateFromSettings( request, settingsResult.getEffectiveSettings() );
151
152 if ( !settingsResult.getProblems().isEmpty() && logger.isWarnEnabled() )
153 {
154 logger.warn( "" );
155 logger.warn( "Some problems were encountered while building the effective settings" );
156
157 for ( SettingsProblem problem : settingsResult.getProblems() )
158 {
159 logger.warn( "{} @ {}", problem.getMessage(), problem.getLocation() );
160 }
161 logger.warn( "" );
162 }
163 }
164
165 private MavenExecutionRequest populateFromSettings( MavenExecutionRequest request, Settings settings )
166 throws MavenExecutionRequestPopulationException
167 {
168 if ( settings == null )
169 {
170 return request;
171 }
172
173 request.setOffline( settings.isOffline() );
174
175 request.setInteractiveMode( settings.isInteractiveMode() );
176
177 request.setPluginGroups( settings.getPluginGroups() );
178
179 request.setLocalRepositoryPath( settings.getLocalRepository() );
180
181 for ( Server server : settings.getServers() )
182 {
183 server = server.clone();
184
185 request.addServer( server );
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199
200 for ( Proxy proxy : settings.getProxies() )
201 {
202 if ( !proxy.isActive() )
203 {
204 continue;
205 }
206
207 proxy = proxy.clone();
208
209 request.addProxy( proxy );
210 }
211
212
213
214
215
216
217
218
219
220 for ( Mirror mirror : settings.getMirrors() )
221 {
222 mirror = mirror.clone();
223
224 request.addMirror( mirror );
225 }
226
227 request.setActiveProfiles( settings.getActiveProfiles() );
228
229 for ( org.apache.maven.settings.Profile rawProfile : settings.getProfiles() )
230 {
231 request.addProfile( SettingsUtils.convertFromSettingsProfile( rawProfile ) );
232
233 if ( settings.getActiveProfiles().contains( rawProfile.getId() ) )
234 {
235 List<Repository> remoteRepositories = rawProfile.getRepositories();
236 for ( Repository remoteRepository : remoteRepositories )
237 {
238 try
239 {
240 request.addRemoteRepository(
241 MavenRepositorySystem.buildArtifactRepository( remoteRepository ) );
242 }
243 catch ( InvalidRepositoryException e )
244 {
245
246 }
247 }
248
249 List<Repository> pluginRepositories = rawProfile.getPluginRepositories();
250 for ( Repository pluginRepository : pluginRepositories )
251 {
252 try
253 {
254 request.addPluginArtifactRepository(
255 MavenRepositorySystem.buildArtifactRepository( pluginRepository ) );
256 }
257 catch ( InvalidRepositoryException e )
258 {
259
260 }
261 }
262 }
263 }
264 return request;
265 }
266
267 private Object getLocation( Source source, File defaultLocation )
268 {
269 if ( source != null )
270 {
271 return source.getLocation();
272 }
273 return defaultLocation;
274 }
275
276 static File resolveFile( File file, String workingDirectory )
277 {
278 if ( file == null )
279 {
280 return null;
281 }
282 else if ( file.isAbsolute() )
283 {
284 return file;
285 }
286 else if ( file.getPath().startsWith( File.separator ) )
287 {
288
289 return file.getAbsoluteFile();
290 }
291 else
292 {
293 return new File( workingDirectory, file.getPath() ).getAbsoluteFile();
294 }
295 }
296 }