1 | |
package org.apache.maven.cli; |
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.IOException; |
24 | |
import java.io.InputStream; |
25 | |
import java.text.SimpleDateFormat; |
26 | |
import java.util.Arrays; |
27 | |
import java.util.Date; |
28 | |
import java.util.Iterator; |
29 | |
import java.util.Locale; |
30 | |
import java.util.Properties; |
31 | |
import java.util.StringTokenizer; |
32 | |
import java.util.Map.Entry; |
33 | |
|
34 | |
import org.apache.commons.cli.CommandLine; |
35 | |
import org.apache.commons.cli.ParseException; |
36 | |
import org.apache.maven.Maven; |
37 | |
import org.apache.maven.SettingsConfigurationException; |
38 | |
import org.apache.maven.artifact.manager.WagonManager; |
39 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
40 | |
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory; |
41 | |
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; |
42 | |
import org.apache.maven.artifact.repository.DefaultArtifactRepository; |
43 | |
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; |
44 | |
import org.apache.maven.execution.DefaultMavenExecutionRequest; |
45 | |
import org.apache.maven.execution.MavenExecutionRequest; |
46 | |
import org.apache.maven.execution.ReactorManager; |
47 | |
import org.apache.maven.monitor.event.DefaultEventDispatcher; |
48 | |
import org.apache.maven.monitor.event.DefaultEventMonitor; |
49 | |
import org.apache.maven.monitor.event.EventDispatcher; |
50 | |
import org.apache.maven.plugin.Mojo; |
51 | |
import org.apache.maven.profiles.DefaultProfileManager; |
52 | |
import org.apache.maven.profiles.ProfileManager; |
53 | |
import org.apache.maven.reactor.MavenExecutionException; |
54 | |
import org.apache.maven.settings.MavenSettingsBuilder; |
55 | |
import org.apache.maven.settings.RuntimeInfo; |
56 | |
import org.apache.maven.settings.Settings; |
57 | |
import org.codehaus.classworlds.ClassWorld; |
58 | |
import org.codehaus.plexus.PlexusContainerException; |
59 | |
import org.codehaus.plexus.component.repository.exception.ComponentLifecycleException; |
60 | |
import org.codehaus.plexus.component.repository.exception.ComponentLookupException; |
61 | |
import org.codehaus.plexus.embed.Embedder; |
62 | |
import org.codehaus.plexus.logging.Logger; |
63 | |
import org.codehaus.plexus.logging.LoggerManager; |
64 | |
import org.codehaus.plexus.util.IOUtil; |
65 | |
import org.codehaus.plexus.util.Os; |
66 | |
import org.codehaus.plexus.util.StringUtils; |
67 | |
import org.codehaus.plexus.util.cli.CommandLineUtils; |
68 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
69 | |
import org.sonatype.plexus.components.cipher.DefaultPlexusCipher; |
70 | |
import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher; |
71 | |
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher; |
72 | |
import org.sonatype.plexus.components.sec.dispatcher.SecUtil; |
73 | |
import org.sonatype.plexus.components.sec.dispatcher.model.SettingsSecurity; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | 0 | public class MavenCli |
81 | |
{ |
82 | |
|
83 | 1 | public static final String OS_NAME = Os.OS_NAME; |
84 | |
|
85 | |
|
86 | 1 | public static final String OS_ARCH = Os.OS_ARCH; |
87 | |
|
88 | |
|
89 | 1 | public static final String OS_VERSION = Os.OS_VERSION; |
90 | |
|
91 | |
private static Embedder embedder; |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public static void main( String[] args ) |
97 | |
{ |
98 | 0 | ClassWorld classWorld = new ClassWorld( "plexus.core", Thread.currentThread().getContextClassLoader() ); |
99 | |
|
100 | 0 | int result = main( args, classWorld ); |
101 | |
|
102 | 0 | System.exit( result ); |
103 | 0 | } |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public static int main( String[] args, ClassWorld classWorld ) |
109 | |
{ |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | 0 | CLIManager cliManager = new CLIManager(); |
115 | |
|
116 | |
CommandLine commandLine; |
117 | |
try |
118 | |
{ |
119 | 0 | commandLine = cliManager.parse( args ); |
120 | |
} |
121 | 0 | catch ( ParseException e ) |
122 | |
{ |
123 | 0 | System.err.println( "Unable to parse command line options: " + e.getMessage() ); |
124 | 0 | cliManager.displayHelp(); |
125 | 0 | return 1; |
126 | 0 | } |
127 | |
|
128 | 0 | boolean debug = commandLine.hasOption( CLIManager.DEBUG ); |
129 | |
|
130 | 0 | boolean showErrors = debug || commandLine.hasOption( CLIManager.ERRORS ); |
131 | |
|
132 | 0 | if ( showErrors ) |
133 | |
{ |
134 | 0 | System.out.println( "+ Error stacktraces are turned on." ); |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | 0 | if ( commandLine.hasOption( CLIManager.HELP ) ) |
142 | |
{ |
143 | 0 | cliManager.displayHelp(); |
144 | 0 | return 0; |
145 | |
} |
146 | |
|
147 | 0 | if ( commandLine.hasOption( CLIManager.VERSION ) ) |
148 | |
{ |
149 | 0 | showVersion(); |
150 | |
|
151 | 0 | return 0; |
152 | |
} |
153 | 0 | else if ( debug || commandLine.hasOption( CLIManager.SHOW_VERSION ) ) |
154 | |
{ |
155 | 0 | showVersion(); |
156 | |
} |
157 | |
|
158 | 0 | EventDispatcher eventDispatcher = new DefaultEventDispatcher(); |
159 | |
|
160 | |
|
161 | |
|
162 | 0 | String mavenHome = System.getProperty( "maven.home" ); |
163 | 0 | if ( mavenHome != null ) |
164 | |
{ |
165 | 0 | System.setProperty( "maven.home", new File( mavenHome ).getAbsolutePath() ); |
166 | |
} |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | 0 | embedder = new Embedder(); |
174 | |
|
175 | |
try |
176 | |
{ |
177 | 0 | embedder.start( classWorld ); |
178 | |
} |
179 | 0 | catch ( PlexusContainerException e ) |
180 | |
{ |
181 | 0 | showFatalError( "Unable to start the embedded plexus container", e, showErrors ); |
182 | |
|
183 | 0 | return 1; |
184 | 0 | } |
185 | |
|
186 | |
|
187 | |
try |
188 | |
{ |
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | 0 | Properties executionProperties = new Properties(); |
195 | 0 | Properties userProperties = new Properties(); |
196 | 0 | populateProperties( commandLine, executionProperties, userProperties ); |
197 | |
|
198 | |
Settings settings; |
199 | |
|
200 | |
try |
201 | |
{ |
202 | 0 | settings = buildSettings( commandLine ); |
203 | |
} |
204 | 0 | catch ( SettingsConfigurationException e ) |
205 | |
{ |
206 | 0 | showError( "Error reading settings.xml: " + e.getMessage(), e, showErrors ); |
207 | |
|
208 | 0 | return 1; |
209 | |
} |
210 | 0 | catch ( ComponentLookupException e ) |
211 | |
{ |
212 | 0 | showFatalError( "Unable to read settings.xml", e, showErrors ); |
213 | |
|
214 | 0 | return 1; |
215 | 0 | } |
216 | |
|
217 | |
DefaultSecDispatcher dispatcher; |
218 | |
try |
219 | |
{ |
220 | 0 | if ( commandLine.hasOption( CLIManager.ENCRYPT_MASTER_PASSWORD ) ) |
221 | |
{ |
222 | 0 | String passwd = commandLine.getOptionValue( CLIManager.ENCRYPT_MASTER_PASSWORD ); |
223 | |
|
224 | 0 | DefaultPlexusCipher cipher = new DefaultPlexusCipher(); |
225 | |
|
226 | 0 | System.out.println( cipher.encryptAndDecorate( passwd, |
227 | |
DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION ) ); |
228 | |
|
229 | 0 | return 0; |
230 | |
} |
231 | 0 | else if ( commandLine.hasOption( CLIManager.ENCRYPT_PASSWORD ) ) |
232 | |
{ |
233 | 0 | String passwd = commandLine.getOptionValue( CLIManager.ENCRYPT_PASSWORD ); |
234 | |
|
235 | 0 | dispatcher = (DefaultSecDispatcher) embedder.lookup( SecDispatcher.ROLE ); |
236 | 0 | String configurationFile = dispatcher.getConfigurationFile(); |
237 | 0 | if ( configurationFile.startsWith( "~" ) ) |
238 | |
{ |
239 | 0 | configurationFile = System.getProperty( "user.home" ) + configurationFile.substring( 1 ); |
240 | |
} |
241 | 0 | String file = System.getProperty( DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION, configurationFile ); |
242 | 0 | embedder.release( dispatcher ); |
243 | |
|
244 | 0 | String master = null; |
245 | |
|
246 | 0 | SettingsSecurity sec = SecUtil.read( file, true ); |
247 | 0 | if ( sec != null ) |
248 | |
{ |
249 | 0 | master = sec.getMaster(); |
250 | |
} |
251 | |
|
252 | 0 | if ( master == null ) |
253 | |
{ |
254 | 0 | System.err.println( "Master password is not set in the setting security file" ); |
255 | |
|
256 | 0 | return 1; |
257 | |
} |
258 | |
|
259 | 0 | DefaultPlexusCipher cipher = new DefaultPlexusCipher(); |
260 | 0 | String masterPasswd = |
261 | |
cipher.decryptDecorated( master, DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION ); |
262 | 0 | System.out.println( cipher.encryptAndDecorate( passwd, masterPasswd ) ); |
263 | |
|
264 | 0 | return 0; |
265 | |
} |
266 | |
} |
267 | 0 | catch ( Exception e ) |
268 | |
{ |
269 | 0 | showFatalError( "Error encrypting password: " + e.getMessage(), e, showErrors ); |
270 | |
|
271 | 0 | return 1; |
272 | 0 | } |
273 | |
|
274 | 0 | Maven maven = null; |
275 | |
|
276 | 0 | MavenExecutionRequest request = null; |
277 | |
|
278 | 0 | LoggerManager loggerManager = null; |
279 | |
|
280 | |
try |
281 | |
{ |
282 | |
|
283 | 0 | loggerManager = (LoggerManager) embedder.lookup( LoggerManager.ROLE ); |
284 | |
|
285 | 0 | if ( debug ) |
286 | |
{ |
287 | 0 | loggerManager.setThreshold( Logger.LEVEL_DEBUG ); |
288 | |
} |
289 | 0 | else if ( commandLine.hasOption( CLIManager.QUIET ) ) |
290 | |
{ |
291 | |
|
292 | |
|
293 | 0 | loggerManager.setThreshold( Logger.LEVEL_ERROR ); |
294 | |
|
295 | |
} |
296 | |
|
297 | 0 | ProfileManager profileManager = new DefaultProfileManager( embedder.getContainer(), executionProperties ); |
298 | |
|
299 | 0 | if ( commandLine.hasOption( CLIManager.ACTIVATE_PROFILES ) ) |
300 | |
{ |
301 | 0 | String [] profileOptionValues = commandLine.getOptionValues( CLIManager.ACTIVATE_PROFILES ); |
302 | |
|
303 | 0 | if ( profileOptionValues != null ) |
304 | |
{ |
305 | 0 | for ( int i=0; i < profileOptionValues.length; ++i ) |
306 | |
{ |
307 | 0 | StringTokenizer profileTokens = new StringTokenizer( profileOptionValues[i], "," ); |
308 | |
|
309 | 0 | while ( profileTokens.hasMoreTokens() ) |
310 | |
{ |
311 | 0 | String profileAction = profileTokens.nextToken().trim(); |
312 | |
|
313 | 0 | if ( profileAction.startsWith( "-" ) || profileAction.startsWith( "!" ) ) |
314 | |
{ |
315 | 0 | profileManager.explicitlyDeactivate( profileAction.substring( 1 ) ); |
316 | |
} |
317 | 0 | else if ( profileAction.startsWith( "+" ) ) |
318 | |
{ |
319 | 0 | profileManager.explicitlyActivate( profileAction.substring( 1 ) ); |
320 | |
} |
321 | |
else |
322 | |
{ |
323 | 0 | profileManager.explicitlyActivate( profileAction ); |
324 | |
} |
325 | 0 | } |
326 | |
} |
327 | |
} |
328 | |
} |
329 | |
|
330 | 0 | request = createRequest( commandLine, settings, eventDispatcher, loggerManager, profileManager, |
331 | |
executionProperties, userProperties, showErrors ); |
332 | |
|
333 | 0 | setProjectFileOptions( commandLine, request ); |
334 | |
|
335 | 0 | maven = |
336 | |
createMavenInstance( settings.isInteractiveMode(), |
337 | |
loggerManager.getLoggerForComponent( WagonManager.ROLE ) ); |
338 | |
} |
339 | 0 | catch ( ComponentLookupException e ) |
340 | |
{ |
341 | 0 | showFatalError( "Unable to configure the Maven application", e, showErrors ); |
342 | |
|
343 | 0 | return 1; |
344 | |
} |
345 | |
finally |
346 | |
{ |
347 | 0 | if ( loggerManager != null ) |
348 | |
{ |
349 | |
try |
350 | |
{ |
351 | 0 | embedder.release( loggerManager ); |
352 | |
} |
353 | 0 | catch ( ComponentLifecycleException e ) |
354 | |
{ |
355 | 0 | showFatalError( "Error releasing logging manager", e, showErrors ); |
356 | 0 | } |
357 | |
} |
358 | |
} |
359 | |
|
360 | |
try |
361 | |
{ |
362 | 0 | maven.execute( request ); |
363 | |
} |
364 | 0 | catch ( MavenExecutionException e ) |
365 | |
{ |
366 | 0 | return 1; |
367 | 0 | } |
368 | |
} |
369 | |
finally |
370 | |
{ |
371 | 0 | try |
372 | |
{ |
373 | 0 | embedder.stop(); |
374 | |
} |
375 | 0 | catch ( Exception e ) |
376 | |
{ |
377 | |
|
378 | 0 | } |
379 | 0 | } |
380 | |
|
381 | 0 | return 0; |
382 | |
} |
383 | |
|
384 | |
private static Settings buildSettings( CommandLine commandLine ) |
385 | |
throws ComponentLookupException, SettingsConfigurationException |
386 | |
{ |
387 | 0 | String userSettingsPath = null; |
388 | |
|
389 | 0 | if ( commandLine.hasOption( CLIManager.ALTERNATE_USER_SETTINGS ) ) |
390 | |
{ |
391 | 0 | userSettingsPath = commandLine.getOptionValue( CLIManager.ALTERNATE_USER_SETTINGS ); |
392 | |
} |
393 | |
|
394 | 0 | if ( commandLine.hasOption( CLIManager.ALTERNATE_GLOBAL_SETTINGS ) ) |
395 | |
{ |
396 | 0 | String globalSettingsPath = commandLine.getOptionValue( CLIManager.ALTERNATE_GLOBAL_SETTINGS ); |
397 | 0 | System.setProperty( MavenSettingsBuilder.ALT_GLOBAL_SETTINGS_XML_LOCATION, globalSettingsPath ); |
398 | |
} |
399 | |
|
400 | 0 | Settings settings = null; |
401 | |
|
402 | 0 | MavenSettingsBuilder settingsBuilder = (MavenSettingsBuilder) embedder.lookup( MavenSettingsBuilder.ROLE ); |
403 | |
|
404 | |
try |
405 | |
{ |
406 | 0 | if ( userSettingsPath != null ) |
407 | |
{ |
408 | 0 | File userSettingsFile = new File( userSettingsPath ); |
409 | |
|
410 | 0 | if ( userSettingsFile.exists() && !userSettingsFile.isDirectory() ) |
411 | |
{ |
412 | 0 | settings = settingsBuilder.buildSettings( userSettingsFile ); |
413 | |
} |
414 | |
else |
415 | |
{ |
416 | 0 | System.out.println( "WARNING: Alternate user settings file: " + userSettingsPath + |
417 | |
" is invalid. Using default path." ); |
418 | |
} |
419 | |
} |
420 | |
|
421 | 0 | if ( settings == null ) |
422 | |
{ |
423 | 0 | settings = settingsBuilder.buildSettings(); |
424 | |
} |
425 | |
} |
426 | 0 | catch ( IOException e ) |
427 | |
{ |
428 | 0 | throw new SettingsConfigurationException( "Error reading settings file", e ); |
429 | |
} |
430 | 0 | catch ( XmlPullParserException e ) |
431 | |
{ |
432 | 0 | throw new SettingsConfigurationException( e.getMessage(), e, e.getLineNumber(), |
433 | |
e.getColumnNumber() ); |
434 | 0 | } |
435 | |
|
436 | |
|
437 | |
|
438 | 0 | if ( commandLine.hasOption( CLIManager.BATCH_MODE ) ) |
439 | |
{ |
440 | 0 | settings.setInteractiveMode( false ); |
441 | |
} |
442 | |
|
443 | 0 | if ( commandLine.hasOption( CLIManager.SUPPRESS_PLUGIN_REGISTRY ) ) |
444 | |
{ |
445 | 0 | settings.setUsePluginRegistry( false ); |
446 | |
} |
447 | |
|
448 | |
|
449 | |
|
450 | 0 | settings.setRuntimeInfo( createRuntimeInfo( commandLine, settings ) ); |
451 | |
|
452 | 0 | return settings; |
453 | |
} |
454 | |
|
455 | |
private static RuntimeInfo createRuntimeInfo( CommandLine commandLine, Settings settings ) |
456 | |
{ |
457 | 0 | RuntimeInfo runtimeInfo = new RuntimeInfo( settings ); |
458 | |
|
459 | 0 | if ( commandLine.hasOption( CLIManager.FORCE_PLUGIN_UPDATES ) || |
460 | |
commandLine.hasOption( CLIManager.FORCE_PLUGIN_UPDATES2 ) ) |
461 | |
{ |
462 | 0 | runtimeInfo.setPluginUpdateOverride( Boolean.TRUE ); |
463 | |
} |
464 | 0 | else if ( commandLine.hasOption( CLIManager.SUPPRESS_PLUGIN_UPDATES ) ) |
465 | |
{ |
466 | 0 | runtimeInfo.setPluginUpdateOverride( Boolean.FALSE ); |
467 | |
} |
468 | |
|
469 | 0 | return runtimeInfo; |
470 | |
} |
471 | |
|
472 | |
|
473 | |
private static void showFatalError( String message, Exception e, boolean show ) |
474 | |
{ |
475 | 0 | System.err.println( "FATAL ERROR: " + message ); |
476 | 0 | if ( show ) |
477 | |
{ |
478 | 0 | System.err.println( "Error stacktrace:" ); |
479 | |
|
480 | 0 | e.printStackTrace(); |
481 | |
} |
482 | |
else |
483 | |
{ |
484 | 0 | System.err.println( "For more information, run with the -e flag" ); |
485 | |
} |
486 | 0 | } |
487 | |
|
488 | |
private static void showError( String message, Exception e, boolean show ) |
489 | |
{ |
490 | 0 | System.err.println( message ); |
491 | 0 | if ( show ) |
492 | |
{ |
493 | 0 | System.err.println( "Error stacktrace:" ); |
494 | |
|
495 | 0 | e.printStackTrace(); |
496 | |
} |
497 | 0 | } |
498 | |
|
499 | |
private static MavenExecutionRequest createRequest( CommandLine commandLine, Settings settings, |
500 | |
EventDispatcher eventDispatcher, LoggerManager loggerManager, |
501 | |
ProfileManager profileManager, Properties executionProperties, |
502 | |
Properties userProperties, boolean showErrors ) |
503 | |
throws ComponentLookupException |
504 | |
{ |
505 | |
MavenExecutionRequest request; |
506 | |
|
507 | 0 | ArtifactRepository localRepository = createLocalRepository( embedder, settings, commandLine ); |
508 | |
|
509 | 0 | File userDir = new File( System.getProperty( "user.dir" ) ); |
510 | |
|
511 | 0 | request = new DefaultMavenExecutionRequest( localRepository, settings, eventDispatcher, |
512 | |
commandLine.getArgList(), userDir.getPath(), profileManager, |
513 | |
executionProperties, userProperties, showErrors ); |
514 | |
|
515 | |
|
516 | 0 | Logger logger = loggerManager.getLoggerForComponent( Mojo.ROLE ); |
517 | |
|
518 | 0 | if ( logger != null ) |
519 | |
{ |
520 | 0 | request.addEventMonitor( new DefaultEventMonitor( logger ) ); |
521 | |
} |
522 | |
|
523 | 0 | if ( commandLine.hasOption( CLIManager.NON_RECURSIVE ) ) |
524 | |
{ |
525 | 0 | request.setRecursive( false ); |
526 | |
} |
527 | |
|
528 | 0 | if ( commandLine.hasOption( CLIManager.FAIL_FAST ) ) |
529 | |
{ |
530 | 0 | request.setFailureBehavior( ReactorManager.FAIL_FAST ); |
531 | |
} |
532 | 0 | else if ( commandLine.hasOption( CLIManager.FAIL_AT_END ) ) |
533 | |
{ |
534 | 0 | request.setFailureBehavior( ReactorManager.FAIL_AT_END ); |
535 | |
} |
536 | 0 | else if ( commandLine.hasOption( CLIManager.FAIL_NEVER ) ) |
537 | |
{ |
538 | 0 | request.setFailureBehavior( ReactorManager.FAIL_NEVER ); |
539 | |
} |
540 | |
|
541 | 0 | return request; |
542 | |
} |
543 | |
|
544 | |
private static void setProjectFileOptions( CommandLine commandLine, MavenExecutionRequest request ) |
545 | |
{ |
546 | 0 | if ( commandLine.hasOption( CLIManager.REACTOR ) ) |
547 | |
{ |
548 | 0 | request.setReactorActive( true ); |
549 | |
} |
550 | 0 | else if ( commandLine.hasOption( CLIManager.ALTERNATE_POM_FILE ) ) |
551 | |
{ |
552 | 0 | request.setPomFile( commandLine.getOptionValue( CLIManager.ALTERNATE_POM_FILE ) ); |
553 | |
} |
554 | |
|
555 | 0 | if ( commandLine.hasOption( CLIManager.RESUME_FROM ) ) |
556 | |
{ |
557 | 0 | request.setResumeFrom( commandLine.getOptionValue( CLIManager.RESUME_FROM ) ); |
558 | |
} |
559 | |
|
560 | 0 | if ( commandLine.hasOption( CLIManager.PROJECT_LIST ) ) |
561 | |
{ |
562 | 0 | String projectList = commandLine.getOptionValue( CLIManager.PROJECT_LIST ); |
563 | 0 | String[] projects = StringUtils.split( projectList, "," ); |
564 | 0 | request.setSelectedProjects( Arrays.asList( projects ) ); |
565 | |
} |
566 | |
|
567 | 0 | if ( commandLine.hasOption( CLIManager.ALSO_MAKE ) && !commandLine.hasOption( CLIManager.ALSO_MAKE_DEPENDENTS ) ) |
568 | |
{ |
569 | 0 | request.setMakeBehavior( ReactorManager.MAKE_MODE ); |
570 | |
} |
571 | 0 | else if ( !commandLine.hasOption( CLIManager.ALSO_MAKE ) |
572 | |
&& commandLine.hasOption( CLIManager.ALSO_MAKE_DEPENDENTS ) ) |
573 | |
{ |
574 | 0 | request.setMakeBehavior( ReactorManager.MAKE_DEPENDENTS_MODE ); |
575 | |
} |
576 | 0 | if ( commandLine.hasOption( CLIManager.ALSO_MAKE ) && commandLine.hasOption( CLIManager.ALSO_MAKE_DEPENDENTS ) ) |
577 | |
{ |
578 | 0 | request.setMakeBehavior( ReactorManager.MAKE_BOTH_MODE ); |
579 | |
} |
580 | 0 | } |
581 | |
|
582 | |
private static Maven createMavenInstance( boolean interactive, Logger logger ) |
583 | |
throws ComponentLookupException |
584 | |
{ |
585 | |
|
586 | 0 | WagonManager wagonManager = (WagonManager) embedder.lookup( WagonManager.ROLE ); |
587 | 0 | if ( interactive ) |
588 | |
{ |
589 | 0 | wagonManager.setDownloadMonitor( new ConsoleDownloadMonitor( logger ) ); |
590 | |
} |
591 | |
else |
592 | |
{ |
593 | 0 | wagonManager.setDownloadMonitor( new BatchModeDownloadMonitor( logger ) ); |
594 | |
} |
595 | |
|
596 | 0 | wagonManager.setInteractive( interactive ); |
597 | |
|
598 | 0 | return (Maven) embedder.lookup( Maven.ROLE ); |
599 | |
} |
600 | |
|
601 | |
private static ArtifactRepository createLocalRepository( Embedder embedder, Settings settings, |
602 | |
CommandLine commandLine ) |
603 | |
throws ComponentLookupException |
604 | |
{ |
605 | |
|
606 | |
|
607 | 0 | ArtifactRepositoryLayout repositoryLayout = |
608 | |
(ArtifactRepositoryLayout) embedder.lookup( ArtifactRepositoryLayout.ROLE, "default" ); |
609 | |
|
610 | 0 | ArtifactRepositoryFactory artifactRepositoryFactory = |
611 | |
(ArtifactRepositoryFactory) embedder.lookup( ArtifactRepositoryFactory.ROLE ); |
612 | |
|
613 | 0 | String url = settings.getLocalRepository(); |
614 | |
|
615 | 0 | if ( !url.startsWith( "file:" ) ) |
616 | |
{ |
617 | 0 | url = "file://" + url; |
618 | |
} |
619 | |
|
620 | 0 | ArtifactRepository localRepository = new DefaultArtifactRepository( "local", url, repositoryLayout ); |
621 | |
|
622 | 0 | boolean snapshotPolicySet = false; |
623 | |
|
624 | 0 | if ( commandLine.hasOption( CLIManager.OFFLINE ) ) |
625 | |
{ |
626 | 0 | settings.setOffline( true ); |
627 | |
|
628 | 0 | snapshotPolicySet = true; |
629 | |
} |
630 | |
|
631 | 0 | if ( !snapshotPolicySet && commandLine.hasOption( CLIManager.UPDATE_SNAPSHOTS ) ) |
632 | |
{ |
633 | 0 | artifactRepositoryFactory.setGlobalUpdatePolicy( ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS ); |
634 | |
} |
635 | |
|
636 | 0 | if ( commandLine.hasOption( CLIManager.CHECKSUM_FAILURE_POLICY ) ) |
637 | |
{ |
638 | 0 | System.out.println( "+ Enabling strict checksum verification on all artifact downloads." ); |
639 | |
|
640 | 0 | artifactRepositoryFactory.setGlobalChecksumPolicy( ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL ); |
641 | |
} |
642 | 0 | else if ( commandLine.hasOption( CLIManager.CHECKSUM_WARNING_POLICY ) ) |
643 | |
{ |
644 | 0 | System.out.println( "+ Disabling strict checksum verification on all artifact downloads." ); |
645 | |
|
646 | 0 | artifactRepositoryFactory.setGlobalChecksumPolicy( ArtifactRepositoryPolicy.CHECKSUM_POLICY_WARN ); |
647 | |
} |
648 | |
|
649 | 0 | return localRepository; |
650 | |
} |
651 | |
|
652 | |
static Properties getBuildProperties() |
653 | |
{ |
654 | 1 | Properties properties = new Properties(); |
655 | 1 | InputStream resourceAsStream = null; |
656 | |
try |
657 | |
{ |
658 | 1 | resourceAsStream = MavenCli.class.getClassLoader().getResourceAsStream( "org/apache/maven/messages/build.properties" ); |
659 | |
|
660 | 1 | if ( resourceAsStream != null ) |
661 | |
{ |
662 | 1 | properties.load( resourceAsStream ); |
663 | |
} |
664 | |
} |
665 | 0 | catch ( IOException e ) |
666 | |
{ |
667 | 0 | System.err.println( "Unable determine version from JAR file: " + e.getMessage() ); |
668 | |
} |
669 | |
finally |
670 | |
{ |
671 | 1 | IOUtil.close( resourceAsStream ); |
672 | 1 | } |
673 | |
|
674 | 1 | return properties; |
675 | |
} |
676 | |
|
677 | |
private static void showVersion() |
678 | |
{ |
679 | 0 | Properties properties = getBuildProperties(); |
680 | |
|
681 | 0 | String timestamp = reduce( properties.getProperty( "timestamp" ) ); |
682 | 0 | String version = reduce( properties.getProperty( "version" ) ); |
683 | 0 | String rev = reduce( properties.getProperty( "buildNumber" ) ); |
684 | |
|
685 | 0 | String msg = "Apache Maven "; |
686 | 0 | msg += ( version != null ? version : "<version unknown>" ); |
687 | 0 | if ( rev != null || timestamp != null ) |
688 | |
{ |
689 | 0 | msg += " ("; |
690 | 0 | msg += ( rev != null ? "r" + rev : "" ); |
691 | 0 | if ( timestamp != null ) |
692 | |
{ |
693 | 0 | SimpleDateFormat fmt = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ssZ" ); |
694 | 0 | String ts = fmt.format( new Date( Long.valueOf( timestamp ).longValue() ) ); |
695 | 0 | msg += ( rev != null ? "; " : "" ) + ts; |
696 | |
} |
697 | 0 | msg += ")"; |
698 | |
} |
699 | |
|
700 | 0 | System.out.println( msg ); |
701 | |
|
702 | 0 | System.out.println( "Java version: " + System.getProperty( "java.version", "<unknown java version>" ) ); |
703 | |
|
704 | 0 | System.out.println( "Java home: " + System.getProperty( "java.home", "<unknown java home>" ) ); |
705 | |
|
706 | 0 | System.out.println( "Default locale: " + Locale.getDefault() + ", platform encoding: " |
707 | |
+ System.getProperty( "file.encoding", "<unknown encoding>" ) ); |
708 | |
|
709 | 0 | System.out.println( "OS name: \"" + Os.OS_NAME + "\" version: \"" + Os.OS_VERSION + |
710 | |
"\" arch: \"" + Os.OS_ARCH + "\" Family: \"" + Os.OS_FAMILY + "\"" ); |
711 | 0 | } |
712 | |
|
713 | |
private static String reduce( String s ) |
714 | |
{ |
715 | 0 | return ( s != null ? ( s.startsWith( "${" ) && s.endsWith( "}" ) ? null : s ) : null ); |
716 | |
} |
717 | |
|
718 | |
|
719 | |
|
720 | |
|
721 | |
|
722 | |
static void populateProperties( CommandLine commandLine, Properties executionProperties, Properties userProperties ) |
723 | |
{ |
724 | |
|
725 | |
|
726 | |
try |
727 | |
{ |
728 | 1 | Properties envVars = CommandLineUtils.getSystemEnvVars(); |
729 | 1 | Iterator i = envVars.entrySet().iterator(); |
730 | 48 | while ( i.hasNext() ) |
731 | |
{ |
732 | 47 | Entry e = (Entry) i.next(); |
733 | 47 | executionProperties.setProperty( "env." + e.getKey().toString(), e.getValue().toString() ); |
734 | 47 | } |
735 | |
} |
736 | 0 | catch ( IOException e ) |
737 | |
{ |
738 | 0 | System.err.println( "Error getting environment vars for profile activation: " + e ); |
739 | 1 | } |
740 | |
|
741 | |
|
742 | |
|
743 | |
|
744 | |
|
745 | |
|
746 | |
|
747 | 1 | if ( commandLine.hasOption( CLIManager.SET_SYSTEM_PROPERTY ) ) |
748 | |
{ |
749 | 1 | String[] defStrs = commandLine.getOptionValues( CLIManager.SET_SYSTEM_PROPERTY ); |
750 | |
|
751 | 1 | if ( defStrs != null ) |
752 | |
{ |
753 | 3 | for ( int i = 0; i < defStrs.length; ++i ) |
754 | |
{ |
755 | 2 | setCliProperty( defStrs[i], userProperties ); |
756 | |
} |
757 | |
} |
758 | |
|
759 | 1 | executionProperties.putAll( userProperties ); |
760 | |
} |
761 | |
|
762 | 1 | executionProperties.putAll( System.getProperties() ); |
763 | 1 | } |
764 | |
|
765 | |
private static void setCliProperty( String property, Properties requestProperties ) |
766 | |
{ |
767 | |
String name; |
768 | |
|
769 | |
String value; |
770 | |
|
771 | 2 | int i = property.indexOf( "=" ); |
772 | |
|
773 | 2 | if ( i <= 0 ) |
774 | |
{ |
775 | 0 | name = property.trim(); |
776 | |
|
777 | 0 | value = "true"; |
778 | |
} |
779 | |
else |
780 | |
{ |
781 | 2 | name = property.substring( 0, i ).trim(); |
782 | |
|
783 | 2 | value = property.substring( i + 1 ).trim(); |
784 | |
} |
785 | |
|
786 | 2 | requestProperties.setProperty( name, value ); |
787 | |
|
788 | |
|
789 | |
|
790 | |
|
791 | |
|
792 | |
|
793 | 2 | System.setProperty( name, value ); |
794 | 2 | } |
795 | |
} |