@@ -200,14 +200,16 @@ public static void main( String[] args )
200200
201201 public static int main ( String [] args , ClassWorld classWorld )
202202 {
203- MavenCli cli = new MavenCli ();
204-
205203 MessageUtils .systemInstall ();
206204 MessageUtils .registerShutdownHook ();
207- int result = cli .doMain ( new CliRequest ( args , classWorld ) );
208- MessageUtils .systemUninstall ();
209-
210- return result ;
205+ try
206+ {
207+ return doMain ( args , classWorld );
208+ }
209+ finally
210+ {
211+ MessageUtils .systemUninstall ();
212+ }
211213 }
212214
213215 // TODO need to externalize CliRequest
@@ -319,13 +321,13 @@ public int doMain( CliRequest cliRequest )
319321 }
320322 catch ( BuildAbort e )
321323 {
322- CLIReportingUtils . showError ( slf4jLogger , "ABORTED" , e , cliRequest .showErrors );
324+ showError ( "ABORTED" , e , cliRequest .showErrors );
323325
324326 return 2 ;
325327 }
326328 catch ( Exception e )
327329 {
328- CLIReportingUtils . showError ( slf4jLogger , "Error executing Maven." , e , cliRequest .showErrors );
330+ showError ( "Error executing Maven." , e , cliRequest .showErrors );
329331
330332 return 1 ;
331333 }
@@ -351,8 +353,7 @@ protected void initialize( CliRequest cliRequest )
351353 String basedirProperty = System .getProperty ( MULTIMODULE_PROJECT_DIRECTORY );
352354 if ( basedirProperty == null )
353355 {
354- System .err .format (
355- "-D%s system property is not set." , MULTIMODULE_PROJECT_DIRECTORY );
356+ printErr ( String .format ( "-D%s system property is not set." , MULTIMODULE_PROJECT_DIRECTORY ) );
356357 throw new ExitException ( 1 );
357358 }
358359 File basedir = new File ( basedirProperty );
@@ -387,7 +388,7 @@ protected void cli( CliRequest cliRequest )
387388 //
388389 slf4jLogger = new Slf4jStdoutLogger ();
389390
390- cliManager = new CLIManager ();
391+ cliManager = createCliManager ();
391392
392393 List <String > args = new ArrayList <>();
393394 CommandLine mavenConfig = null ;
@@ -415,8 +416,7 @@ protected void cli( CliRequest cliRequest )
415416 }
416417 catch ( ParseException e )
417418 {
418- System .err .println ( "Unable to parse maven.config: " + e .getMessage () );
419- cliManager .displayHelp ( System .out );
419+ printError ( "Unable to parse maven.config: " , e );
420420 throw e ;
421421 }
422422
@@ -433,29 +433,59 @@ protected void cli( CliRequest cliRequest )
433433 }
434434 catch ( ParseException e )
435435 {
436- System .err .println ( "Unable to parse command line options: " + e .getMessage () );
437- cliManager .displayHelp ( System .out );
436+ printError ( "Unable to parse command line options: " , e );
438437 throw e ;
439438 }
440439 }
441440
441+ protected void showError ( String message , Throwable e , boolean showStackTrace )
442+ {
443+ CLIReportingUtils .showError ( slf4jLogger , message , e , showStackTrace );
444+ }
445+
446+ protected void printError ( String message , ParseException e )
447+ {
448+ printErr ( message + e .getMessage () );
449+ printHelp ();
450+ }
451+
452+ protected void printHelp ()
453+ {
454+ cliManager .displayHelp ( System .out );
455+ }
456+
457+ protected void printOut ( String message )
458+ {
459+ System .out .println ( message );
460+ }
461+
462+ protected void printErr ( String message )
463+ {
464+ System .err .println ( message );
465+ }
466+
467+ protected CLIManager createCliManager ()
468+ {
469+ return new CLIManager ();
470+ }
471+
442472 protected void informativeCommands ( CliRequest cliRequest ) throws ExitException
443473 {
444474 if ( cliRequest .commandLine .hasOption ( CLIManager .HELP ) )
445475 {
446- cliManager . displayHelp ( System . out );
476+ printHelp ( );
447477 throw new ExitException ( 0 );
448478 }
449479
450480 if ( cliRequest .commandLine .hasOption ( CLIManager .VERSION ) )
451481 {
452482 if ( cliRequest .commandLine .hasOption ( CLIManager .QUIET ) )
453483 {
454- System . out . println ( CLIReportingUtils .showVersionMinimal () );
484+ printOut ( CLIReportingUtils .showVersionMinimal () );
455485 }
456486 else
457487 {
458- System . out . println ( CLIReportingUtils .showVersion () );
488+ printOut ( CLIReportingUtils .showVersion () );
459489 }
460490 throw new ExitException ( 0 );
461491 }
@@ -604,7 +634,7 @@ protected void version( CliRequest cliRequest )
604634 {
605635 if ( cliRequest .verbose || cliRequest .commandLine .hasOption ( CLIManager .SHOW_VERSION ) )
606636 {
607- System . out . println ( CLIReportingUtils .showVersion () );
637+ printOut ( CLIReportingUtils .showVersion () );
608638 }
609639 }
610640
@@ -670,8 +700,11 @@ protected PlexusContainer container( CliRequest cliRequest )
670700 List <File > extClassPath = parseExtClasspath ( cliRequest );
671701
672702 CoreExtensionEntry coreEntry = CoreExtensionEntry .discoverFrom ( coreRealm );
703+
704+ List <CoreExtension > extensionsDesc = loadCoreExtensionsDescriptors ( cliRequest .multiModuleProjectDirectory );
705+
673706 List <CoreExtensionEntry > extensions =
674- loadCoreExtensions ( cliRequest , coreRealm , coreEntry .getExportedArtifacts () );
707+ loadCoreExtensions ( extensionsDesc , coreRealm , coreEntry .getExportedArtifacts () );
675708
676709 ClassRealm containerRealm = setupContainerRealm ( cliRequest .classWorld , coreRealm , extClassPath , extensions );
677710
@@ -689,15 +722,7 @@ protected PlexusContainer container( CliRequest cliRequest )
689722
690723 final CoreExports exports = new CoreExports ( containerRealm , exportedArtifacts , exportedPackages );
691724
692- DefaultPlexusContainer container = new DefaultPlexusContainer ( cc , new AbstractModule ()
693- {
694- @ Override
695- protected void configure ()
696- {
697- bind ( ILoggerFactory .class ).toInstance ( slf4jLoggerFactory );
698- bind ( CoreExports .class ).toInstance ( exports );
699- }
700- } );
725+ DefaultPlexusContainer container = new DefaultPlexusContainer ( cc , createModule ( exports ) );
701726
702727 // NOTE: To avoid inconsistencies, we'll use the TCCL exclusively for lookups
703728 container .setLookupRealm ( null );
@@ -713,8 +738,14 @@ protected void configure()
713738
714739 customizeContainer ( container );
715740
716- container .getLoggerManager ().setThresholds ( cliRequest .request .getLoggingLevel () );
741+ populateFromContainer ( cliRequest , container );
742+
743+ return container ;
744+ }
717745
746+ protected void populateFromContainer ( CliRequest cliRequest , PlexusContainer container )
747+ throws ComponentLookupException
748+ {
718749 eventSpyDispatcher = container .lookup ( EventSpyDispatcher .class );
719750
720751 DefaultEventSpyContext eventSpyContext = new DefaultEventSpyContext ();
@@ -740,33 +771,61 @@ protected void configure()
740771 toolchainsBuilder = container .lookup ( ToolchainsBuilder .class );
741772
742773 dispatcher = (DefaultSecDispatcher ) container .lookup ( SecDispatcher .class , "maven" );
774+ }
743775
744- return container ;
776+ protected AbstractModule createModule ( CoreExports exports )
777+ {
778+ return new AbstractModule ()
779+ {
780+ @ Override
781+ protected void configure ()
782+ {
783+ bind ( ILoggerFactory .class ).toInstance ( slf4jLoggerFactory );
784+ bind ( CoreExports .class ).toInstance ( exports );
785+ }
786+ };
745787 }
746788
747789 protected List <CoreExtension > loadCoreExtensionsDescriptors ( File multiModuleProjectDirectory )
748790 {
749- if ( cliRequest . multiModuleProjectDirectory == null )
791+ if ( multiModuleProjectDirectory == null )
750792 {
751793 return Collections .emptyList ();
752794 }
753795
754- File extensionsFile = new File ( cliRequest . multiModuleProjectDirectory , EXTENSIONS_FILENAME );
796+ File extensionsFile = new File ( multiModuleProjectDirectory , EXTENSIONS_FILENAME );
755797 if ( !extensionsFile .isFile () )
756798 {
757799 return Collections .emptyList ();
758800 }
759801
760802 try
761803 {
762- List <CoreExtension > extensions = readCoreExtensionsDescriptor ( extensionsFile );
804+ return readCoreExtensionsDescriptor ( extensionsFile );
805+ }
806+ catch ( Exception e )
807+ {
808+ slf4jLogger .warn ( "Failed to read extensions descriptor from '{}'" , extensionsFile , e );
809+ }
810+
811+ return Collections .emptyList ();
812+ }
813+
814+ protected List <CoreExtensionEntry > loadCoreExtensions (
815+ List <CoreExtension > extensions ,
816+ ClassRealm containerRealm ,
817+ Set <String > providedArtifacts )
818+ {
819+
820+ try
821+ {
763822 if ( extensions .isEmpty () )
764823 {
765824 return Collections .emptyList ();
766825 }
767826
768827 ContainerConfiguration cc = new DefaultContainerConfiguration () //
769- .setClassWorld ( cliRequest . classWorld ) //
828+ .setClassWorld ( containerRealm . getWorld () ) //
770829 .setRealm ( containerRealm ) //
771830 .setClassPathScanning ( PlexusConstants .SCANNING_INDEX ) //
772831 .setAutoWiring ( true ) //
@@ -784,6 +843,9 @@ protected void configure()
784843
785844 try
786845 {
846+ CliRequest cliRequest = new CliRequest ( new String [0 ], classWorld );
847+ cliRequest .commandLine = new CommandLine .Builder ().build ();
848+
787849 container .setLookupRealm ( null );
788850
789851 container .setLoggerManager ( plexusLoggerManager );
@@ -792,10 +854,12 @@ protected void configure()
792854
793855 Thread .currentThread ().setContextClassLoader ( container .getContainerRealm () );
794856
795- executionRequestPopulator = container .lookup ( MavenExecutionRequestPopulator .class );
796-
797857 configurationProcessors = container .lookupMap ( ConfigurationProcessor .class );
798858
859+ eventSpyDispatcher = container .lookup ( EventSpyDispatcher .class );
860+
861+ properties ( cliRequest );
862+
799863 configure ( cliRequest );
800864
801865 MavenExecutionRequest request = DefaultMavenExecutionRequest .copy ( cliRequest .request );
@@ -812,7 +876,6 @@ protected void configure()
812876 }
813877 finally
814878 {
815- executionRequestPopulator = null ;
816879 container .dispose ();
817880 }
818881 }
@@ -823,7 +886,7 @@ protected void configure()
823886 }
824887 catch ( Exception e )
825888 {
826- slf4jLogger .warn ( "Failed to read extensions descriptor from '{}'" , extensionsFile , e );
889+ slf4jLogger .warn ( "Failed to load extensions" , e );
827890 }
828891 return Collections .emptyList ();
829892 }
@@ -939,8 +1002,7 @@ protected void encryption( CliRequest cliRequest )
9391002
9401003 DefaultPlexusCipher cipher = new DefaultPlexusCipher ();
9411004
942- System .out .println (
943- cipher .encryptAndDecorate ( passwd , DefaultSecDispatcher .SYSTEM_PROPERTY_SEC_LOCATION ) );
1005+ printOut ( cipher .encryptAndDecorate ( passwd , DefaultSecDispatcher .SYSTEM_PROPERTY_SEC_LOCATION ) );
9441006
9451007 throw new ExitException ( 0 );
9461008 }
@@ -986,7 +1048,7 @@ else if ( cliRequest.commandLine.hasOption( CLIManager.ENCRYPT_PASSWORD ) )
9861048
9871049 DefaultPlexusCipher cipher = new DefaultPlexusCipher ();
9881050 String masterPasswd = cipher .decryptDecorated ( master , DefaultSecDispatcher .SYSTEM_PROPERTY_SEC_LOCATION );
989- System . out . println ( cipher .encryptAndDecorate ( passwd , masterPasswd ) );
1051+ printOut ( cipher .encryptAndDecorate ( passwd , masterPasswd ) );
9901052
9911053 throw new ExitException ( 0 );
9921054 }
@@ -1014,7 +1076,7 @@ protected int execute( CliRequest cliRequest )
10141076
10151077 eventSpyDispatcher .onEvent ( request );
10161078
1017- MavenExecutionResult result = maven . execute ( request );
1079+ MavenExecutionResult result = doExecute ( request );
10181080
10191081 eventSpyDispatcher .onEvent ( result );
10201082
@@ -1101,7 +1163,13 @@ else if ( !failedProjects.isEmpty() )
11011163 }
11021164 }
11031165
1104- private void logBuildResumeHint ( String resumeBuildHint )
1166+ protected MavenExecutionResult doExecute ( MavenExecutionRequest request )
1167+ {
1168+ MavenExecutionResult result = maven .execute ( request );
1169+ return result ;
1170+ }
1171+
1172+ protected void logBuildResumeHint ( String resumeBuildHint )
11051173 {
11061174 slf4jLogger .error ( "" );
11071175 slf4jLogger .error ( "After correcting the problems, you can resume the build with the command" );
@@ -1720,7 +1788,7 @@ protected int calculateDegreeOfConcurrencyWithCoreMultiplier( String threadConfi
17201788
17211789 protected void populateProperties ( CommandLine commandLine , Properties systemProperties , Properties userProperties )
17221790 {
1723- EnvironmentUtils . addEnvVars ( systemProperties );
1791+ addEnvVars ( systemProperties );
17241792
17251793 // ----------------------------------------------------------------------
17261794 // Options that are set on the command line become system properties
@@ -1741,7 +1809,7 @@ protected void populateProperties( CommandLine commandLine, Properties systemPro
17411809 }
17421810 }
17431811
1744- SystemProperties . addSystemProperties ( systemProperties );
1812+ addSystemProperties ( systemProperties );
17451813
17461814 // ----------------------------------------------------------------------
17471815 // Properties containing info about the currently running version of Maven
@@ -1757,6 +1825,16 @@ protected void populateProperties( CommandLine commandLine, Properties systemPro
17571825 systemProperties .setProperty ( "maven.build.version" , mavenBuildVersion );
17581826 }
17591827
1828+ protected void addSystemProperties ( Properties systemProperties )
1829+ {
1830+ SystemProperties .addSystemProperties ( systemProperties );
1831+ }
1832+
1833+ protected void addEnvVars ( Properties systemProperties )
1834+ {
1835+ EnvironmentUtils .addEnvVars ( systemProperties );
1836+ }
1837+
17601838 protected void setCliProperty ( String property , Properties properties )
17611839 {
17621840 String name ;
0 commit comments