You are viewing a plain text version of this content. The canonical link for it is here.
Posted to m2-dev@maven.apache.org by jv...@apache.org on 2004/07/03 03:39:46 UTC

cvs commit: maven-components/maven-core/src/test/java/org/apache/maven/plugin PluginTest.java

jvanzyl     2004/07/02 18:39:45

  Modified:    maven-core/src/main/java/org/apache/maven Maven.java
                        MavenCli.java
               maven-core/src/main/resources/META-INF/plexus components.xml
               maven-core/src/test/java/org/apache/maven MavenTest.java
               maven-core/src/test/java/org/apache/maven/plugin
                        PluginTest.java
  Added:       maven-core/src/main/java/org/apache/maven DefaultMaven.java
  Removed:     maven-core/src/main/java/org/apache/maven
                        DefaultMavenCore.java MavenCore.java
  Log:
  o some changes brought about by trying to embed the maven component inside
    continuum and mide.
  
  Revision  Changes    Path
  1.8       +29 -98    maven-components/maven-core/src/main/java/org/apache/maven/Maven.java
  
  Index: Maven.java
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-core/src/main/java/org/apache/maven/Maven.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Maven.java	1 Jul 2004 01:55:43 -0000	1.7
  +++ Maven.java	3 Jul 2004 01:39:45 -0000	1.8
  @@ -17,7 +17,6 @@
    */
   
   import java.io.File;
  -import java.net.URL;
   import java.util.List;
   import java.util.Map;
   
  @@ -26,138 +25,70 @@
   import org.apache.maven.project.MavenProject;
   import org.apache.maven.project.ProjectBuildingException;
   
  -import org.codehaus.classworlds.ClassWorld;
  -import org.codehaus.plexus.embed.Embedder;
  -
   /**
    * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
    * @version $Id$
    */
  -public class Maven
  -    implements MavenCore
  +public interface Maven
   {
  -    private MavenCore maven;
  -
  -    public Maven( String mavenHome, ClassWorld bootClassWorld )
  -        throws Exception
  -    {
  -        Embedder embedder = new Embedder();
  -
  -        ClassWorld classWorld;
  -
  -        if ( bootClassWorld == null )
  -        {
  -            classWorld = new ClassWorld();
  -
  -            classWorld.newRealm( "core", embedder.getClass().getClassLoader() );
  -        }
  -        else
  -        {
  -            classWorld = bootClassWorld;
  -        }
  -
  -        embedder.addContextValue( "maven.home", mavenHome );
  -
  -        embedder.start( classWorld );
  -
  -        maven = (MavenCore) embedder.lookup( MavenCore.ROLE );
  -    }
  +    static String ROLE = Maven.class.getName();
   
       // ----------------------------------------------------------------------
  -    // Project execution
  +    // Execution
       // ----------------------------------------------------------------------
   
  -    public ExecutionResponse execute( List goals )
  -        throws GoalNotFoundException
  -    {
  -        return maven.execute( (MavenProject) null, goals );
  -    }
  +    ExecutionResponse execute( List goals )
  +        throws GoalNotFoundException;
   
  -    public ExecutionResponse execute( File projectFile, List goals )
  -        throws ProjectBuildingException, GoalNotFoundException
  -    {
  -        return maven.execute( getProject( projectFile ), goals );
  -    }
  +    ExecutionResponse execute( MavenProject project, List goals )
  +        throws GoalNotFoundException;
   
  -    public ExecutionResponse execute( MavenProject project, List goals )
  -        throws GoalNotFoundException
  -    {
  -        return maven.execute( project, goals );
  -    }
  +    ExecutionResponse execute( File project, List goals )
  +        throws ProjectBuildingException, GoalNotFoundException;
   
       // ----------------------------------------------------------------------
       // Reactor execution
       // ----------------------------------------------------------------------
   
  -    public ExecutionResponse executeReactor( String goal, String includes, String excludes )
  -        throws ReactorException, GoalNotFoundException
  -    {
  -        return maven.executeReactor( goal, includes, excludes );
  -    }
  +    ExecutionResponse executeReactor( String goal, String includes, String excludes )
  +        throws ReactorException, GoalNotFoundException;
   
       // ----------------------------------------------------------------------
       // Plugin descriptors
       // ----------------------------------------------------------------------
   
  -    public Map getPluginDescriptors()
  -    {
  -        return maven.getPluginDescriptors();
  -    }
  -
  -    public PluginDescriptor getPluginDescriptor( String pluginId )
  -    {
  -        return maven.getPluginDescriptor( pluginId );
  -    }
  +    Map getPluginDescriptors();
  +
  +    PluginDescriptor getPluginDescriptor( String pluginId );
   
       // ----------------------------------------------------------------------
       // Goal descriptors
       // ----------------------------------------------------------------------
   
  -    public Map getMojoDescriptors()
  -    {
  -        return maven.getMojoDescriptors();
  -    }
  -
  -    public MojoDescriptor getMojoDescriptor( String goalId )
  -    {
  -        return maven.getMojoDescriptor( goalId );
  -    }
  +    Map getMojoDescriptors();
  +
  +    MojoDescriptor getMojoDescriptor( String goalId );
   
       // ----------------------------------------------------------------------
  -    // Project building
  +    // Maven home
       // ----------------------------------------------------------------------
   
  -    public MavenProject getProject( File project )
  -        throws ProjectBuildingException
  -    {
  -        return maven.getProject( project );
  -    }
  +    void setMavenHome( String mavenHome );
  +
  +    String getMavenHome();
   
       // ----------------------------------------------------------------------
  -    // Maven home
  +    // Maven local repository
       // ----------------------------------------------------------------------
   
  -    public void setMavenHome( String mavenHome )
  -    {
  -        maven.setMavenHome( mavenHome );
  -    }
  -
  -    public String getMavenHome()
  -    {
  -        return maven.getMavenHome();
  -    }
  +    void setLocalRepository( String localRepository );
  +
  +    String getLocalRepository();
   
       // ----------------------------------------------------------------------
  -    // Maven local repository
  +    // Maven project handling
       // ----------------------------------------------------------------------
   
  -    public void setLocalRepository( String localRepository )
  -    {
  -        maven.setLocalRepository( localRepository );
  -    }
  -
  -    public String getLocalRepository()
  -    {
  -        return maven.getLocalRepository();
  -    }
  +    public MavenProject getProject( File project )
  +        throws ProjectBuildingException;
   }
  
  
  
  1.8       +54 -30    maven-components/maven-core/src/main/java/org/apache/maven/MavenCli.java
  
  Index: MavenCli.java
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-core/src/main/java/org/apache/maven/MavenCli.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- MavenCli.java	2 Jul 2004 02:23:30 -0000	1.7
  +++ MavenCli.java	3 Jul 2004 01:39:45 -0000	1.8
  @@ -16,12 +16,6 @@
    * limitations under the License.
    */
   
  -import java.io.File;
  -import java.io.FileInputStream;
  -import java.util.Iterator;
  -import java.util.Properties;
  -import java.util.TreeMap;
  -
   import org.apache.commons.cli.CommandLine;
   import org.apache.commons.cli.CommandLineParser;
   import org.apache.commons.cli.HelpFormatter;
  @@ -30,8 +24,14 @@
   import org.apache.commons.cli.ParseException;
   import org.apache.commons.cli.PosixParser;
   import org.apache.maven.plugin.descriptor.MojoDescriptor;
  -
   import org.codehaus.classworlds.ClassWorld;
  +import org.codehaus.plexus.embed.Embedder;
  +
  +import java.io.File;
  +import java.io.FileInputStream;
  +import java.util.Iterator;
  +import java.util.Properties;
  +import java.util.TreeMap;
   
   /**
    * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
  @@ -52,31 +52,15 @@
   
           initializeSystemProperties( commandLine );
   
  -        String mavenHome = System.getProperty( "maven.home" );
  +        Embedder embedder = new Embedder();
   
  -        Maven maven = new Maven( mavenHome, classWorld );
  +        embedder.addContextValue( "maven.home", findMavenHome() );
   
  -        Properties p = new Properties();
  +        embedder.start( classWorld );
   
  -        p.load( new FileInputStream( new File( System.getProperty( "user.home" ), "build.properties" ) ) );
  +        Maven maven = (Maven) embedder.lookup( Maven.ROLE );
   
  -        String localRepository = p.getProperty( MavenConstants.MAVEN_REPO_LOCAL );
  -
  -        if ( localRepository == null )
  -        {
  -            throw new Exception( "Missing 'maven.repo.local' from ~/build.properties." );
  -        }
  -
  -        System.out.println( "Using " + localRepository + " as local repo." );
  -
  -        maven.setLocalRepository( localRepository );
  -
  -        // ----------------------------------------------------------------------
  -        // We will look for project2.xml files for a while so that folks can
  -        // try out m2 without screwing up other developers using m1. m1 and m2
  -        // can co-exist happily together so this just makes it easy to use both
  -        // the v3 and v4 of the POM.
  -        // ----------------------------------------------------------------------
  +        maven.setLocalRepository( findLocalRepository() );
   
           File projectFile;
   
  @@ -160,7 +144,43 @@
           }
       }
   
  -    public static void initializeSystemProperties( CommandLine commandLine )
  +    // ----------------------------------------------------------------------
  +    // Local repository
  +    // ----------------------------------------------------------------------
  +
  +    private static String findLocalRepository()
  +        throws Exception
  +    {
  +        Properties p = new Properties();
  +
  +        p.load( new FileInputStream( new File( System.getProperty( "user.home" ), "build.properties" ) ) );
  +
  +        String localRepository = p.getProperty( MavenConstants.MAVEN_REPO_LOCAL );
  +
  +        if ( localRepository == null )
  +        {
  +            throw new Exception( "Missing 'maven.repo.local' from ~/build.properties." );
  +        }
  +
  +        return localRepository;
  +    }
  +
  +    // ----------------------------------------------------------------------
  +    // Maven home
  +    // ----------------------------------------------------------------------
  +
  +    private static String findMavenHome()
  +    {
  +        String mavenHome = System.getProperty( "maven.home" );
  +
  +        return mavenHome;
  +    }
  +
  +    // ----------------------------------------------------------------------
  +    // System properties handling
  +    // ----------------------------------------------------------------------
  +
  +    private static void initializeSystemProperties( CommandLine commandLine )
       {
           // Options that are set on the command line become system properties
           // and therefore are set in the session properties. System properties
  @@ -218,6 +238,10 @@
   
           System.setProperty( name, value );
       }
  +
  +    // ----------------------------------------------------------------------
  +    // Command line manager
  +    // ----------------------------------------------------------------------
   
       static class CLIManager
       {
  
  
  
  1.1                  maven-components/maven-core/src/main/java/org/apache/maven/DefaultMaven.java
  
  Index: DefaultMaven.java
  ===================================================================
  package org.apache.maven;
  
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  import java.io.File;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Date;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
  
  import org.apache.maven.lifecycle.MavenLifecycleContext;
  import org.apache.maven.lifecycle.MavenLifecycleManager;
  import org.apache.maven.plugin.PluginManager;
  import org.apache.maven.plugin.descriptor.MojoDescriptor;
  import org.apache.maven.plugin.descriptor.PluginDescriptor;
  import org.apache.maven.project.MavenProject;
  import org.apache.maven.project.MavenProjectBuilder;
  import org.apache.maven.project.ProjectBuildingException;
  
  import org.codehaus.plexus.PlexusConstants;
  import org.codehaus.plexus.PlexusContainer;
  import org.codehaus.plexus.context.Context;
  import org.codehaus.plexus.context.ContextException;
  import org.codehaus.plexus.i18n.I18N;
  import org.codehaus.plexus.logging.AbstractLogEnabled;
  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
  import org.codehaus.plexus.util.FileUtils;
  import org.codehaus.plexus.util.StringUtils;
  
  public class DefaultMaven
      extends AbstractLogEnabled
      implements Maven, Contextualizable
  {
      private PlexusContainer container;
  
      private String mavenHome;
  
      private String localRepository;
  
      private boolean logResults = true;
  
      // ----------------------------------------------------------------------
      // Components
      // ----------------------------------------------------------------------
  
      private PluginManager pluginManager;
  
      private MavenLifecycleManager lifecycleManager;
  
      private MavenProjectBuilder projectBuilder;
  
      private I18N i18n;
  
      // ----------------------------------------------------------------------
      // Project execution
      // ----------------------------------------------------------------------
  
      public ExecutionResponse execute( List goals )
          throws GoalNotFoundException
      {
          return execute( (MavenProject) null, goals );
      }
  
      public ExecutionResponse execute( File projectFile, List goals )
          throws ProjectBuildingException, GoalNotFoundException
      {
          return execute( getProject( projectFile ), goals );
      }
  
      public ExecutionResponse execute( MavenProject project, List goals )
          throws GoalNotFoundException
      {
          Date fullStop;
  
          Date fullStart = new Date();
  
          ExecutionResponse response = new ExecutionResponse();
  
          for ( Iterator iterator = goals.iterator(); iterator.hasNext(); )
          {
              String goal = (String) iterator.next();
  
              if ( !getMojoDescriptors().containsKey( goal ) )
              {
                  throw new GoalNotFoundException( goal );
              }
  
              MavenLifecycleContext context;
  
              try
              {
                  context = new MavenLifecycleContext( container, project, getMojoDescriptor( goal ) );
  
                  lifecycleManager.execute( context );
  
                  if ( context.isExecutionFailure() )
                  {
                      response.setExecutionFailure( context.getMojoDescriptor().getId(), context.getFailureResponse() );
  
                      break;
                  }
              }
              catch ( Exception e )
              {
                  response.setException( e );
  
                  if ( logResults )
                  {
                      line();
  
                      getLogger().error( "BUILD ERROR" );
  
                      line();
  
                      getLogger().error( "Cause: ", e );
  
                      line();
  
                      stats( fullStart, new Date() );
  
                      line();
                  }
              }
          }
  
          fullStop = new Date();
  
          if ( logResults )
          {
              if ( response.isExecutionFailure() )
              {
                  line();
  
                  getLogger().info( "BUILD FAILURE" );
  
                  line();
  
                  getLogger().info( "Reason: " + response.getFailureResponse().shortMessage() );
  
                  line();
  
                  getLogger().info( response.getFailureResponse().longMessage() );
  
                  line();
  
                  stats( fullStart, fullStop );
  
                  line();
              }
              else
              {
                  line();
  
                  getLogger().info( "BUILD SUCCESSFUL" );
  
                  line();
  
                  stats( fullStart, fullStop );
  
                  line();
              }
          }
  
          return response;
      }
  
      private void stats( Date fullStart, Date fullStop )
      {
          long fullDiff = fullStop.getTime() - fullStart.getTime();
  
          getLogger().info( "Total time: " + formatTime( fullDiff ) );
  
          getLogger().info( "Finished at: " + fullStop );
  
          final long mb = 1024 * 1024;
  
          System.gc();
  
          Runtime r = Runtime.getRuntime();
  
          getLogger().info( "Final Memory: " + ((r.totalMemory() - r.freeMemory()) / mb) + "M/" + (r.totalMemory() / mb) + "M");
  
      }
  
      private void line()
      {
          getLogger().info( "----------------------------------------------------------------------------" );
      }
  
      // ----------------------------------------------------------------------
      // Reactor execution
      // ----------------------------------------------------------------------
  
      public ExecutionResponse executeReactor( String goals, String includes, String excludes )
          throws ReactorException, GoalNotFoundException
      {
          List projects = new ArrayList();
  
          getLogger().info( "Starting the reactor..." );
  
          try
          {
              List files = FileUtils.getFiles( new File( System.getProperty( "user.dir" ) ), includes, excludes );
  
              for ( Iterator iterator = files.iterator(); iterator.hasNext(); )
              {
                  File f = (File) iterator.next();
  
                  MavenProject project = projectBuilder.build( f, getLocalRepository() );
  
                  projects.add( project );
              }
  
              projects = projectBuilder.getSortedProjects( projects );
          }
          catch ( Exception e )
          {
              throw new ReactorException( "Error processing projects for the reactor: ", e );
          }
  
          getLogger().info( "Our processing order:" );
  
          for ( Iterator iterator = projects.iterator(); iterator.hasNext(); )
          {
              MavenProject project = (MavenProject) iterator.next();
  
              getLogger().info( project.getName() );
          }
  
          List goalsList = Arrays.asList( StringUtils.split( goals, "," ) );
  
          ExecutionResponse response = null;
  
          for ( Iterator iterator = projects.iterator(); iterator.hasNext(); )
          {
              MavenProject project = (MavenProject) iterator.next();
  
              System.out.println( "\n\n\n" );
  
              line();
  
              getLogger().info( "Building " + project.getName() );
  
              line();
  
              response = execute( project, goalsList );
  
              if ( response.isExecutionFailure() )
              {
                  break;
              }
          }
  
          return response;
      }
  
      // ----------------------------------------------------------------------
      // Plugin descriptors
      // ----------------------------------------------------------------------
  
      public Map getPluginDescriptors()
      {
          return pluginManager.getPluginDescriptors();
      }
  
      public PluginDescriptor getPluginDescriptor( String pluginId )
      {
          return pluginManager.getPluginDescriptor( pluginId );
      }
  
      // ----------------------------------------------------------------------
      // Goal descriptors
      // ----------------------------------------------------------------------
  
      public Map getMojoDescriptors()
      {
          return pluginManager.getMojoDescriptors();
      }
  
      public MojoDescriptor getMojoDescriptor( String goalId )
      {
          return pluginManager.getMojoDescriptor( goalId );
      }
  
      // ----------------------------------------------------------------------
      // Project building
      // ----------------------------------------------------------------------
  
      public MavenProject getProject( File project )
          throws ProjectBuildingException
      {
          if ( project.exists() )
          {
              if ( project.length() == 0 )
              {
                  throw new ProjectBuildingException( i18n.format( "empty.descriptor.error", project.getName() ) );
              }
          }
  
          return projectBuilder.build( project, getLocalRepository() );
      }
  
      // ----------------------------------------------------------------------
      // Reactor
      // ----------------------------------------------------------------------
  
      public List getSortedProjects( List projects )
          throws Exception
      {
          return projectBuilder.getSortedProjects( projects );
      }
  
      // ----------------------------------------------------------------------
      // Maven home
      // ----------------------------------------------------------------------
  
      public void setMavenHome( String mavenHome )
      {
          this.mavenHome = mavenHome;
      }
  
      public String getMavenHome()
      {
          if ( mavenHome == null )
          {
              mavenHome = System.getProperty( MavenConstants.MAVEN_HOME );
          }
  
          return mavenHome;
      }
  
      // ----------------------------------------------------------------------
      // Maven local repository
      // ----------------------------------------------------------------------
  
      public void setLocalRepository( String localRepository )
      {
          this.localRepository = localRepository;
      }
  
      public String getLocalRepository()
      {
          return localRepository;
      }
  
      // ----------------------------------------------------------------------
      // Lifecylce Management
      // ----------------------------------------------------------------------
  
      public void contextualize( Context context )
          throws ContextException
      {
          container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
      }
  
      // ----------------------------------------------------------------------
      //
      // ----------------------------------------------------------------------
  
      protected static String formatTime( long ms )
      {
          long secs = ms / 1000;
          long min = secs / 60;
          secs = secs % 60;
  
          if ( min > 0 )
          {
              return min + " minutes " + secs + " seconds";
          }
          else
          {
              return secs + " seconds";
          }
      }
  }
  
  
  
  1.5       +2 -2      maven-components/maven-core/src/main/resources/META-INF/plexus/components.xml
  
  Index: components.xml
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-core/src/main/resources/META-INF/plexus/components.xml,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- components.xml	2 Jul 2004 02:23:30 -0000	1.4
  +++ components.xml	3 Jul 2004 01:39:45 -0000	1.5
  @@ -1,8 +1,8 @@
   <component-set>
     <components>
       <component>
  -      <role>org.apache.maven.MavenCore</role>
  -      <implementation>org.apache.maven.DefaultMavenCore</implementation>
  +      <role>org.apache.maven.Maven</role>
  +      <implementation>org.apache.maven.DefaultMaven</implementation>
         <requirements>
           <requirement>
             <role>org.apache.maven.plugin.PluginManager</role>
  
  
  
  1.4       +2 -2      maven-components/maven-core/src/test/java/org/apache/maven/MavenTest.java
  
  Index: MavenTest.java
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-core/src/test/java/org/apache/maven/MavenTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- MavenTest.java	27 Jun 2004 07:39:08 -0000	1.3
  +++ MavenTest.java	3 Jul 2004 01:39:45 -0000	1.4
  @@ -27,7 +27,7 @@
       public void testMaven()
           throws Exception
       {
  -        MavenCore maven = (MavenCore) lookup( MavenCore.ROLE );
  +        Maven maven = (Maven) lookup( Maven.ROLE );
   
           assertNotNull( maven );
       }
  
  
  
  1.5       +3 -3      maven-components/maven-core/src/test/java/org/apache/maven/plugin/PluginTest.java
  
  Index: PluginTest.java
  ===================================================================
  RCS file: /home/cvs/maven-components/maven-core/src/test/java/org/apache/maven/plugin/PluginTest.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- PluginTest.java	27 Jun 2004 07:39:08 -0000	1.4
  +++ PluginTest.java	3 Jul 2004 01:39:45 -0000	1.5
  @@ -16,7 +16,7 @@
    * limitations under the License.
    */
   
  -import org.apache.maven.MavenCore;
  +import org.apache.maven.Maven;
   import org.apache.maven.MavenTestCase;
   import org.apache.maven.plugin.descriptor.PluginDescriptor;
   import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
  @@ -75,7 +75,7 @@
       {
           registerPlugin( "integrated-plugin.xml" );
   
  -        MavenCore maven = (MavenCore) lookup( MavenCore.ROLE );
  +        Maven maven = (Maven) lookup( Maven.ROLE );
   
           List goals = new ArrayList();