You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by jv...@apache.org on 2009/03/10 05:31:11 UTC

svn commit: r751980 - in /maven/components/branches/MNG-2766: maven-core/src/main/java/org/apache/maven/execution/ maven-core/src/main/java/org/apache/maven/lifecycle/ maven-core/src/main/java/org/apache/maven/plugin/ maven-core/src/test/java/org/apach...

Author: jvanzyl
Date: Tue Mar 10 04:31:10 2009
New Revision: 751980

URL: http://svn.apache.org/viewvc?rev=751980&view=rev
Log:
o my test for executing a mojo in isolation is now complete and passes and gives some real indication of the problem that  exists in trying to expose the plugin manager to any external client. the test is pretty nasty but now i can
  work against this while i refactor. i can now merge to trunk.


Modified:
    maven/components/branches/MNG-2766/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java
    maven/components/branches/MNG-2766/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java
    maven/components/branches/MNG-2766/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java
    maven/components/branches/MNG-2766/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java
    maven/components/branches/MNG-2766/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestPopulator.java

Modified: maven/components/branches/MNG-2766/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java
URL: http://svn.apache.org/viewvc/maven/components/branches/MNG-2766/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java?rev=751980&r1=751979&r2=751980&view=diff
==============================================================================
--- maven/components/branches/MNG-2766/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java (original)
+++ maven/components/branches/MNG-2766/maven-core/src/main/java/org/apache/maven/execution/MavenSession.java Tue Mar 10 04:31:10 2009
@@ -68,20 +68,20 @@
     
     private MavenRealmManager realmManager;
     
-    public MavenSession( ArtifactRepository localRepository, MavenRealmManager realmManager )    
+    private List<String> pluginGroups;
+    
+    public MavenSession( ArtifactRepository localRepository, List<String> pluginGroups, MavenRealmManager realmManager )    
     {        
         this.localRepository = localRepository;
+        this.pluginGroups = pluginGroups;
         this.realmManager = realmManager;
     }
     
     public MavenSession( PlexusContainer container, MavenExecutionRequest request, EventDispatcher eventDispatcher, ReactorManager reactorManager )
     {
         this.container = container;
-
         this.request = request;
-
         this.eventDispatcher = eventDispatcher;
-
         this.reactorManager = reactorManager;
     }
 
@@ -97,6 +97,11 @@
 
     public Map getPluginContext( PluginDescriptor pluginDescriptor, MavenProject project )
     {
+        if ( reactorManager == null )
+        {
+            return null;
+        }
+        
         return reactorManager.getPluginContext( pluginDescriptor, project );
     }
 
@@ -289,6 +294,11 @@
     
     public List<String> getPluginGroups()
     {
+        if ( pluginGroups != null )
+        {
+            return pluginGroups;            
+        }
+        
         return request.getPluginGroups();
     }
     

