You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@maven.apache.org by jv...@apache.org on 2002/12/01 05:34:12 UTC

cvs commit: jakarta-turbine-maven/src/java/org/apache/maven/cli App.java

jvanzyl     2002/11/30 20:34:12

  Modified:    src/java/org/apache/maven/cli App.java
  Log:
  refactoring
  
  Revision  Changes    Path
  1.10      +91 -82    jakarta-turbine-maven/src/java/org/apache/maven/cli/App.java
  
  Index: App.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-maven/src/java/org/apache/maven/cli/App.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- App.java	30 Nov 2002 17:18:11 -0000	1.9
  +++ App.java	1 Dec 2002 04:34:12 -0000	1.10
  @@ -64,7 +64,6 @@
   import org.apache.commons.jelly.XMLOutput;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  -import org.apache.maven.MavenConstants;
   import org.apache.maven.MavenUtils;
   import org.apache.maven.app.Maven;
   import org.apache.maven.app.RepoConfigException;
  @@ -199,9 +198,6 @@
       // I N S T A N C E  M E M B E R S
       // ------------------------------------------------------------
   
  -    /** Maven instance. */
  -    private Maven maven;
  -
       /** CLI Parser */
       private CommandLine commandLine;
   
  @@ -250,26 +246,6 @@
       }
   
       /**
  -     * Set the Maven instance.
  -     *
  -     * @param maven Maven instance.
  -     */
  -    public void setMaven( Maven maven )
  -    {
  -        this.maven = maven;
  -    }
  -
  -    /**
  -     * Get the Maven instance.
  -     *
  -     * @return Maven The Maven instance.
  -     */
  -    public Maven getMaven()
  -    {
  -        return this.maven;
  -    }
  -
  -    /**
        * Set the descriptor directory.
        *
        * @param descriptorDirectory The directory where the project.xml
  @@ -300,6 +276,10 @@
           return this.commandLine;
       }
   
  +    // ----------------------------------------------------------------------
  +    // I N S T A N C E  M E T H O D S
  +    // ----------------------------------------------------------------------
  +
       /** Perform initialization.
        *
        * @param args The command-line arguments.
  @@ -312,67 +292,81 @@
        *          the local or remote repositories is malformed.
        * @throws Exception If any other exceptions occur.
        */
  -    public void initializeCore( String[] args )
  +    public void initialize( String[] args )
           throws ParseException, IOException, MalformedURLException, Exception
       {
           log.debug( "Initializing core." );
   
           commandLine = CLIManager.parse( args );
   
  +        File descriptorFile = null;
  +
           if ( getCli().hasOption( FIND_POM_DESCRIPTOR ) )
           {
  -            File file = find( getCli().getOptionValue( FIND_POM_DESCRIPTOR ) );
  -            if ( file != null )
  +            descriptorFile = find( getCli().getOptionValue( FIND_POM_DESCRIPTOR ) );
  +
  +            if ( descriptorFile != null )
               {
  -                getMaven().setDir( file.getParentFile() );
  -                setDescriptorDirectory( file.getParentFile() );
  -                initializeProject( file );
  +                setDescriptorDirectory( descriptorFile.getParentFile() );
               }
               else
               {
  -                initializeProject( new File( getCli().getOptionValue( FIND_POM_DESCRIPTOR ) ) );
  +                descriptorFile = new File( getCli().getOptionValue( FIND_POM_DESCRIPTOR ) );
               }
           }
   
  -        getMaven().addGoalNames( getCli().getArgList() );
  -
           if ( getCli().hasOption( SET_WORKING_DIRECTORY ) )
           {
               setDescriptorDirectory( new File( getCli().getOptionValue( SET_WORKING_DIRECTORY ) ) );
  -            getMaven().setDir( new File( getCli().getOptionValue( SET_WORKING_DIRECTORY ) ) );
           }
           else if ( !getCli().hasOption( FIND_POM_DESCRIPTOR ) )
           {
               setDescriptorDirectory( new File( System.getProperty( "user.dir" ) ) );
  -            getMaven().setDir( new File( System.getProperty( "user.dir" ) ) );
           }
   
           if ( getCli().hasOption( SET_POM_DESCRIPTOR ) )
           {
  -            initializeProject( new File( getCli().getOptionValue( SET_POM_DESCRIPTOR ) ) );
  +            descriptorFile = new File( getCli().getOptionValue( SET_POM_DESCRIPTOR ) );
           }
           else if ( !getCli().hasOption( FIND_POM_DESCRIPTOR ) )
           {
  -            initializeProject( new File( getDescriptorDirectory(), POM_FILE_NAME ) );
  +            descriptorFile = new File( getDescriptorDirectory(), POM_FILE_NAME );
           }
   
  -        if ( getCli().hasOption( SET_SYSTEM_PROPERTY ) )
  -        {
  -            String[] defStrs = getCli().getOptionValues( SET_SYSTEM_PROPERTY );
  +        //!! This whole pile of crap above needs to be sorted out. I'm sure some
  +        // of it isn't being used but we'll check on the maven user list first.
   
  -            for ( int i = 0; i < defStrs.length; ++i )
  -            {
  -                setCliProperty( defStrs[i] );
  -            }
  -        }
  +        initializeSystemProperties();
  +        initializeContext();
  +        initializeMavenBean();
  +        initializeProject( descriptorFile );
  +        initializeIO();
  +    }
   
  -        // Create the MavenJellyContext
  +    /**
  +     * Initialize the context that will be used by all the components
  +     * used in Maven.
  +     */
  +    private void initializeContext()
  +    {
  +        // The creation of the maven jelly context is the start of the whole
  +        // process. We use the context to hold all the information that is
  +        // shared between all the components used in maven.
           setContext( MavenUtils.createContext( getDescriptorDirectory() ) );
  -        getMaven().setContext( getContext() );
   
  -        initializeIO();
  +        // Set the descriptor directory.
  +        getContext().setDescriptorDirectory( getDescriptorDirectory() );
  +    }
   
  -        System.setProperty( "user.dir", getDescriptorDirectory().getPath() );
  +    /**
  +     * Initialize the maven bean.
  +     */
  +    private void initializeMavenBean()
  +    {
  +        Maven maven = new Maven();
  +        maven.setContext( getContext() );
  +        maven.addGoalNames( getCli().getArgList() );
  +        getContext().setMaven( maven );
       }
   
       /**
  @@ -391,7 +385,7 @@
                   + " file you specified has a length of zero." );
           }
   
  -        getMaven().setProject( MavenUtils.getProject( projectFile, getDescriptorDirectory() ) );
  +        getContext().setProject( MavenUtils.getProject( projectFile, getDescriptorDirectory() ) );
       }
   
       /**
  @@ -424,7 +418,11 @@
   
           if ( getCli().hasOption( EMACS_OUTPUT ) )
           {
  -            getMaven().setEmacsMode( true );
  +            getContext().setEmacsModeOn( Boolean.TRUE );
  +        }
  +        else
  +        {
  +            getContext().setEmacsModeOn( Boolean.FALSE );
           }
       }
   
  @@ -449,6 +447,23 @@
       }
   
       /**
  +     * Setup any system properties that have been specified on
  +     * the CLI.
  +     */
  +    public void initializeSystemProperties()
  +    {
  +        if ( getCli().hasOption( SET_SYSTEM_PROPERTY ) )
  +        {
  +            String[] defStrs = getCli().getOptionValues( SET_SYSTEM_PROPERTY );
  +
  +            for ( int i = 0; i < defStrs.length; ++i )
  +            {
  +                setCliProperty( defStrs[i] );
  +            }
  +        }
  +    }
  +
  +    /**
        * Set a property based upon a commandline <code>name=value</code> string.
        *
        *  @param defStr The <code>name=value</code> string.
  @@ -486,7 +501,7 @@
   
           try
           {
  -            initializeCore( args );
  +            initialize( args );
           }
           catch ( ParseException e )
           {
  @@ -531,15 +546,13 @@
               return;
           }
   
  -        Project mavenProject = getMaven().getProject();
  -
           if ( !getCli().hasOption( CONSOLE_BANNER ) )
           {
               printConsoleMavenHeader();
               System.out.println();
           }
   
  -        printConsoleProjectHeader( mavenProject );
  +        printConsoleProjectHeader( getContext().getProject() );
           System.out.println();
   
           boolean failed = false;
  @@ -547,7 +560,7 @@
           try
           {
               checkOnline();
  -            getMaven().initialize();
  +            getContext().getMaven().initialize();
   
               if ( getCli().hasOption( DISPLAY_GOALS ) )
               {
  @@ -558,8 +571,8 @@
               }
               else
               {
  -                getMaven().verifyProject();
  -                getMaven().attainGoals();
  +                getContext().getMaven().verifyProject();
  +                getContext().getMaven().attainGoals();
               }
           }
           catch ( UnknownGoalException e )
  @@ -591,7 +604,6 @@
           {
               failed = true;
               returnCode = handleUnattainableGoalException( e );
  -            // returnCode = RC_GOAL_FAILED;
           }
           catch ( JellyException e )
           {
  @@ -645,32 +657,32 @@
   
           System.out.println( "" );
           System.err.println( "BUILD FAILED" );
  -        
  +
           String msg = null;
  -        
  +
           String fileName = null;
           String elementName = null;
  -        
  +
           int lineNumber = -1;
           int column = -1;
  -        
  +
           Throwable rootCause = e.getRootCause();
  -        
  +
           if ( rootCause != null )
           {
               if ( rootCause instanceof JellyException )
               {
                   returnCode = RC_JELLY_FAILED;
                   JellyException jellyEx = (JellyException) rootCause;
  -                
  +
                   fileName = jellyEx.getFileName();
                   elementName = jellyEx.getElementName();
  -                
  +
                   lineNumber = jellyEx.getLineNumber();
                   column = jellyEx.getColumnNumber();
  -                
  +
                   rootCause = jellyEx.getCause();
  -                
  +
                   if ( rootCause == null )
                   {
                       msg = jellyEx.getReason();
  @@ -681,9 +693,9 @@
                       {
                           rootCause.printStackTrace();
                       }
  -                    
  +
                       msg = rootCause.getLocalizedMessage();
  -                    
  +
                       if ( msg == null )
                       {
                           msg = rootCause.getClass().getName();
  @@ -694,7 +706,7 @@
               {
                   rootCause.printStackTrace();
                   msg = rootCause.getLocalizedMessage();
  -                
  +
                   if ( msg == null )
                   {
                       msg = rootCause.getClass().getName();
  @@ -705,7 +717,7 @@
           {
               msg = e.getLocalizedMessage();
           }
  -        
  +
           if ( fileName != null )
           {
               System.err.println( "File...... " + fileName );
  @@ -713,9 +725,9 @@
               System.err.println( "Line...... " + lineNumber );
               System.err.println( "Column.... " + column );
           }
  -        
  +
           System.err.println( msg );
  -        
  +
           if ( getCli().hasOption( DEBUG_X ) )
           {
               e.printStackTrace();
  @@ -949,13 +961,13 @@
           System.out.println();
           System.out.println( "Non documented goals : " );
           System.out.println();
  -        
  +
           for ( Iterator i = list.iterator(); i.hasNext(); )
           {
               String goalName = (String) i.next();
  -            
  +
               String goalDescription = getContext().getPluginManager().getGoalDescription( goalName );
  -            
  +
               boolean hasDesc = false;
   
               /*
  @@ -969,7 +981,7 @@
               {
                   hasDesc = goalDescription.trim().length() != 0;
               }
  -            
  +
               if ( !hasDesc )
               {
                   System.out.println( "  " + goalName );
  @@ -1105,10 +1117,7 @@
           throws Exception
       {
           Date start = new Date();
  -        Maven maven = new Maven();
  -
           App app = new App();
  -        app.setMaven( maven );
           app.doMain( args, start );
       }
   }