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.



git-svn-id: https://svn.apache.org/repos/asf/maven/components/branches/MNG-2766@751980 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jason van Zyl 2009-03-10 04:31:10 +00:00
parent da776815ac
commit 191aded423
5 changed files with 142 additions and 79 deletions

View File

@ -68,20 +68,20 @@ public class MavenSession
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 class MavenSession
public Map getPluginContext( PluginDescriptor pluginDescriptor, MavenProject project )
{
if ( reactorManager == null )
{
return null;
}
return reactorManager.getPluginContext( pluginDescriptor, project );
}
@ -289,6 +294,11 @@ public class MavenSession
public List<String> getPluginGroups()
{
if ( pluginGroups != null )
{
return pluginGroups;
}
return request.getPluginGroups();
}

View File

@ -167,7 +167,7 @@ public class DefaultLifecycleExecutor
try
{
descriptor = getMojoDescriptor( task, session, project, task, true, false );
descriptor = getMojoDescriptor( task, session, project );
}
catch ( LifecycleExecutionException e )
{
@ -423,7 +423,7 @@ public class DefaultLifecycleExecutor
}
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 @@ public class DefaultLifecycleExecutor
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 @@ public class DefaultLifecycleExecutor
for ( Iterator i = goals.iterator(); i.hasNext(); )
{
MojoExecution mojoExecution = (MojoExecution) i.next();
System.out.println( ">> " + mojoExecution );
MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
@ -1007,7 +1005,7 @@ public class DefaultLifecycleExecutor
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 @@ public class DefaultLifecycleExecutor
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 @@ public class DefaultLifecycleExecutor
}
}
// 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 @@ public class DefaultLifecycleExecutor
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 @@ public class DefaultLifecycleExecutor
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;
}

View File

@ -136,6 +136,7 @@ public class DefaultPluginManager
@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 @@ public class DefaultPluginManager
// 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 @@ public class DefaultPluginManager
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 @@ public class DefaultPluginManager
{
mojo = getConfiguredMojo( session, dom, project, false, mojoExecution, realmActions );
dispatcher.dispatchStart( event, goalExecId );
//dispatcher.dispatchStart( event, goalExecId );
pluginRealm = pluginDescriptor.getClassRealm();
@ -561,7 +564,7 @@ public class DefaultPluginManager
session.addReport( mojoDescriptor, (MavenReport) mojo );
}
dispatcher.dispatchEnd( event, goalExecId );
//dispatcher.dispatchEnd( event, goalExecId );
}
catch ( MojoExecutionException e )
{
@ -764,11 +767,14 @@ public class DefaultPluginManager
{
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 @@ public class DefaultPluginManager
}
}
//!!jvz This should not be here, it's part of pre-processing.
private void loadPluginMappings( String groupId, List pluginRepositories, ArtifactRepository localRepository )
throws RepositoryMetadataResolutionException
{

View File

@ -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.realm.MavenRealmManager;
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 @@ public class LifecycleExecutorTest
// - 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.
// 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" ) );
MavenRealmManager realmManager = new DefaultMavenRealmManager( getContainer(), new ConsoleLogger( 0, "logger" ) );
MavenSession session = new MavenSession( localRepository, realmManager );
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 )

View File

@ -17,6 +17,7 @@ package org.apache.maven.embedder.execution;
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 @@ public class DefaultMavenExecutionRequestPopulator
Settings settings = request.getSettings();
request.setPluginGroups( settings.getPluginGroups() );
List<org.apache.maven.settings.Profile> settingsProfiles = settings.getProfiles();
List<String> settingsActiveProfileIds = settings.getActiveProfiles();