Modified: maven/components/branches/MNG-2766/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java
URL: http://svn.apache.org/viewvc/maven/components/branches/MNG-2766/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java?rev=751980&r1=751979&r2=751980&view=diff
==============================================================================
--- maven/components/branches/MNG-2766/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java (original)
+++ maven/components/branches/MNG-2766/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycleExecutor.java Tue Mar 10 04:31:10 2009
@@ -167,7 +167,7 @@
         
         try
         {
-            descriptor = getMojoDescriptor( task, session, project, task, true, false );
+            descriptor = getMojoDescriptor( task, session, project );
         }
         catch ( LifecycleExecutionException e )
         {
@@ -423,7 +423,7 @@
                 }
                 else
                 {
-                    MojoDescriptor mojo = getMojoDescriptor( task, session, project, task, true, false );
+                    MojoDescriptor mojo = getMojoDescriptor( task, session, project );
 
                     // if the mojo descriptor was found, determine aggregator status according to:
                     // 1. whether the mojo declares itself an aggregator
@@ -522,7 +522,7 @@
         throws LifecycleExecutionException, BuildFailureException, PluginNotFoundException
     {
         // guaranteed to come from the CLI and not be part of a phase
-        MojoDescriptor mojoDescriptor = getMojoDescriptor( task, session, project, task, true, false );
+        MojoDescriptor mojoDescriptor = getMojoDescriptor( task, session, project );
         executeGoals( Collections.singletonList( new MojoExecution( mojoDescriptor ) ), forkEntryPoints, session, project );
     }
 
@@ -532,8 +532,6 @@
         for ( Iterator i = goals.iterator(); i.hasNext(); )
         {
             MojoExecution mojoExecution = (MojoExecution) i.next();
-
-            System.out.println( ">> " + mojoExecution );
             
             MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
 
@@ -1007,7 +1005,7 @@
                     String goal = tok.nextToken().trim();
 
                     // Not from the CLI, don't use prefix
-                    MojoDescriptor mojoDescriptor = getMojoDescriptor( goal, session, project, selectedPhase, false, optionalMojos.contains( goal ) );
+                    MojoDescriptor mojoDescriptor = getMojoDescriptor( goal, session, project );
 
                     if ( mojoDescriptor == null )
                     {
@@ -1230,41 +1228,40 @@
         return goals;
     }
 
-    private MojoDescriptor getMojoDescriptor( String task, MavenSession session, MavenProject project, String invokedVia, boolean canUsePrefix, boolean isOptionalMojo )
+    MojoDescriptor getMojoDescriptor( String task, MavenSession session, MavenProject project )
         throws LifecycleExecutionException
     {
         String goal;
         Plugin plugin;
 
-        PluginDescriptor pluginDescriptor = null;
-
-        StringTokenizer tok = new StringTokenizer( task, ":" );
-        int numTokens = tok.countTokens();
-
-        if ( numTokens == 2 )
+        PluginDescriptor pluginDescriptor = null;        
+        String[] taskSegments = StringUtils.split( task, ":" );
+        
+        if ( taskSegments.length == 2 )
         {
-            if ( !canUsePrefix )
-            {
-                String msg = "Mapped-prefix lookup of mojos are only supported from direct invocation. " + "Please use specification of the form groupId:artifactId[:version]:goal instead. "
-                    + "(Offending mojo: \'" + task + "\', invoked via: \'" + invokedVia + "\')";
-                throw new LifecycleExecutionException( msg );
-            }
-
-            String prefix = tok.nextToken();
-            goal = tok.nextToken();
-
-            plugin = pluginManager.findPluginForPrefix( prefix, project, session );
+            String prefix = taskSegments[0];
+            goal = taskSegments[1];
             
+            // This is the case where someone has executed a single goal from the command line
+            // of the form:
+            //
+            // mvn remote-resources:process
+            //
+            // From the metadata stored on the server which has been created as part of a standard
+            // Maven plugin deployment we will find the right PluginDescriptor from the remote
+            // repository.
+            
+            plugin = pluginManager.findPluginForPrefix( prefix, project, session );
+                        
             if ( plugin == null )
             {
                 plugin = new Plugin();
-
                 plugin.setGroupId( pluginDescriptor.getGroupId() );
                 plugin.setArtifactId( pluginDescriptor.getArtifactId() );
                 plugin.setVersion( pluginDescriptor.getVersion() );
             }
 
-            // 3. search plugins in the current POM
+            // Search plugin in the current POM
             if ( plugin == null )
             {
                 for ( Iterator i = project.getBuildPlugins().iterator(); i.hasNext(); )
@@ -1280,7 +1277,7 @@
                 }
             }
 
-            // 4. default to o.a.m.plugins and maven-<prefix>-plugin
+            // Default to o.a.m.plugins and maven-<prefix>-plugin
             if ( plugin == null )
             {
                 plugin = new Plugin();
@@ -1288,19 +1285,18 @@
                 plugin.setArtifactId( PluginDescriptor.getDefaultPluginArtifactId( prefix ) );
             }
         }
-        else if ( numTokens == 3 || numTokens == 4 )
+        else if ( taskSegments.length == 3 || taskSegments.length == 4 )
         {
             plugin = new Plugin();
+            plugin.setGroupId( taskSegments[0] );
+            plugin.setArtifactId( taskSegments[1] );
 
-            plugin.setGroupId( tok.nextToken() );
-            plugin.setArtifactId( tok.nextToken() );
-
-            if ( numTokens == 4 )
+            if ( taskSegments.length == 4 )
             {
-                plugin.setVersion( tok.nextToken() );
+                plugin.setVersion( taskSegments[3] );
             }
 
-            goal = tok.nextToken();
+            goal = taskSegments[4];
         }
         else
         {
@@ -1334,18 +1330,7 @@
         project.addPlugin( plugin );
 
         MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo( goal );
-        if ( mojoDescriptor == null )
-        {
-            if ( isOptionalMojo )
-            {
-                logger.info( "Skipping missing optional mojo: " + task );
-            }
-            else
-            {
-                throw new LifecycleExecutionException( "Required goal not found: " + task + " in " + pluginDescriptor.getId() );
-            }
-        }
-
+        
         return mojoDescriptor;
     }
 

Modified: maven/components/branches/MNG-2766/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java
URL: http://svn.apache.org/viewvc/maven/components/branches/MNG-2766/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java?rev=751980&r1=751979&r2=751980&view=diff
==============================================================================
--- maven/components/branches/MNG-2766/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java (original)
+++ maven/components/branches/MNG-2766/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java Tue Mar 10 04:31:10 2009
@@ -136,6 +136,7 @@
     @Requirement
     protected MavenProjectBuilder mavenProjectBuilder;
 
+    //!!jvz This is one of the last components to remove the legacy system.
     @Requirement
     protected RepositoryMetadataManager repositoryMetadataManager;    
     
@@ -413,13 +414,16 @@
     // Mojo execution
     // ----------------------------------------------------------------------
 
+    public void executeMojo( MavenProject project, MojoExecution mojoExecution )
+        throws MojoFailureException, PluginExecutionException, PluginConfigurationException
+    {
+    }    
+    
     public void executeMojo( MavenProject project, MojoExecution mojoExecution, MavenSession session )
         throws MojoFailureException, PluginExecutionException, PluginConfigurationException
     {
         MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
 
-        // NOTE: I'm putting these checks in here, since this is the central point of access for
-        // anything that wants to execute a mojo.
         if ( mojoDescriptor.isProjectRequired() && !session.isUsingPOMsFromFilesystem() )
         {
             throw new PluginExecutionException( mojoExecution, project, "Cannot execute mojo: " + mojoDescriptor.getGoal()
@@ -495,10 +499,9 @@
             try
             {
                 List<InterpolatorProperty> interpolatorProperties = new ArrayList<InterpolatorProperty>();
-                interpolatorProperties.addAll( InterpolatorProperty.toInterpolatorProperties( session.getProjectBuilderConfiguration().getExecutionProperties(),
-                                                                                              PomInterpolatorTag.EXECUTION_PROPERTIES.name() ) );
-                interpolatorProperties
-                    .addAll( InterpolatorProperty.toInterpolatorProperties( session.getProjectBuilderConfiguration().getUserProperties(), PomInterpolatorTag.USER_PROPERTIES.name() ) );
+                interpolatorProperties.addAll( InterpolatorProperty.toInterpolatorProperties(
+                session.getProjectBuilderConfiguration().getExecutionProperties(), PomInterpolatorTag.EXECUTION_PROPERTIES.name() ) );
+                interpolatorProperties.addAll( InterpolatorProperty.toInterpolatorProperties( session.getProjectBuilderConfiguration().getUserProperties(), PomInterpolatorTag.USER_PROPERTIES.name() ) );
                 String interpolatedDom = interpolateXmlString( String.valueOf( dom ), interpolatorProperties );
                 dom = Xpp3DomBuilder.build( new StringReader( interpolatedDom ) );
             }
@@ -532,7 +535,7 @@
         {
             mojo = getConfiguredMojo( session, dom, project, false, mojoExecution, realmActions );
 
-            dispatcher.dispatchStart( event, goalExecId );
+            //dispatcher.dispatchStart( event, goalExecId );
 
             pluginRealm = pluginDescriptor.getClassRealm();
 
@@ -561,7 +564,7 @@
                 session.addReport( mojoDescriptor, (MavenReport) mojo );
             }
 
-            dispatcher.dispatchEnd( event, goalExecId );
+            //dispatcher.dispatchEnd( event, goalExecId );
         }
         catch ( MojoExecutionException e )
         {
@@ -764,11 +767,14 @@
             {
                 Map pluginContext = session.getPluginContext( pluginDescriptor, project );
 
-                pluginContext.put( "project", project );
+                if ( pluginContext != null )
+                {
+                    pluginContext.put( "project", project );
 
-                pluginContext.put( "pluginDescriptor", pluginDescriptor );
+                    pluginContext.put( "pluginDescriptor", pluginDescriptor );
 
-                ( (ContextEnabled) mojo ).setPluginContext( pluginContext );
+                    ( (ContextEnabled) mojo ).setPluginContext( pluginContext );
+                }
             }
 
             mojo.setLog( new DefaultLog( logger ) );
@@ -1876,6 +1882,7 @@
         }
     }
 
+    //!!jvz This should not be here, it's part of pre-processing.
     private void loadPluginMappings( String groupId, List pluginRepositories, ArtifactRepository localRepository )
         throws RepositoryMetadataResolutionException
     {

Modified: maven/components/branches/MNG-2766/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java
URL: http://svn.apache.org/viewvc/maven/components/branches/MNG-2766/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java?rev=751980&r1=751979&r2=751980&view=diff
==============================================================================
--- maven/components/branches/MNG-2766/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java (original)
+++ maven/components/branches/MNG-2766/maven-core/src/test/java/org/apache/maven/lifecycle/LifecycleExecutorTest.java Tue Mar 10 04:31:10 2009
@@ -1,12 +1,19 @@
 package org.apache.maven.lifecycle;
 
 import java.io.File;
+import java.util.Arrays;
+import java.util.Properties;
 
 import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.execution.DefaultMavenExecutionRequest;
+import org.apache.maven.execution.MavenExecutionRequest;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.model.Plugin;
+import org.apache.maven.model.PluginExecution;
+import org.apache.maven.model.Repository;
 import org.apache.maven.plugin.MavenPluginCollector;
 import org.apache.maven.plugin.MavenPluginDiscoverer;
+import org.apache.maven.plugin.MojoExecution;
 import org.apache.maven.plugin.PluginManager;
 import org.apache.maven.plugin.descriptor.MojoDescriptor;
 import org.apache.maven.plugin.descriptor.PluginDescriptor;
@@ -19,22 +26,31 @@
 import org.apache.maven.repository.RepositorySystem;
 import org.codehaus.plexus.ContainerConfiguration;
 import org.codehaus.plexus.PlexusTestCase;
+import org.codehaus.plexus.component.annotations.Requirement;
 import org.codehaus.plexus.logging.console.ConsoleLogger;
+import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.xml.Xpp3Dom;
 
 public class LifecycleExecutorTest
     extends PlexusTestCase
 {
+    @Requirement
     private MavenProjectBuilder projectBuilder;
     
+    @Requirement
     private RepositorySystem repositorySystem;
     
+    @Requirement
     private PluginManager pluginManager;
     
+    @Requirement
     private DefaultLifecycleExecutor lifecycleExecutor;
 
     protected void setUp()
         throws Exception
     {
+        //!!jvz need these injected into the test cases as this is a pita.
         projectBuilder = lookup( MavenProjectBuilder.class );        
         repositorySystem = lookup( RepositorySystem.class );        
         pluginManager = lookup( PluginManager.class );        
@@ -49,39 +65,83 @@
         // - configure the plugin [extension point]
         // - execute the plugin    
         
+        // Our test POM and this is actually the Maven POM so not the best idea.
         File pom = new File( getBasedir(), "src/test/pom.xml" );
-       
-        // For testing I want to use my standard local repository and settings.
+        File targetPom = new File( getBasedir(), "target/pom-plugin.xml" );
+        FileUtils.copyFile( pom, targetPom );
+        if ( !targetPom.getParentFile().exists() )
+        {
+            targetPom.getParentFile().mkdirs();
+        }
         
         ArtifactRepository localRepository = repositorySystem.createLocalRepository( new File( "/Users/jvanzyl/.m2/repository" ) ); 
         
+        Repository repository = new Repository();
+        repository.setUrl( "http://repo1.maven.org/maven2" );
+        repository.setId( "central" );
         ProjectBuilderConfiguration configuration = new DefaultProjectBuilderConfiguration()
             .setLocalRepository( localRepository )
-            .setRemoteRepositories( null );
-        
-        MavenProject project = projectBuilder.build( pom, configuration );
-        
-        // now i want to grab the configuration for the remote resources plugin
+            .setRemoteRepositories( Arrays.asList( repositorySystem.buildArtifactRepository( repository ) ) );
         
+        MavenProject project = projectBuilder.build( targetPom, configuration );              
         assertEquals( "maven", project.getArtifactId() );
+        assertEquals( "3.0-SNAPSHOT", project.getVersion() );
         
-        Plugin plugin = new Plugin();
-        plugin.setGroupId( "org.apache.maven.plugins" );
-        plugin.setArtifactId( "maven-remote-resources-plugin" );
-        // The version should be specified in the POM.
-        
-        MavenRealmManager realmManager = new DefaultMavenRealmManager( getContainer(), new ConsoleLogger( 0, "logger" ) );        
-        MavenSession session = new MavenSession( localRepository, realmManager );
+        // We need a local repository and realm manager in the session in order to execute a mojo. This
+        // is not really a good situation.
+        MavenRealmManager realmManager = new DefaultMavenRealmManager( getContainer(), new ConsoleLogger( 0, "logger" ) );  
+        
+        MavenExecutionRequest request = new DefaultMavenExecutionRequest()
+            .setProjectPresent( true )
+            .setPluginGroups( Arrays.asList( new String[]{ "org.apache.maven.plugins"} ) )
+            .setLocalRepository( localRepository )
+            .setRemoteRepositories( Arrays.asList( repositorySystem.buildArtifactRepository( repository ) ) )
+            .setRealmManager( realmManager )
+            .setProperties( new Properties() );
+                                      
+        MavenSession session = new MavenSession( getContainer(), request, null, null );
+        session.setCurrentProject( project );
+              
+        String pluginArtifactId = "remote-resources";
+        String goal = "process";
+        MojoDescriptor mojoDescriptor = lifecycleExecutor.getMojoDescriptor( pluginArtifactId + ":" + goal, session, project );        
         
-        PluginDescriptor pd = pluginManager.loadPlugin( plugin, project, session );        
+        PluginDescriptor pd = mojoDescriptor.getPluginDescriptor();
         assertNotNull( pd );
         assertEquals( "org.apache.maven.plugins", pd.getGroupId() );
         assertEquals( "maven-remote-resources-plugin", pd.getArtifactId() );
         assertEquals( "1.0", pd.getVersion() );        
         
-        MojoDescriptor mojoDescriptor = pd.getMojo( "process" );
-        assertNotNull( mojoDescriptor );
-        System.out.println( "configuration >>> " + mojoDescriptor.getConfiguration() );
+        MojoExecution me = new MojoExecution( mojoDescriptor );       
+        
+        for ( Plugin bp : project.getBuildPlugins() )
+        {
+            // The POM builder should have merged any configuration at the upper level of the plugin
+            // element with the execution level configuration where our goal is.
+            
+            // We have our plugin
+            if ( bp.getArtifactId().equals( pd.getArtifactId() ) )
+            {
+                // Search through executions
+                for( PluginExecution e : bp.getExecutions() )
+                {         
+                    // Search though goals to match what's been asked for.
+                    for ( String g : e.getGoals() )
+                    {
+                        // This goal matches what's been asked for.                            
+                        if( g.equals( goal ) )
+                        {
+                            me.setConfiguration( (Xpp3Dom) e.getConfiguration() );
+                        }
+                    }
+                }
+            }
+        }
+        
+        // Need some xpath action in here. Make sure the mojoExecution configuration is intact
+                
+        // Now the magical mojo descriptor is complete and I can execute the mojo.
+        pluginManager.executeMojo( project, me, session );        
     }
     
     protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration )

Modified: maven/components/branches/MNG-2766/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestPopulator.java
URL: http://svn.apache.org/viewvc/maven/components/branches/MNG-2766/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestPopulator.java?rev=751980&r1=751979&r2=751980&view=diff
==============================================================================
--- maven/components/branches/MNG-2766/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestPopulator.java (original)
+++ maven/components/branches/MNG-2766/maven-embedder/src/main/java/org/apache/maven/embedder/execution/DefaultMavenExecutionRequestPopulator.java Tue Mar 10 04:31:10 2009
@@ -17,6 +17,7 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
@@ -174,7 +175,7 @@
         Settings settings = request.getSettings();
 
         request.setPluginGroups( settings.getPluginGroups() );
-
+        
         List<org.apache.maven.settings.Profile> settingsProfiles = settings.getProfiles();
 
         List<String> settingsActiveProfileIds = settings.getActiveProfiles();