public static int main()

in maven-core/src/main/java/org/apache/maven/cli/MavenCli.java [108:382]


    public static int main( String[] args, ClassWorld classWorld )
    {
        // ----------------------------------------------------------------------
        // Setup the command line parser
        // ----------------------------------------------------------------------

        CLIManager cliManager = new CLIManager();

        CommandLine commandLine;
        try
        {
            commandLine = cliManager.parse( args );
        }
        catch ( ParseException e )
        {
            System.err.println( "Unable to parse command line options: " + e.getMessage() );
            cliManager.displayHelp();
            return 1;
        }

        boolean debug = commandLine.hasOption( CLIManager.DEBUG );

        boolean showErrors = debug || commandLine.hasOption( CLIManager.ERRORS );

        if ( showErrors )
        {
            System.out.println( "+ Error stacktraces are turned on." );
        }

        // ----------------------------------------------------------------------
        // Process particular command line options
        // ----------------------------------------------------------------------

        if ( commandLine.hasOption( CLIManager.HELP ) )
        {
            cliManager.displayHelp();
            return 0;
        }

        if ( commandLine.hasOption( CLIManager.VERSION ) )
        {
            showVersion();

            return 0;
        }
        else if ( debug || commandLine.hasOption( CLIManager.SHOW_VERSION ) )
        {
            showVersion();
        }

        EventDispatcher eventDispatcher = new DefaultEventDispatcher();

        // Make sure the Maven home directory is an absolute path to save us from confusion with say drive-relative
        // Windows paths.
        String mavenHome = System.getProperty( "maven.home" );
        if ( mavenHome != null )
        {
            System.setProperty( "maven.home", new File( mavenHome ).getAbsolutePath() );
        }

        // ----------------------------------------------------------------------
        // Now that we have everything that we need we will fire up plexus and
        // bring the maven component to life for use.
        // ----------------------------------------------------------------------

        embedder = new Embedder();

        try
        {
            embedder.start( classWorld );
        }
        catch ( PlexusContainerException e )
        {
            showFatalError( "Unable to start the embedded plexus container", e, showErrors );

            return 1;
        }

        // wraps the following code to ensure the embedder is stopped no matter what else happens.
        try
        {
            // ----------------------------------------------------------------------
            // The execution properties need to be created before the settings
            // are constructed.
            // ----------------------------------------------------------------------

            Properties executionProperties = new Properties();
            Properties userProperties = new Properties();
            populateProperties( commandLine, executionProperties, userProperties );

            Settings settings;

            try
            {
                settings = buildSettings( commandLine );
            }
            catch ( SettingsConfigurationException e )
            {
                showError( "Error reading settings.xml: " + e.getMessage(), e, showErrors );

                return 1;
            }
            catch ( ComponentLookupException e )
            {
                showFatalError( "Unable to read settings.xml", e, showErrors );

                return 1;
            }

            DefaultSecDispatcher dispatcher;
            try
            {
                if ( commandLine.hasOption( CLIManager.ENCRYPT_MASTER_PASSWORD ) )
                {
                    String passwd = commandLine.getOptionValue( CLIManager.ENCRYPT_MASTER_PASSWORD );

                    DefaultPlexusCipher cipher = new DefaultPlexusCipher();

                    System.out.println( cipher.encryptAndDecorate( passwd,
                                                                   DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION ) );

                    return 0;
                }
                else if ( commandLine.hasOption( CLIManager.ENCRYPT_PASSWORD ) )
                {
                    String passwd = commandLine.getOptionValue( CLIManager.ENCRYPT_PASSWORD );

                    dispatcher = (DefaultSecDispatcher) embedder.lookup( SecDispatcher.ROLE );
                    String configurationFile = dispatcher.getConfigurationFile();
                    if ( configurationFile.startsWith( "~" ) )
                    {
                        configurationFile = System.getProperty( "user.home" ) + configurationFile.substring( 1 );
                    }
                    String file = System.getProperty( DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION, configurationFile );
                    embedder.release( dispatcher );

                    String master = null;

                    SettingsSecurity sec = SecUtil.read( file, true );
                    if ( sec != null )
                    {
                        master = sec.getMaster();
                    }

                    if ( master == null )
                    {
                        System.err.println( "Master password is not set in the setting security file" );

                        return 1;
                    }

                    DefaultPlexusCipher cipher = new DefaultPlexusCipher();
                    String masterPasswd =
                        cipher.decryptDecorated( master, DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION );
                    System.out.println( cipher.encryptAndDecorate( passwd, masterPasswd ) );

                    return 0;
                }
            }
            catch ( Exception e )
            {
                showFatalError( "Error encrypting password: " + e.getMessage(), e, showErrors );

                return 1;
            }

            Maven maven = null;

            MavenExecutionRequest request = null;

            LoggerManager loggerManager = null;

            try
            {
                // logger must be created first
                loggerManager = (LoggerManager) embedder.lookup( LoggerManager.ROLE );

                if ( debug )
                {
                    loggerManager.setThreshold( Logger.LEVEL_DEBUG );
                }
                else if ( commandLine.hasOption( CLIManager.QUIET ) )
                {
                    // TODO: we need to do some more work here. Some plugins use sys out or log errors at info level.
                    // Ideally, we could use Warn across the board
                    loggerManager.setThreshold( Logger.LEVEL_ERROR );
                    // TODO:Additionally, we can't change the mojo level because the component key includes the version and it isn't known ahead of time. This seems worth changing.
                }

                ProfileManager profileManager = new DefaultProfileManager( embedder.getContainer(), executionProperties );

                if ( commandLine.hasOption( CLIManager.ACTIVATE_PROFILES ) )
                {
                    String [] profileOptionValues = commandLine.getOptionValues( CLIManager.ACTIVATE_PROFILES );

                    if ( profileOptionValues != null )
                    {
                        for ( int i = 0; i < profileOptionValues.length; ++i )
                        {
                            StringTokenizer profileTokens = new StringTokenizer( profileOptionValues[i], "," );

                            while ( profileTokens.hasMoreTokens() )
                            {
                                String profileAction = profileTokens.nextToken().trim();

                                if ( profileAction.startsWith( "-" ) || profileAction.startsWith( "!" ) )
                                {
                                    profileManager.explicitlyDeactivate( profileAction.substring( 1 ) );
                                }
                                else if ( profileAction.startsWith( "+" ) )
                                {
                                    profileManager.explicitlyActivate( profileAction.substring( 1 ) );
                                }
                                else
                                {
                                    profileManager.explicitlyActivate( profileAction );
                                }
                            }
                        }
                    }
                }

                request = createRequest( commandLine, settings, eventDispatcher, loggerManager, profileManager,
                                         executionProperties, userProperties, showErrors );

                setProjectFileOptions( commandLine, request );

                maven =
                    createMavenInstance( settings.isInteractiveMode(),
                                         loggerManager.getLoggerForComponent( WagonManager.ROLE ) );
            }
            catch ( ComponentLookupException e )
            {
                showFatalError( "Unable to configure the Maven application", e, showErrors );

                return 1;
            }
            finally
            {
                if ( loggerManager != null )
                {
                    try
                    {
                        embedder.release( loggerManager );
                    }
                    catch ( ComponentLifecycleException e )
                    {
                        showFatalError( "Error releasing logging manager", e, showErrors );
                    }
                }
            }

            try
            {
                maven.execute( request );
            }
            catch ( MavenExecutionException e )
            {
                return 1;
            }
        }
        finally
        {
            try
            {
                embedder.stop();
            }
            catch ( Exception e )
            {
                // do nothing; we took our best shot.
            }
        }

        return 0;
    }