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 com.google.inject.AbstractModule;
23 import org.apache.commons.cli.CommandLine;
24 import org.apache.commons.cli.Option;
25 import org.apache.commons.cli.ParseException;
26 import org.apache.commons.cli.UnrecognizedOptionException;
27 import org.apache.maven.BuildAbort;
28 import org.apache.maven.InternalErrorException;
29 import org.apache.maven.Maven;
30 import org.apache.maven.building.FileSource;
31 import org.apache.maven.building.Problem;
32 import org.apache.maven.building.Source;
33 import org.apache.maven.cli.configuration.ConfigurationProcessor;
34 import org.apache.maven.cli.configuration.SettingsXmlConfigurationProcessor;
35 import org.apache.maven.cli.event.DefaultEventSpyContext;
36 import org.apache.maven.cli.event.ExecutionEventLogger;
37 import org.apache.maven.cli.internal.BootstrapCoreExtensionManager;
38 import org.apache.maven.cli.internal.extension.model.CoreExtension;
39 import org.apache.maven.cli.internal.extension.model.io.xpp3.CoreExtensionsXpp3Reader;
40 import org.apache.maven.cli.logging.Slf4jConfiguration;
41 import org.apache.maven.cli.logging.Slf4jConfigurationFactory;
42 import org.apache.maven.cli.logging.Slf4jLoggerManager;
43 import org.apache.maven.cli.logging.Slf4jStdoutLogger;
44 import org.apache.maven.cli.transfer.ConsoleMavenTransferListener;
45 import org.apache.maven.cli.transfer.QuietMavenTransferListener;
46 import org.apache.maven.cli.transfer.Slf4jMavenTransferListener;
47 import org.apache.maven.eventspy.internal.EventSpyDispatcher;
48 import org.apache.maven.exception.DefaultExceptionHandler;
49 import org.apache.maven.exception.ExceptionHandler;
50 import org.apache.maven.exception.ExceptionSummary;
51 import org.apache.maven.execution.DefaultMavenExecutionRequest;
52 import org.apache.maven.execution.ExecutionListener;
53 import org.apache.maven.execution.MavenExecutionRequest;
54 import org.apache.maven.execution.MavenExecutionRequestPopulationException;
55 import org.apache.maven.execution.MavenExecutionRequestPopulator;
56 import org.apache.maven.execution.MavenExecutionResult;
57 import org.apache.maven.execution.scope.internal.MojoExecutionScopeModule;
58 import org.apache.maven.extension.internal.CoreExports;
59 import org.apache.maven.extension.internal.CoreExtensionEntry;
60 import org.apache.maven.lifecycle.LifecycleExecutionException;
61 import org.apache.maven.model.building.ModelProcessor;
62 import org.apache.maven.project.MavenProject;
63 import org.apache.maven.properties.internal.EnvironmentUtils;
64 import org.apache.maven.properties.internal.SystemProperties;
65 import org.apache.maven.session.scope.internal.SessionScopeModule;
66 import org.apache.maven.shared.utils.logging.MessageBuilder;
67 import org.apache.maven.shared.utils.logging.MessageUtils;
68 import org.apache.maven.toolchain.building.DefaultToolchainsBuildingRequest;
69 import org.apache.maven.toolchain.building.ToolchainsBuilder;
70 import org.apache.maven.toolchain.building.ToolchainsBuildingResult;
71 import org.codehaus.plexus.ContainerConfiguration;
72 import org.codehaus.plexus.DefaultContainerConfiguration;
73 import org.codehaus.plexus.DefaultPlexusContainer;
74 import org.codehaus.plexus.PlexusConstants;
75 import org.codehaus.plexus.PlexusContainer;
76 import org.codehaus.plexus.classworlds.ClassWorld;
77 import org.codehaus.plexus.classworlds.realm.ClassRealm;
78 import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
79 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
80 import org.codehaus.plexus.logging.LoggerManager;
81 import org.codehaus.plexus.util.StringUtils;
82 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
83 import org.eclipse.aether.transfer.TransferListener;
84 import org.slf4j.ILoggerFactory;
85 import org.slf4j.Logger;
86 import org.slf4j.LoggerFactory;
87 import org.sonatype.plexus.components.cipher.DefaultPlexusCipher;
88 import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;
89 import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
90 import org.sonatype.plexus.components.sec.dispatcher.SecUtil;
91 import org.sonatype.plexus.components.sec.dispatcher.model.SettingsSecurity;
92
93 import java.io.BufferedInputStream;
94 import java.io.Console;
95 import java.io.File;
96 import java.io.FileInputStream;
97 import java.io.FileNotFoundException;
98 import java.io.FileOutputStream;
99 import java.io.IOException;
100 import java.io.InputStream;
101 import java.io.PrintStream;
102 import java.nio.file.Files;
103 import java.util.ArrayList;
104 import java.util.Collections;
105 import java.util.HashSet;
106 import java.util.LinkedHashMap;
107 import java.util.List;
108 import java.util.Map;
109 import java.util.Map.Entry;
110 import java.util.Properties;
111 import java.util.Set;
112 import java.util.StringTokenizer;
113 import java.util.regex.Matcher;
114 import java.util.regex.Pattern;
115
116 import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
117
118
119
120
121
122
123 public class MavenCli
124 {
125 public static final String LOCAL_REPO_PROPERTY = "maven.repo.local";
126
127 public static final String MULTIMODULE_PROJECT_DIRECTORY = "maven.multiModuleProjectDirectory";
128
129 public static final String USER_HOME = System.getProperty( "user.home" );
130
131 public static final File USER_MAVEN_CONFIGURATION_HOME = new File( USER_HOME, ".m2" );
132
133 public static final File DEFAULT_USER_TOOLCHAINS_FILE = new File( USER_MAVEN_CONFIGURATION_HOME, "toolchains.xml" );
134
135 public static final File DEFAULT_GLOBAL_TOOLCHAINS_FILE =
136 new File( System.getProperty( "maven.conf" ), "toolchains.xml" );
137
138 private static final String EXT_CLASS_PATH = "maven.ext.class.path";
139
140 private static final String EXTENSIONS_FILENAME = ".mvn/extensions.xml";
141
142 private static final String MVN_MAVEN_CONFIG = ".mvn/maven.config";
143
144 public static final String STYLE_COLOR_PROPERTY = "style.color";
145
146 private ClassWorld classWorld;
147
148 private LoggerManager plexusLoggerManager;
149
150 private ILoggerFactory slf4jLoggerFactory;
151
152 private Logger slf4jLogger;
153
154 private EventSpyDispatcher eventSpyDispatcher;
155
156 private ModelProcessor modelProcessor;
157
158 private Maven maven;
159
160 private MavenExecutionRequestPopulator executionRequestPopulator;
161
162 private ToolchainsBuilder toolchainsBuilder;
163
164 private DefaultSecDispatcher dispatcher;
165
166 private Map<String, ConfigurationProcessor> configurationProcessors;
167
168 public MavenCli()
169 {
170 this( null );
171 }
172
173
174 public MavenCli( ClassWorld classWorld )
175 {
176 this.classWorld = classWorld;
177 }
178
179 public static void main( String[] args )
180 {
181 int result = main( args, null );
182
183 System.exit( result );
184 }
185
186 public static int main( String[] args, ClassWorld classWorld )
187 {
188 MavenCli cli = new MavenCli();
189
190 MessageUtils.systemInstall();
191 MessageUtils.registerShutdownHook();
192 int result = cli.doMain( new CliRequest( args, classWorld ) );
193 MessageUtils.systemUninstall();
194
195 return result;
196 }
197
198
199 public static int doMain( String[] args, ClassWorld classWorld )
200 {
201 MavenCli cli = new MavenCli();
202 return cli.doMain( new CliRequest( args, classWorld ) );
203 }
204
205
206
207
208
209
210 public int doMain( String[] args, String workingDirectory, PrintStream stdout, PrintStream stderr )
211 {
212 PrintStream oldout = System.out;
213 PrintStream olderr = System.err;
214
215 final Set<String> realms;
216 if ( classWorld != null )
217 {
218 realms = new HashSet<>();
219 for ( ClassRealm realm : classWorld.getRealms() )
220 {
221 realms.add( realm.getId() );
222 }
223 }
224 else
225 {
226 realms = Collections.emptySet();
227 }
228
229 try
230 {
231 if ( stdout != null )
232 {
233 System.setOut( stdout );
234 }
235 if ( stderr != null )
236 {
237 System.setErr( stderr );
238 }
239
240 CliRequest cliRequest = new CliRequest( args, classWorld );
241 cliRequest.workingDirectory = workingDirectory;
242
243 return doMain( cliRequest );
244 }
245 finally
246 {
247 if ( classWorld != null )
248 {
249 for ( ClassRealm realm : new ArrayList<>( classWorld.getRealms() ) )
250 {
251 String realmId = realm.getId();
252 if ( !realms.contains( realmId ) )
253 {
254 try
255 {
256 classWorld.disposeRealm( realmId );
257 }
258 catch ( NoSuchRealmException ignored )
259 {
260
261 }
262 }
263 }
264 }
265 System.setOut( oldout );
266 System.setErr( olderr );
267 }
268 }
269
270
271 public int doMain( CliRequest cliRequest )
272 {
273 PlexusContainer localContainer = null;
274 try
275 {
276 initialize( cliRequest );
277 cli( cliRequest );
278 properties( cliRequest );
279 logging( cliRequest );
280 version( cliRequest );
281 localContainer = container( cliRequest );
282 commands( cliRequest );
283 configure( cliRequest );
284 toolchains( cliRequest );
285 populateRequest( cliRequest );
286 encryption( cliRequest );
287 repository( cliRequest );
288 return execute( cliRequest );
289 }
290 catch ( ExitException e )
291 {
292 return e.exitCode;
293 }
294 catch ( UnrecognizedOptionException e )
295 {
296
297 return 1;
298 }
299 catch ( BuildAbort e )
300 {
301 CLIReportingUtils.showError( slf4jLogger, "ABORTED", e, cliRequest.showErrors );
302
303 return 2;
304 }
305 catch ( Exception e )
306 {
307 CLIReportingUtils.showError( slf4jLogger, "Error executing Maven.", e, cliRequest.showErrors );
308
309 return 1;
310 }
311 finally
312 {
313 if ( localContainer != null )
314 {
315 localContainer.dispose();
316 }
317 }
318 }
319
320 void initialize( CliRequest cliRequest )
321 throws ExitException
322 {
323 if ( cliRequest.workingDirectory == null )
324 {
325 cliRequest.workingDirectory = System.getProperty( "user.dir" );
326 }
327
328 if ( cliRequest.multiModuleProjectDirectory == null )
329 {
330 String basedirProperty = System.getProperty( MULTIMODULE_PROJECT_DIRECTORY );
331 if ( basedirProperty == null )
332 {
333 System.err.format(
334 "-D%s system property is not set.", MULTIMODULE_PROJECT_DIRECTORY );
335 throw new ExitException( 1 );
336 }
337 File basedir = basedirProperty != null ? new File( basedirProperty ) : new File( "" );
338 try
339 {
340 cliRequest.multiModuleProjectDirectory = basedir.getCanonicalFile();
341 }
342 catch ( IOException e )
343 {
344 cliRequest.multiModuleProjectDirectory = basedir.getAbsoluteFile();
345 }
346 }
347
348
349
350
351
352 String mavenHome = System.getProperty( "maven.home" );
353
354 if ( mavenHome != null )
355 {
356 System.setProperty( "maven.home", new File( mavenHome ).getAbsolutePath() );
357 }
358 }
359
360 void cli( CliRequest cliRequest )
361 throws Exception
362 {
363
364
365
366
367 slf4jLogger = new Slf4jStdoutLogger();
368
369 CLIManager cliManager = new CLIManager();
370
371 List<String> args = new ArrayList<>();
372 CommandLine mavenConfig = null;
373 try
374 {
375 File configFile = new File( cliRequest.multiModuleProjectDirectory, MVN_MAVEN_CONFIG );
376
377 if ( configFile.isFile() )
378 {
379 for ( String arg : new String( Files.readAllBytes( configFile.toPath() ) ).split( "\\s+" ) )
380 {
381 if ( !arg.isEmpty() )
382 {
383 args.add( arg );
384 }
385 }
386
387 mavenConfig = cliManager.parse( args.toArray( new String[args.size()] ) );
388 List<?> unrecongized = mavenConfig.getArgList();
389 if ( !unrecongized.isEmpty() )
390 {
391 throw new ParseException( "Unrecognized maven.config entries: " + unrecongized );
392 }
393 }
394 }
395 catch ( ParseException e )
396 {
397 System.err.println( "Unable to parse maven.config: " + e.getMessage() );
398 cliManager.displayHelp( System.out );
399 throw e;
400 }
401
402 try
403 {
404 if ( mavenConfig == null )
405 {
406 cliRequest.commandLine = cliManager.parse( cliRequest.args );
407 }
408 else
409 {
410 cliRequest.commandLine = cliMerge( cliManager.parse( cliRequest.args ), mavenConfig );
411 }
412 }
413 catch ( ParseException e )
414 {
415 System.err.println( "Unable to parse command line options: " + e.getMessage() );
416 cliManager.displayHelp( System.out );
417 throw e;
418 }
419
420 if ( cliRequest.commandLine.hasOption( CLIManager.HELP ) )
421 {
422 cliManager.displayHelp( System.out );
423 throw new ExitException( 0 );
424 }
425
426 if ( cliRequest.commandLine.hasOption( CLIManager.VERSION ) )
427 {
428 System.out.println( CLIReportingUtils.showVersion() );
429 throw new ExitException( 0 );
430 }
431 }
432
433 private CommandLine cliMerge( CommandLine mavenArgs, CommandLine mavenConfig )
434 {
435 CommandLine.Builder commandLineBuilder = new CommandLine.Builder();
436
437
438 for ( String arg : mavenArgs.getArgs() )
439 {
440 commandLineBuilder.addArg( arg );
441 }
442 for ( String arg : mavenConfig.getArgs() )
443 {
444 commandLineBuilder.addArg( arg );
445 }
446
447
448 List<Option> setPropertyOptions = new ArrayList<>();
449 for ( Option opt : mavenArgs.getOptions() )
450 {
451 if ( String.valueOf( CLIManager.SET_SYSTEM_PROPERTY ).equals( opt.getOpt() ) )
452 {
453 setPropertyOptions.add( opt );
454 }
455 else
456 {
457 commandLineBuilder.addOption( opt );
458 }
459 }
460 for ( Option opt : mavenConfig.getOptions() )
461 {
462 commandLineBuilder.addOption( opt );
463 }
464
465 for ( Option opt : setPropertyOptions )
466 {
467 commandLineBuilder.addOption( opt );
468 }
469 return commandLineBuilder.build();
470 }
471
472
473
474
475 void logging( CliRequest cliRequest )
476 {
477
478 cliRequest.debug = cliRequest.commandLine.hasOption( CLIManager.DEBUG );
479 cliRequest.quiet = !cliRequest.debug && cliRequest.commandLine.hasOption( CLIManager.QUIET );
480 cliRequest.showErrors = cliRequest.debug || cliRequest.commandLine.hasOption( CLIManager.ERRORS );
481
482 slf4jLoggerFactory = LoggerFactory.getILoggerFactory();
483 Slf4jConfiguration slf4jConfiguration = Slf4jConfigurationFactory.getConfiguration( slf4jLoggerFactory );
484
485 if ( cliRequest.debug )
486 {
487 cliRequest.request.setLoggingLevel( MavenExecutionRequest.LOGGING_LEVEL_DEBUG );
488 slf4jConfiguration.setRootLoggerLevel( Slf4jConfiguration.Level.DEBUG );
489 }
490 else if ( cliRequest.quiet )
491 {
492 cliRequest.request.setLoggingLevel( MavenExecutionRequest.LOGGING_LEVEL_ERROR );
493 slf4jConfiguration.setRootLoggerLevel( Slf4jConfiguration.Level.ERROR );
494 }
495
496
497
498
499 String styleColor = cliRequest.getUserProperties().getProperty( STYLE_COLOR_PROPERTY, "auto" );
500 if ( "always".equals( styleColor ) )
501 {
502 MessageUtils.setColorEnabled( true );
503 }
504 else if ( "never".equals( styleColor ) )
505 {
506 MessageUtils.setColorEnabled( false );
507 }
508 else if ( !"auto".equals( styleColor ) )
509 {
510 throw new IllegalArgumentException( "Invalid color configuration option [" + styleColor
511 + "]. Supported values are (auto|always|never)." );
512 }
513 else if ( cliRequest.commandLine.hasOption( CLIManager.BATCH_MODE )
514 || cliRequest.commandLine.hasOption( CLIManager.LOG_FILE ) )
515 {
516 MessageUtils.setColorEnabled( false );
517 }
518
519
520 if ( cliRequest.commandLine.hasOption( CLIManager.LOG_FILE ) )
521 {
522 File logFile = new File( cliRequest.commandLine.getOptionValue( CLIManager.LOG_FILE ) );
523 logFile = resolveFile( logFile, cliRequest.workingDirectory );
524
525
526 try
527 {
528 PrintStream ps = new PrintStream( new FileOutputStream( logFile ) );
529 System.setOut( ps );
530 System.setErr( ps );
531 }
532 catch ( FileNotFoundException e )
533 {
534
535
536
537 }
538 }
539
540 slf4jConfiguration.activate();
541
542 plexusLoggerManager = new Slf4jLoggerManager();
543 slf4jLogger = slf4jLoggerFactory.getLogger( this.getClass().getName() );
544 }
545
546 private void version( CliRequest cliRequest )
547 {
548 if ( cliRequest.debug || cliRequest.commandLine.hasOption( CLIManager.SHOW_VERSION ) )
549 {
550 System.out.println( CLIReportingUtils.showVersion() );
551 }
552 }
553
554 private void commands( CliRequest cliRequest )
555 {
556 if ( cliRequest.showErrors )
557 {
558 slf4jLogger.info( "Error stacktraces are turned on." );
559 }
560
561 if ( MavenExecutionRequest.CHECKSUM_POLICY_WARN.equals( cliRequest.request.getGlobalChecksumPolicy() ) )
562 {
563 slf4jLogger.info( "Disabling strict checksum verification on all artifact downloads." );
564 }
565 else if ( MavenExecutionRequest.CHECKSUM_POLICY_FAIL.equals( cliRequest.request.getGlobalChecksumPolicy() ) )
566 {
567 slf4jLogger.info( "Enabling strict checksum verification on all artifact downloads." );
568 }
569
570 if ( slf4jLogger.isDebugEnabled() )
571 {
572 slf4jLogger.debug( "Message scheme: " + ( MessageUtils.isColorEnabled() ? "color" : "plain" ) );
573 if ( MessageUtils.isColorEnabled() )
574 {
575 MessageBuilder buff = MessageUtils.buffer();
576 buff.a( "Message styles: " );
577 buff.a( MessageUtils.level().debug( "debug" ) ).a( ' ' );
578 buff.a( MessageUtils.level().info( "info" ) ).a( ' ' );
579 buff.a( MessageUtils.level().warning( "warning" ) ).a( ' ' );
580 buff.a( MessageUtils.level().error( "error" ) ).a( ' ' );
581
582 buff.success( "success" ).a( ' ' );
583 buff.failure( "failure" ).a( ' ' );
584 buff.strong( "strong" ).a( ' ' );
585 buff.mojo( "mojo" ).a( ' ' );
586 buff.project( "project" );
587 slf4jLogger.debug( buff.toString() );
588 }
589 }
590 }
591
592
593
594 void properties( CliRequest cliRequest )
595 {
596 populateProperties( cliRequest.commandLine, cliRequest.systemProperties, cliRequest.userProperties );
597 }
598
599 private PlexusContainer container( CliRequest cliRequest )
600 throws Exception
601 {
602 if ( cliRequest.classWorld == null )
603 {
604 cliRequest.classWorld = new ClassWorld( "plexus.core", Thread.currentThread().getContextClassLoader() );
605 }
606
607 ClassRealm coreRealm = cliRequest.classWorld.getClassRealm( "plexus.core" );
608 if ( coreRealm == null )
609 {
610 coreRealm = cliRequest.classWorld.getRealms().iterator().next();
611 }
612
613 List<File> extClassPath = parseExtClasspath( cliRequest );
614
615 CoreExtensionEntry coreEntry = CoreExtensionEntry.discoverFrom( coreRealm );
616 List<CoreExtensionEntry> extensions =
617 loadCoreExtensions( cliRequest, coreRealm, coreEntry.getExportedArtifacts() );
618
619 ClassRealm containerRealm = setupContainerRealm( cliRequest.classWorld, coreRealm, extClassPath, extensions );
620
621 ContainerConfiguration cc = new DefaultContainerConfiguration().setClassWorld( cliRequest.classWorld )
622 .setRealm( containerRealm ).setClassPathScanning( PlexusConstants.SCANNING_INDEX ).setAutoWiring( true )
623 .setJSR250Lifecycle( true ).setName( "maven" );
624
625 Set<String> exportedArtifacts = new HashSet<>( coreEntry.getExportedArtifacts() );
626 Set<String> exportedPackages = new HashSet<>( coreEntry.getExportedPackages() );
627 for ( CoreExtensionEntry extension : extensions )
628 {
629 exportedArtifacts.addAll( extension.getExportedArtifacts() );
630 exportedPackages.addAll( extension.getExportedPackages() );
631 }
632
633 final CoreExports exports = new CoreExports( containerRealm, exportedArtifacts, exportedPackages );
634
635 DefaultPlexusContainer container = new DefaultPlexusContainer( cc, new AbstractModule()
636 {
637 @Override
638 protected void configure()
639 {
640 bind( ILoggerFactory.class ).toInstance( slf4jLoggerFactory );
641 bind( CoreExports.class ).toInstance( exports );
642 }
643 } );
644
645
646 container.setLookupRealm( null );
647 Thread.currentThread().setContextClassLoader( container.getContainerRealm() );
648
649 container.setLoggerManager( plexusLoggerManager );
650
651 for ( CoreExtensionEntry extension : extensions )
652 {
653 container.discoverComponents( extension.getClassRealm(), new SessionScopeModule( container ),
654 new MojoExecutionScopeModule( container ) );
655 }
656
657 customizeContainer( container );
658
659 container.getLoggerManager().setThresholds( cliRequest.request.getLoggingLevel() );
660
661 eventSpyDispatcher = container.lookup( EventSpyDispatcher.class );
662
663 DefaultEventSpyContext eventSpyContext = new DefaultEventSpyContext();
664 Map<String, Object> data = eventSpyContext.getData();
665 data.put( "plexus", container );
666 data.put( "workingDirectory", cliRequest.workingDirectory );
667 data.put( "systemProperties", cliRequest.systemProperties );
668 data.put( "userProperties", cliRequest.userProperties );
669 data.put( "versionProperties", CLIReportingUtils.getBuildProperties() );
670 eventSpyDispatcher.init( eventSpyContext );
671
672
673 slf4jLogger = slf4jLoggerFactory.getLogger( this.getClass().getName() );
674
675 maven = container.lookup( Maven.class );
676
677 executionRequestPopulator = container.lookup( MavenExecutionRequestPopulator.class );
678
679 modelProcessor = createModelProcessor( container );
680
681 configurationProcessors = container.lookupMap( ConfigurationProcessor.class );
682
683 toolchainsBuilder = container.lookup( ToolchainsBuilder.class );
684
685 dispatcher = (DefaultSecDispatcher) container.lookup( SecDispatcher.class, "maven" );
686
687 return container;
688 }
689
690 private List<CoreExtensionEntry> loadCoreExtensions( CliRequest cliRequest, ClassRealm containerRealm,
691 Set<String> providedArtifacts )
692 {
693 if ( cliRequest.multiModuleProjectDirectory == null )
694 {
695 return Collections.emptyList();
696 }
697
698 File extensionsFile = new File( cliRequest.multiModuleProjectDirectory, EXTENSIONS_FILENAME );
699 if ( !extensionsFile.isFile() )
700 {
701 return Collections.emptyList();
702 }
703
704 try
705 {
706 List<CoreExtension> extensions = readCoreExtensionsDescriptor( extensionsFile );
707 if ( extensions.isEmpty() )
708 {
709 return Collections.emptyList();
710 }
711
712 ContainerConfiguration cc = new DefaultContainerConfiguration()
713 .setClassWorld( cliRequest.classWorld )
714 .setRealm( containerRealm )
715 .setClassPathScanning( PlexusConstants.SCANNING_INDEX )
716 .setAutoWiring( true )
717 .setJSR250Lifecycle( true )
718 .setName( "maven" );
719
720 DefaultPlexusContainer container = new DefaultPlexusContainer( cc, new AbstractModule()
721 {
722 @Override
723 protected void configure()
724 {
725 bind( ILoggerFactory.class ).toInstance( slf4jLoggerFactory );
726 }
727 } );
728
729 try
730 {
731 container.setLookupRealm( null );
732
733 container.setLoggerManager( plexusLoggerManager );
734
735 container.getLoggerManager().setThresholds( cliRequest.request.getLoggingLevel() );
736
737 Thread.currentThread().setContextClassLoader( container.getContainerRealm() );
738
739 executionRequestPopulator = container.lookup( MavenExecutionRequestPopulator.class );
740
741 configurationProcessors = container.lookupMap( ConfigurationProcessor.class );
742
743 configure( cliRequest );
744
745 MavenExecutionRequest request = DefaultMavenExecutionRequest.copy( cliRequest.request );
746
747 request = populateRequest( cliRequest, request );
748
749 request = executionRequestPopulator.populateDefaults( request );
750
751 BootstrapCoreExtensionManager resolver = container.lookup( BootstrapCoreExtensionManager.class );
752
753 return resolver.loadCoreExtensions( request, providedArtifacts, extensions );
754 }
755 finally
756 {
757 executionRequestPopulator = null;
758 container.dispose();
759 }
760 }
761 catch ( RuntimeException e )
762 {
763
764 throw e;
765 }
766 catch ( Exception e )
767 {
768 slf4jLogger.warn( "Failed to read extensions descriptor " + extensionsFile + ": " + e.getMessage() );
769 }
770 return Collections.emptyList();
771 }
772
773 private List<CoreExtension> readCoreExtensionsDescriptor( File extensionsFile )
774 throws IOException, XmlPullParserException
775 {
776 CoreExtensionsXpp3Reader parser = new CoreExtensionsXpp3Reader();
777
778 try ( InputStream is = new BufferedInputStream( new FileInputStream( extensionsFile ) ) )
779 {
780
781 return parser.read( is ).getExtensions();
782 }
783
784 }
785
786 private ClassRealm setupContainerRealm( ClassWorld classWorld, ClassRealm coreRealm, List<File> extClassPath,
787 List<CoreExtensionEntry> extensions )
788 throws Exception
789 {
790 if ( !extClassPath.isEmpty() || !extensions.isEmpty() )
791 {
792 ClassRealm extRealm = classWorld.newRealm( "maven.ext", null );
793
794 extRealm.setParentRealm( coreRealm );
795
796 slf4jLogger.debug( "Populating class realm " + extRealm.getId() );
797
798 for ( File file : extClassPath )
799 {
800 slf4jLogger.debug( " Included " + file );
801
802 extRealm.addURL( file.toURI().toURL() );
803 }
804
805 for ( CoreExtensionEntry entry : reverse( extensions ) )
806 {
807 Set<String> exportedPackages = entry.getExportedPackages();
808 ClassRealm realm = entry.getClassRealm();
809 for ( String exportedPackage : exportedPackages )
810 {
811 extRealm.importFrom( realm, exportedPackage );
812 }
813 if ( exportedPackages.isEmpty() )
814 {
815
816 extRealm.importFrom( realm, realm.getId() );
817 }
818 }
819
820 return extRealm;
821 }
822
823 return coreRealm;
824 }
825
826 private static <T> List<T> reverse( List<T> list )
827 {
828 List<T> copy = new ArrayList<>( list );
829 Collections.reverse( copy );
830 return copy;
831 }
832
833 private List<File> parseExtClasspath( CliRequest cliRequest )
834 {
835 String extClassPath = cliRequest.userProperties.getProperty( EXT_CLASS_PATH );
836 if ( extClassPath == null )
837 {
838 extClassPath = cliRequest.systemProperties.getProperty( EXT_CLASS_PATH );
839 }
840
841 List<File> jars = new ArrayList<>();
842
843 if ( StringUtils.isNotEmpty( extClassPath ) )
844 {
845 for ( String jar : StringUtils.split( extClassPath, File.pathSeparator ) )
846 {
847 File file = resolveFile( new File( jar ), cliRequest.workingDirectory );
848
849 slf4jLogger.debug( " Included " + file );
850
851 jars.add( file );
852 }
853 }
854
855 return jars;
856 }
857
858
859
860
861 private void encryption( CliRequest cliRequest )
862 throws Exception
863 {
864 if ( cliRequest.commandLine.hasOption( CLIManager.ENCRYPT_MASTER_PASSWORD ) )
865 {
866 String passwd = cliRequest.commandLine.getOptionValue( CLIManager.ENCRYPT_MASTER_PASSWORD );
867
868 if ( passwd == null )
869 {
870 Console cons = System.console();
871 char[] password = ( cons == null ) ? null : cons.readPassword( "Master password: " );
872 if ( password != null )
873 {
874
875 passwd = String.copyValueOf( password );
876
877
878 java.util.Arrays.fill( password, ' ' );
879 }
880 }
881
882 DefaultPlexusCipher cipher = new DefaultPlexusCipher();
883
884 System.out.println(
885 cipher.encryptAndDecorate( passwd, DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION ) );
886
887 throw new ExitException( 0 );
888 }
889 else if ( cliRequest.commandLine.hasOption( CLIManager.ENCRYPT_PASSWORD ) )
890 {
891 String passwd = cliRequest.commandLine.getOptionValue( CLIManager.ENCRYPT_PASSWORD );
892
893 if ( passwd == null )
894 {
895 Console cons = System.console();
896 char[] password = ( cons == null ) ? null : cons.readPassword( "Password: " );
897 if ( password != null )
898 {
899
900 passwd = String.copyValueOf( password );
901
902
903 java.util.Arrays.fill( password, ' ' );
904 }
905 }
906
907 String configurationFile = dispatcher.getConfigurationFile();
908
909 if ( configurationFile.startsWith( "~" ) )
910 {
911 configurationFile = System.getProperty( "user.home" ) + configurationFile.substring( 1 );
912 }
913
914 String file = System.getProperty( DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION, configurationFile );
915
916 String master = null;
917
918 SettingsSecurity sec = SecUtil.read( file, true );
919 if ( sec != null )
920 {
921 master = sec.getMaster();
922 }
923
924 if ( master == null )
925 {
926 throw new IllegalStateException( "Master password is not set in the setting security file: " + file );
927 }
928
929 DefaultPlexusCipher cipher = new DefaultPlexusCipher();
930 String masterPasswd = cipher.decryptDecorated( master, DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION );
931 System.out.println( cipher.encryptAndDecorate( passwd, masterPasswd ) );
932
933 throw new ExitException( 0 );
934 }
935 }
936
937 private void repository( CliRequest cliRequest )
938 throws Exception
939 {
940 if ( cliRequest.commandLine.hasOption( CLIManager.LEGACY_LOCAL_REPOSITORY ) || Boolean.getBoolean(
941 "maven.legacyLocalRepo" ) )
942 {
943 cliRequest.request.setUseLegacyLocalRepository( true );
944 }
945 }
946
947 private int execute( CliRequest cliRequest )
948 throws MavenExecutionRequestPopulationException
949 {
950 MavenExecutionRequest request = executionRequestPopulator.populateDefaults( cliRequest.request );
951
952 eventSpyDispatcher.onEvent( request );
953
954 MavenExecutionResult result = maven.execute( request );
955
956 eventSpyDispatcher.onEvent( result );
957
958 eventSpyDispatcher.close();
959
960 if ( result.hasExceptions() )
961 {
962 ExceptionHandler handler = new DefaultExceptionHandler();
963
964 Map<String, String> references = new LinkedHashMap<>();
965
966 MavenProject project = null;
967
968 for ( Throwable exception : result.getExceptions() )
969 {
970 ExceptionSummary summary = handler.handleException( exception );
971
972 logSummary( summary, references, "", cliRequest.showErrors );
973
974 if ( project == null && exception instanceof LifecycleExecutionException )
975 {
976 project = ( (LifecycleExecutionException) exception ).getProject();
977 }
978 }
979
980 slf4jLogger.error( "" );
981
982 if ( !cliRequest.showErrors )
983 {
984 slf4jLogger.error( "To see the full stack trace of the errors, re-run Maven with the "
985 + buffer().strong( "-e" ) + " switch." );
986 }
987 if ( !slf4jLogger.isDebugEnabled() )
988 {
989 slf4jLogger.error( "Re-run Maven using the " + buffer().strong( "-X" )
990 + " switch to enable full debug logging." );
991 }
992
993 if ( !references.isEmpty() )
994 {
995 slf4jLogger.error( "" );
996 slf4jLogger.error( "For more information about the errors and possible solutions"
997 + ", please read the following articles:" );
998
999 for ( Map.Entry<String, String> entry : references.entrySet() )
1000 {
1001 slf4jLogger.error( buffer().strong( entry.getValue() ) + " " + entry.getKey() );
1002 }
1003 }
1004
1005 if ( project != null && !project.equals( result.getTopologicallySortedProjects().get( 0 ) ) )
1006 {
1007 slf4jLogger.error( "" );
1008 slf4jLogger.error( "After correcting the problems, you can resume the build with the command" );
1009 slf4jLogger.error( buffer().a( " " ).strong( "mvn <goals> -rf "
1010 + getResumeFrom( result.getTopologicallySortedProjects(), project ) ).toString() );
1011 }
1012
1013 if ( MavenExecutionRequest.REACTOR_FAIL_NEVER.equals( cliRequest.request.getReactorFailureBehavior() ) )
1014 {
1015 slf4jLogger.info( "Build failures were ignored." );
1016
1017 return 0;
1018 }
1019 else
1020 {
1021 return 1;
1022 }
1023 }
1024 else
1025 {
1026 return 0;
1027 }
1028 }
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046 private String getResumeFrom( List<MavenProject> mavenProjects, MavenProject failedProject )
1047 {
1048 for ( MavenProject buildProject : mavenProjects )
1049 {
1050 if ( failedProject.getArtifactId().equals( buildProject.getArtifactId() ) && !failedProject.equals(
1051 buildProject ) )
1052 {
1053 return failedProject.getGroupId() + ":" + failedProject.getArtifactId();
1054 }
1055 }
1056 return ":" + failedProject.getArtifactId();
1057 }
1058
1059 private void logSummary( ExceptionSummary summary, Map<String, String> references, String indent,
1060 boolean showErrors )
1061 {
1062 String referenceKey = "";
1063
1064 if ( StringUtils.isNotEmpty( summary.getReference() ) )
1065 {
1066 referenceKey = references.get( summary.getReference() );
1067 if ( referenceKey == null )
1068 {
1069 referenceKey = "[Help " + ( references.size() + 1 ) + "]";
1070 references.put( summary.getReference(), referenceKey );
1071 }
1072 }
1073
1074 String msg = summary.getMessage();
1075
1076 if ( StringUtils.isNotEmpty( referenceKey ) )
1077 {
1078 if ( msg.indexOf( '\n' ) < 0 )
1079 {
1080 msg += " -> " + buffer().strong( referenceKey );
1081 }
1082 else
1083 {
1084 msg += "\n-> " + buffer().strong( referenceKey );
1085 }
1086 }
1087
1088 String[] lines = msg.split( "(\r\n)|(\r)|(\n)" );
1089 String currentColor = "";
1090
1091 for ( int i = 0; i < lines.length; i++ )
1092 {
1093
1094 String line = currentColor + lines[i];
1095
1096
1097 Matcher matcher = LAST_ANSI_SEQUENCE.matcher( line );
1098 String nextColor = "";
1099 if ( matcher.find() )
1100 {
1101 nextColor = matcher.group( 1 );
1102 if ( ANSI_RESET.equals( nextColor ) )
1103 {
1104
1105 nextColor = "";
1106 }
1107 }
1108
1109
1110 line = indent + line + ( "".equals( nextColor ) ? "" : ANSI_RESET );
1111
1112 if ( ( i == lines.length - 1 ) && ( showErrors
1113 || ( summary.getException() instanceof InternalErrorException ) ) )
1114 {
1115 slf4jLogger.error( line, summary.getException() );
1116 }
1117 else
1118 {
1119 slf4jLogger.error( line );
1120 }
1121
1122 currentColor = nextColor;
1123 }
1124
1125 indent += " ";
1126
1127 for ( ExceptionSummary child : summary.getChildren() )
1128 {
1129 logSummary( child, references, indent, showErrors );
1130 }
1131 }
1132
1133 private static final Pattern LAST_ANSI_SEQUENCE = Pattern.compile( "(\u001B\\[[;\\d]*[ -/]*[@-~])[^\u001B]*$" );
1134
1135 private static final String ANSI_RESET = "\u001B\u005Bm";
1136
1137 private void configure( CliRequest cliRequest )
1138 throws Exception
1139 {
1140
1141
1142
1143
1144
1145
1146 cliRequest.request.setEventSpyDispatcher( eventSpyDispatcher );
1147
1148
1149
1150
1151
1152
1153
1154
1155 int userSuppliedConfigurationProcessorCount = configurationProcessors.size() - 1;
1156
1157 if ( userSuppliedConfigurationProcessorCount == 0 )
1158 {
1159
1160
1161
1162
1163 configurationProcessors.get( SettingsXmlConfigurationProcessor.HINT ).process( cliRequest );
1164 }
1165 else if ( userSuppliedConfigurationProcessorCount == 1 )
1166 {
1167
1168
1169
1170 for ( Entry<String, ConfigurationProcessor> entry : configurationProcessors.entrySet() )
1171 {
1172 String hint = entry.getKey();
1173 if ( !hint.equals( SettingsXmlConfigurationProcessor.HINT ) )
1174 {
1175 ConfigurationProcessor configurationProcessor = entry.getValue();
1176 configurationProcessor.process( cliRequest );
1177 }
1178 }
1179 }
1180 else if ( userSuppliedConfigurationProcessorCount > 1 )
1181 {
1182
1183
1184
1185 StringBuilder sb = new StringBuilder(
1186 String.format( "\nThere can only be one user supplied ConfigurationProcessor, there are %s:\n\n",
1187 userSuppliedConfigurationProcessorCount ) );
1188 for ( Entry<String, ConfigurationProcessor> entry : configurationProcessors.entrySet() )
1189 {
1190 String hint = entry.getKey();
1191 if ( !hint.equals( SettingsXmlConfigurationProcessor.HINT ) )
1192 {
1193 ConfigurationProcessor configurationProcessor = entry.getValue();
1194 sb.append( String.format( "%s\n", configurationProcessor.getClass().getName() ) );
1195 }
1196 }
1197 sb.append( String.format( "\n" ) );
1198 throw new Exception( sb.toString() );
1199 }
1200 }
1201
1202 private void toolchains( CliRequest cliRequest )
1203 throws Exception
1204 {
1205 File userToolchainsFile;
1206
1207 if ( cliRequest.commandLine.hasOption( CLIManager.ALTERNATE_USER_TOOLCHAINS ) )
1208 {
1209 userToolchainsFile =
1210 new File( cliRequest.commandLine.getOptionValue( CLIManager.ALTERNATE_USER_TOOLCHAINS ) );
1211 userToolchainsFile = resolveFile( userToolchainsFile, cliRequest.workingDirectory );
1212
1213 if ( !userToolchainsFile.isFile() )
1214 {
1215 throw new FileNotFoundException(
1216 "The specified user toolchains file does not exist: " + userToolchainsFile );
1217 }
1218 }
1219 else
1220 {
1221 userToolchainsFile = DEFAULT_USER_TOOLCHAINS_FILE;
1222 }
1223
1224 File globalToolchainsFile;
1225
1226 if ( cliRequest.commandLine.hasOption( CLIManager.ALTERNATE_GLOBAL_TOOLCHAINS ) )
1227 {
1228 globalToolchainsFile =
1229 new File( cliRequest.commandLine.getOptionValue( CLIManager.ALTERNATE_GLOBAL_TOOLCHAINS ) );
1230 globalToolchainsFile = resolveFile( globalToolchainsFile, cliRequest.workingDirectory );
1231
1232 if ( !globalToolchainsFile.isFile() )
1233 {
1234 throw new FileNotFoundException(
1235 "The specified global toolchains file does not exist: " + globalToolchainsFile );
1236 }
1237 }
1238 else
1239 {
1240 globalToolchainsFile = DEFAULT_GLOBAL_TOOLCHAINS_FILE;
1241 }
1242
1243 cliRequest.request.setGlobalToolchainsFile( globalToolchainsFile );
1244 cliRequest.request.setUserToolchainsFile( userToolchainsFile );
1245
1246 DefaultToolchainsBuildingRequest toolchainsRequest = new DefaultToolchainsBuildingRequest();
1247 if ( globalToolchainsFile.isFile() )
1248 {
1249 toolchainsRequest.setGlobalToolchainsSource( new FileSource( globalToolchainsFile ) );
1250 }
1251 if ( userToolchainsFile.isFile() )
1252 {
1253 toolchainsRequest.setUserToolchainsSource( new FileSource( userToolchainsFile ) );
1254 }
1255
1256 eventSpyDispatcher.onEvent( toolchainsRequest );
1257
1258 slf4jLogger.debug(
1259 "Reading global toolchains from " + getLocation( toolchainsRequest.getGlobalToolchainsSource(),
1260 globalToolchainsFile ) );
1261 slf4jLogger.debug( "Reading user toolchains from " + getLocation( toolchainsRequest.getUserToolchainsSource(),
1262 userToolchainsFile ) );
1263
1264 ToolchainsBuildingResult toolchainsResult = toolchainsBuilder.build( toolchainsRequest );
1265
1266 eventSpyDispatcher.onEvent( toolchainsRequest );
1267
1268 executionRequestPopulator.populateFromToolchains( cliRequest.request,
1269 toolchainsResult.getEffectiveToolchains() );
1270
1271 if ( !toolchainsResult.getProblems().isEmpty() && slf4jLogger.isWarnEnabled() )
1272 {
1273 slf4jLogger.warn( "" );
1274 slf4jLogger.warn( "Some problems were encountered while building the effective toolchains" );
1275
1276 for ( Problem problem : toolchainsResult.getProblems() )
1277 {
1278 slf4jLogger.warn( problem.getMessage() + " @ " + problem.getLocation() );
1279 }
1280
1281 slf4jLogger.warn( "" );
1282 }
1283 }
1284
1285 private Object getLocation( Source source, File defaultLocation )
1286 {
1287 if ( source != null )
1288 {
1289 return source.getLocation();
1290 }
1291 return defaultLocation;
1292 }
1293
1294 private MavenExecutionRequest populateRequest( CliRequest cliRequest )
1295 {
1296 return populateRequest( cliRequest, cliRequest.request );
1297 }
1298
1299 @SuppressWarnings( "checkstyle:methodlength" )
1300 private MavenExecutionRequest populateRequest( CliRequest cliRequest, MavenExecutionRequest request )
1301 {
1302 CommandLine commandLine = cliRequest.commandLine;
1303 String workingDirectory = cliRequest.workingDirectory;
1304 boolean quiet = cliRequest.quiet;
1305 boolean showErrors = cliRequest.showErrors;
1306
1307 String[] deprecatedOptions = { "up", "npu", "cpu", "npr" };
1308 for ( String deprecatedOption : deprecatedOptions )
1309 {
1310 if ( commandLine.hasOption( deprecatedOption ) )
1311 {
1312 slf4jLogger.warn( "Command line option -" + deprecatedOption
1313 + " is deprecated and will be removed in future Maven versions." );
1314 }
1315 }
1316
1317
1318
1319
1320
1321
1322 if ( commandLine.hasOption( CLIManager.BATCH_MODE ) )
1323 {
1324 request.setInteractiveMode( false );
1325 }
1326
1327 boolean noSnapshotUpdates = false;
1328 if ( commandLine.hasOption( CLIManager.SUPRESS_SNAPSHOT_UPDATES ) )
1329 {
1330 noSnapshotUpdates = true;
1331 }
1332
1333
1334
1335
1336
1337 List<String> goals = commandLine.getArgList();
1338
1339 boolean recursive = true;
1340
1341
1342 String reactorFailureBehaviour = MavenExecutionRequest.REACTOR_FAIL_FAST;
1343
1344 if ( commandLine.hasOption( CLIManager.NON_RECURSIVE ) )
1345 {
1346 recursive = false;
1347 }
1348
1349 if ( commandLine.hasOption( CLIManager.FAIL_FAST ) )
1350 {
1351 reactorFailureBehaviour = MavenExecutionRequest.REACTOR_FAIL_FAST;
1352 }
1353 else if ( commandLine.hasOption( CLIManager.FAIL_AT_END ) )
1354 {
1355 reactorFailureBehaviour = MavenExecutionRequest.REACTOR_FAIL_AT_END;
1356 }
1357 else if ( commandLine.hasOption( CLIManager.FAIL_NEVER ) )
1358 {
1359 reactorFailureBehaviour = MavenExecutionRequest.REACTOR_FAIL_NEVER;
1360 }
1361
1362 if ( commandLine.hasOption( CLIManager.OFFLINE ) )
1363 {
1364 request.setOffline( true );
1365 }
1366
1367 boolean updateSnapshots = false;
1368
1369 if ( commandLine.hasOption( CLIManager.UPDATE_SNAPSHOTS ) )
1370 {
1371 updateSnapshots = true;
1372 }
1373
1374 String globalChecksumPolicy = null;
1375
1376 if ( commandLine.hasOption( CLIManager.CHECKSUM_FAILURE_POLICY ) )
1377 {
1378 globalChecksumPolicy = MavenExecutionRequest.CHECKSUM_POLICY_FAIL;
1379 }
1380 else if ( commandLine.hasOption( CLIManager.CHECKSUM_WARNING_POLICY ) )
1381 {
1382 globalChecksumPolicy = MavenExecutionRequest.CHECKSUM_POLICY_WARN;
1383 }
1384
1385 File baseDirectory = new File( workingDirectory, "" ).getAbsoluteFile();
1386
1387
1388
1389
1390
1391 List<String> activeProfiles = new ArrayList<>();
1392
1393 List<String> inactiveProfiles = new ArrayList<>();
1394
1395 if ( commandLine.hasOption( CLIManager.ACTIVATE_PROFILES ) )
1396 {
1397 String[] profileOptionValues = commandLine.getOptionValues( CLIManager.ACTIVATE_PROFILES );
1398 if ( profileOptionValues != null )
1399 {
1400 for ( String profileOptionValue : profileOptionValues )
1401 {
1402 StringTokenizer profileTokens = new StringTokenizer( profileOptionValue, "," );
1403
1404 while ( profileTokens.hasMoreTokens() )
1405 {
1406 String profileAction = profileTokens.nextToken().trim();
1407
1408 if ( profileAction.startsWith( "-" ) || profileAction.startsWith( "!" ) )
1409 {
1410 inactiveProfiles.add( profileAction.substring( 1 ) );
1411 }
1412 else if ( profileAction.startsWith( "+" ) )
1413 {
1414 activeProfiles.add( profileAction.substring( 1 ) );
1415 }
1416 else
1417 {
1418 activeProfiles.add( profileAction );
1419 }
1420 }
1421 }
1422 }
1423 }
1424
1425 TransferListener transferListener;
1426
1427 if ( quiet )
1428 {
1429 transferListener = new QuietMavenTransferListener();
1430 }
1431 else if ( request.isInteractiveMode() && !cliRequest.commandLine.hasOption( CLIManager.LOG_FILE ) )
1432 {
1433
1434
1435
1436
1437 transferListener = getConsoleTransferListener( cliRequest.commandLine.hasOption( CLIManager.DEBUG ) );
1438 }
1439 else
1440 {
1441 transferListener = getBatchTransferListener();
1442 }
1443
1444 ExecutionListener executionListener = new ExecutionEventLogger();
1445 if ( eventSpyDispatcher != null )
1446 {
1447 executionListener = eventSpyDispatcher.chainListener( executionListener );
1448 }
1449
1450 String alternatePomFile = null;
1451 if ( commandLine.hasOption( CLIManager.ALTERNATE_POM_FILE ) )
1452 {
1453 alternatePomFile = commandLine.getOptionValue( CLIManager.ALTERNATE_POM_FILE );
1454 }
1455
1456 request.setBaseDirectory( baseDirectory ).setGoals( goals ).setSystemProperties(
1457 cliRequest.systemProperties ).setUserProperties( cliRequest.userProperties ).setReactorFailureBehavior(
1458 reactorFailureBehaviour )
1459 .setRecursive( recursive )
1460 .setShowErrors( showErrors )
1461 .addActiveProfiles( activeProfiles )
1462 .addInactiveProfiles( inactiveProfiles )
1463 .setExecutionListener( executionListener ).setTransferListener(
1464 transferListener )
1465 .setUpdateSnapshots( updateSnapshots )
1466 .setNoSnapshotUpdates( noSnapshotUpdates )
1467 .setGlobalChecksumPolicy( globalChecksumPolicy )
1468 .setMultiModuleProjectDirectory( cliRequest.multiModuleProjectDirectory );
1469
1470 if ( alternatePomFile != null )
1471 {
1472 File pom = resolveFile( new File( alternatePomFile ), workingDirectory );
1473 if ( pom.isDirectory() )
1474 {
1475 pom = new File( pom, "pom.xml" );
1476 }
1477
1478 request.setPom( pom );
1479 }
1480 else if ( modelProcessor != null )
1481 {
1482 File pom = modelProcessor.locatePom( baseDirectory );
1483
1484 if ( pom.isFile() )
1485 {
1486 request.setPom( pom );
1487 }
1488 }
1489
1490 if ( ( request.getPom() != null ) && ( request.getPom().getParentFile() != null ) )
1491 {
1492 request.setBaseDirectory( request.getPom().getParentFile() );
1493 }
1494
1495 if ( commandLine.hasOption( CLIManager.RESUME_FROM ) )
1496 {
1497 request.setResumeFrom( commandLine.getOptionValue( CLIManager.RESUME_FROM ) );
1498 }
1499
1500 if ( commandLine.hasOption( CLIManager.PROJECT_LIST ) )
1501 {
1502 String[] projectOptionValues = commandLine.getOptionValues( CLIManager.PROJECT_LIST );
1503
1504 List<String> inclProjects = new ArrayList<>();
1505 List<String> exclProjects = new ArrayList<>();
1506
1507 if ( projectOptionValues != null )
1508 {
1509 for ( String projectOptionValue : projectOptionValues )
1510 {
1511 StringTokenizer projectTokens = new StringTokenizer( projectOptionValue, "," );
1512
1513 while ( projectTokens.hasMoreTokens() )
1514 {
1515 String projectAction = projectTokens.nextToken().trim();
1516
1517 if ( projectAction.startsWith( "-" ) || projectAction.startsWith( "!" ) )
1518 {
1519 exclProjects.add( projectAction.substring( 1 ) );
1520 }
1521 else if ( projectAction.startsWith( "+" ) )
1522 {
1523 inclProjects.add( projectAction.substring( 1 ) );
1524 }
1525 else
1526 {
1527 inclProjects.add( projectAction );
1528 }
1529 }
1530 }
1531 }
1532
1533 request.setSelectedProjects( inclProjects );
1534 request.setExcludedProjects( exclProjects );
1535 }
1536
1537 if ( commandLine.hasOption( CLIManager.ALSO_MAKE ) && !commandLine.hasOption(
1538 CLIManager.ALSO_MAKE_DEPENDENTS ) )
1539 {
1540 request.setMakeBehavior( MavenExecutionRequest.REACTOR_MAKE_UPSTREAM );
1541 }
1542 else if ( !commandLine.hasOption( CLIManager.ALSO_MAKE ) && commandLine.hasOption(
1543 CLIManager.ALSO_MAKE_DEPENDENTS ) )
1544 {
1545 request.setMakeBehavior( MavenExecutionRequest.REACTOR_MAKE_DOWNSTREAM );
1546 }
1547 else if ( commandLine.hasOption( CLIManager.ALSO_MAKE ) && commandLine.hasOption(
1548 CLIManager.ALSO_MAKE_DEPENDENTS ) )
1549 {
1550 request.setMakeBehavior( MavenExecutionRequest.REACTOR_MAKE_BOTH );
1551 }
1552
1553 String localRepoProperty = request.getUserProperties().getProperty( MavenCli.LOCAL_REPO_PROPERTY );
1554
1555 if ( localRepoProperty == null )
1556 {
1557 localRepoProperty = request.getSystemProperties().getProperty( MavenCli.LOCAL_REPO_PROPERTY );
1558 }
1559
1560 if ( localRepoProperty != null )
1561 {
1562 request.setLocalRepositoryPath( localRepoProperty );
1563 }
1564
1565 request.setCacheNotFound( true );
1566 request.setCacheTransferError( false );
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576 final String threadConfiguration = commandLine.hasOption( CLIManager.THREADS )
1577 ? commandLine.getOptionValue( CLIManager.THREADS )
1578 : null;
1579
1580 if ( threadConfiguration != null )
1581 {
1582
1583
1584
1585 request.setBuilderId( "multithreaded" );
1586
1587 if ( threadConfiguration.contains( "C" ) )
1588 {
1589 request.setDegreeOfConcurrency( calculateDegreeOfConcurrencyWithCoreMultiplier( threadConfiguration ) );
1590 }
1591 else
1592 {
1593 request.setDegreeOfConcurrency( Integer.valueOf( threadConfiguration ) );
1594 }
1595 }
1596
1597
1598
1599
1600 if ( commandLine.hasOption( CLIManager.BUILDER ) )
1601 {
1602 request.setBuilderId( commandLine.getOptionValue( CLIManager.BUILDER ) );
1603 }
1604
1605 return request;
1606 }
1607
1608 int calculateDegreeOfConcurrencyWithCoreMultiplier( String threadConfiguration )
1609 {
1610 int procs = Runtime.getRuntime().availableProcessors();
1611 return (int) ( Float.valueOf( threadConfiguration.replace( "C", "" ) ) * procs );
1612 }
1613
1614 static File resolveFile( File file, String workingDirectory )
1615 {
1616 if ( file == null )
1617 {
1618 return null;
1619 }
1620 else if ( file.isAbsolute() )
1621 {
1622 return file;
1623 }
1624 else if ( file.getPath().startsWith( File.separator ) )
1625 {
1626
1627 return file.getAbsoluteFile();
1628 }
1629 else
1630 {
1631 return new File( workingDirectory, file.getPath() ).getAbsoluteFile();
1632 }
1633 }
1634
1635
1636
1637
1638
1639 static void populateProperties( CommandLine commandLine, Properties systemProperties, Properties userProperties )
1640 {
1641 EnvironmentUtils.addEnvVars( systemProperties );
1642
1643
1644
1645
1646
1647
1648
1649 if ( commandLine.hasOption( CLIManager.SET_SYSTEM_PROPERTY ) )
1650 {
1651 String[] defStrs = commandLine.getOptionValues( CLIManager.SET_SYSTEM_PROPERTY );
1652
1653 if ( defStrs != null )
1654 {
1655 for ( String defStr : defStrs )
1656 {
1657 setCliProperty( defStr, userProperties );
1658 }
1659 }
1660 }
1661
1662 SystemProperties.addSystemProperties( systemProperties );
1663
1664
1665
1666
1667
1668
1669 Properties buildProperties = CLIReportingUtils.getBuildProperties();
1670
1671 String mavenVersion = buildProperties.getProperty( CLIReportingUtils.BUILD_VERSION_PROPERTY );
1672 systemProperties.setProperty( "maven.version", mavenVersion );
1673
1674 String mavenBuildVersion = CLIReportingUtils.createMavenVersionString( buildProperties );
1675 systemProperties.setProperty( "maven.build.version", mavenBuildVersion );
1676 }
1677
1678 private static void setCliProperty( String property, Properties properties )
1679 {
1680 String name;
1681
1682 String value;
1683
1684 int i = property.indexOf( '=' );
1685
1686 if ( i <= 0 )
1687 {
1688 name = property.trim();
1689
1690 value = "true";
1691 }
1692 else
1693 {
1694 name = property.substring( 0, i ).trim();
1695
1696 value = property.substring( i + 1 );
1697 }
1698
1699 properties.setProperty( name, value );
1700
1701
1702
1703
1704
1705
1706 System.setProperty( name, value );
1707 }
1708
1709 static class ExitException
1710 extends Exception
1711 {
1712 int exitCode;
1713
1714 ExitException( int exitCode )
1715 {
1716 this.exitCode = exitCode;
1717 }
1718 }
1719
1720
1721
1722
1723
1724 protected TransferListener getConsoleTransferListener( boolean printResourceNames )
1725 {
1726 return new ConsoleMavenTransferListener( System.out, printResourceNames );
1727 }
1728
1729 protected TransferListener getBatchTransferListener()
1730 {
1731 return new Slf4jMavenTransferListener();
1732 }
1733
1734 protected void customizeContainer( PlexusContainer container )
1735 {
1736 }
1737
1738 protected ModelProcessor createModelProcessor( PlexusContainer container )
1739 throws ComponentLookupException
1740 {
1741 return container.lookup( ModelProcessor.class );
1742 }
1743 }