o a test which demonstrates the difficulting in testing the execution of a single mojo, indicates some work to isolate the system and make it easier

for people to reuse this code. igor had to jump through these same hoops to get isolated mojo execution working in m2e. needs some love.


git-svn-id: https://svn.apache.org/repos/asf/maven/components/branches/MNG-2766@751582 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jason van Zyl 2009-03-09 04:51:13 +00:00
parent 51e30e4640
commit da776815ac
9 changed files with 153 additions and 53 deletions

View File

@ -63,6 +63,17 @@ public class MavenSession
private Map reports = new LinkedHashMap();
//For testing
private ArtifactRepository localRepository;
private MavenRealmManager realmManager;
public MavenSession( ArtifactRepository localRepository, MavenRealmManager realmManager )
{
this.localRepository = localRepository;
this.realmManager = realmManager;
}
public MavenSession( PlexusContainer container, MavenExecutionRequest request, EventDispatcher eventDispatcher, ReactorManager reactorManager )
{
this.container = container;
@ -76,11 +87,15 @@ public MavenSession( PlexusContainer container, MavenExecutionRequest request, E
public MavenRealmManager getRealmManager()
{
if ( realmManager != null )
{
return realmManager;
}
return request.getRealmManager();
}
public Map getPluginContext( PluginDescriptor pluginDescriptor,
MavenProject project )
public Map getPluginContext( PluginDescriptor pluginDescriptor, MavenProject project )
{
return reactorManager.getPluginContext( pluginDescriptor, project );
}
@ -89,9 +104,14 @@ public PlexusContainer getContainer()
{
return container;
}
public ArtifactRepository getLocalRepository()
{
if ( localRepository != null )
{
return localRepository;
}
return request.getLocalRepository();
}

View File

@ -927,7 +927,7 @@ private Lifecycle getLifecycleForPhase( String phase )
return lifecycle;
}
private MojoDescriptor getMojoDescriptor( PluginDescriptor pluginDescriptor, String goal )
MojoDescriptor getMojoDescriptor( PluginDescriptor pluginDescriptor, String goal )
throws LifecycleExecutionException
{
MojoDescriptor desc = pluginDescriptor.getMojo( goal );

View File

@ -258,7 +258,7 @@ else if ( projectPlugin.getVersion() == null || Artifact.RELEASE_VERSION.equals(
try
{
pluginRealm = realmManager.createPluginRealm( projectPlugin, pluginArtifact, artifacts, coreArtifactFilterManager.getArtifactFilter() );
logger.debug( "Created realm: " + pluginRealm + " for plugin: " + projectPlugin.getKey() );
}
catch ( RealmManagementException e )
@ -288,7 +288,7 @@ else if ( projectPlugin.getVersion() == null || Artifact.RELEASE_VERSION.equals(
logger.debug( "Checking for plugin descriptor for: " + projectPlugin.getKey() + " with version: " + projectPlugin.getVersion() + " in collector: " + pluginCollector );
PluginDescriptor pluginDescriptor = pluginCollector.getPluginDescriptor( projectPlugin );
if ( pluginDescriptor == null )
{
if ( ( pluginRealm != null ) && logger.isDebugEnabled() )
@ -1825,8 +1825,7 @@ public PluginDescriptor loadIsolatedPluginDescriptor( Plugin plugin, MavenProjec
// Plugin Mapping Manager
public org.apache.maven.model.Plugin getByPrefix( String pluginPrefix, List groupIds, List pluginRepositories,
ArtifactRepository localRepository )
public org.apache.maven.model.Plugin getByPrefix( String pluginPrefix, List groupIds, List pluginRepositories, ArtifactRepository localRepository )
{
// if not found, try from the remote repository
if ( !pluginDefinitionsByPrefix.containsKey( pluginPrefix ) )

View File

@ -0,0 +1,92 @@
package org.apache.maven.lifecycle;
import java.io.File;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.MavenPluginCollector;
import org.apache.maven.plugin.MavenPluginDiscoverer;
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.DefaultProjectBuilderConfiguration;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuilderConfiguration;
import org.apache.maven.realm.DefaultMavenRealmManager;
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.logging.console.ConsoleLogger;
public class LifecycleExecutorTest
extends PlexusTestCase
{
private MavenProjectBuilder projectBuilder;
private RepositorySystem repositorySystem;
private PluginManager pluginManager;
private DefaultLifecycleExecutor lifecycleExecutor;
protected void setUp()
throws Exception
{
projectBuilder = lookup( MavenProjectBuilder.class );
repositorySystem = lookup( RepositorySystem.class );
pluginManager = lookup( PluginManager.class );
lifecycleExecutor = (DefaultLifecycleExecutor) lookup( LifecycleExecutor.class );
}
public void testMojoExecution()
throws Exception
{
// - find the plugin [extension point: any client may wish to do whatever they choose]
// - load the plugin into a classloader [extension point: we want to take them from a repository, some may take from disk or whatever]
// - configure the plugin [extension point]
// - execute the plugin
File pom = new File( getBasedir(), "src/test/pom.xml" );
// For testing I want to use my standard local repository and settings.
ArtifactRepository localRepository = repositorySystem.createLocalRepository( new File( "/Users/jvanzyl/.m2/repository" ) );
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
assertEquals( "maven", project.getArtifactId() );
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 );
PluginDescriptor pd = pluginManager.loadPlugin( plugin, project, session );
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() );
}
protected void customizeContainerConfiguration( ContainerConfiguration containerConfiguration )
{
containerConfiguration.addComponentDiscoverer( new MavenPluginDiscoverer() );
containerConfiguration.addComponentDiscoveryListener( new MavenPluginCollector() );
}
}

View File

@ -1,28 +0,0 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
-->
<component-set>
<components>
<component>
<role>org.apache.maven.plugin.PluginLoader</role>
<role-hint>default</role-hint>
<implementation>org.apache.maven.lifecycle.plan.testutils.TestPluginLoader</implementation>
</component>
</components>
</component-set>

View File

@ -482,7 +482,8 @@ private void start( Configuration configuration )
ContainerConfiguration cc = new DefaultContainerConfiguration()
.addComponentDiscoverer( new MavenPluginDiscoverer() )
.addComponentDiscoveryListener( new MavenPluginCollector() )
.setClassWorld( classWorld ).setName( "embedder" );
.setClassWorld( classWorld )
.setName( "embedder" );
container = new DefaultPlexusContainer( cc );
}

View File

@ -17,6 +17,7 @@
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
@ -96,7 +97,7 @@ public class LegacyRepositorySystem
private Map<String, AuthenticationInfo> authenticationInfoMap = new HashMap<String, AuthenticationInfo>();
private Map<String, RepositoryPermissions> serverPermissionsMap = new HashMap<String, RepositoryPermissions>();
// Artifact Creation
public Artifact createArtifact( String groupId, String artifactId, String version, String scope, String type )
@ -171,17 +172,17 @@ public Artifact createPluginArtifact( Plugin plugin )
/**
* @return {@link Set} &lt; {@link Artifact} >
* @todo desperately needs refactoring. It's just here because it's implementation is maven-project specific
* @todo desperately needs refactoring. It's just here because it's implementation is
* maven-project specific
*/
public Set<Artifact> createArtifacts( List<Dependency> dependencies, String inheritedScope,
ArtifactFilter dependencyFilter, MavenRepositoryWrapper reactor )
public Set<Artifact> createArtifacts( List<Dependency> dependencies, String inheritedScope, ArtifactFilter dependencyFilter, MavenRepositoryWrapper reactor )
throws VersionNotFoundException
{
return createArtifacts( artifactFactory, dependencies, inheritedScope, dependencyFilter, reactor );
}
@Deprecated
public static Set<Artifact> createArtifacts( ArtifactFactory artifactFactory,List<Dependency> dependencies, String inheritedScope, ArtifactFilter dependencyFilter, MavenRepositoryWrapper reactor )
public static Set<Artifact> createArtifacts( ArtifactFactory artifactFactory, List<Dependency> dependencies, String inheritedScope, ArtifactFilter dependencyFilter, MavenRepositoryWrapper reactor )
throws VersionNotFoundException
{
Set<Artifact> projectArtifacts = new LinkedHashSet<Artifact>( dependencies.size() );
@ -208,9 +209,7 @@ public static Set<Artifact> createArtifacts( ArtifactFactory artifactFactory,Lis
{
throw new VersionNotFoundException( reactor.getId(), d, reactor.getFile(), e );
}
Artifact artifact = artifactFactory.createDependencyArtifact( d.getGroupId(), d.getArtifactId(),
versionRange, d.getType(), d.getClassifier(),
scope, inheritedScope, d.isOptional() );
Artifact artifact = artifactFactory.createDependencyArtifact( d.getGroupId(), d.getArtifactId(), versionRange, d.getType(), d.getClassifier(), scope, inheritedScope, d.isOptional() );
if ( Artifact.SCOPE_SYSTEM.equals( scope ) )
{
@ -257,8 +256,8 @@ public static Set<Artifact> createArtifacts( ArtifactFactory artifactFactory,Lis
}
return projectArtifacts;
}
}
public ArtifactRepository buildArtifactRepository( Repository repo )
throws InvalidRepositoryException
{
@ -319,6 +318,19 @@ public ArtifactRepositoryPolicy buildArtifactRepositoryPolicy( RepositoryPolicy
// From MavenExecutionRequestPopulator
public ArtifactRepository createLocalRepository( File localRepository )
throws InvalidRepositoryException
{
try
{
return createRepository( localRepository.toURI().toURL().toString(), "maven.repo.local" );
}
catch ( MalformedURLException e )
{
throw new InvalidRepositoryException( "Error creating local repository.", "maven.repo.local", e );
}
}
public ArtifactRepository createLocalRepository( String url, String repositoryId )
throws IOException
{
@ -373,7 +385,7 @@ private ArtifactRepository createRepository( String url, String repositoryId )
}
public ArtifactResolutionResult resolve( ArtifactResolutionRequest request )
{
{
return artifactResolver.resolve( request );
}
@ -400,8 +412,7 @@ public void addProxy( String protocol, String host, int port, String username, S
proxies.put( protocol, proxyInfo );
}
public void addAuthenticationInfo( String repositoryId, String username, String password, String privateKey,
String passphrase )
public void addAuthenticationInfo( String repositoryId, String username, String password, String privateKey, String passphrase )
{
AuthenticationInfo authInfo = new AuthenticationInfo();
authInfo.setUserName( username );
@ -437,14 +448,14 @@ public void addPermissionInfo( String repositoryId, String filePermissions, Stri
serverPermissionsMap.put( repositoryId, permissions );
}
}
// Mirror
public void addMirror( String id, String mirrorOf, String url )
{
mirrorBuilder.addMirror( id, mirrorOf, url );
}
public List<ArtifactRepository> getMirrors( List<ArtifactRepository> repositories )
{
return mirrorBuilder.getMirrors( repositories );

View File

@ -15,6 +15,7 @@
* the License.
*/
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;
@ -61,9 +62,13 @@ Set<Artifact> createArtifacts( List<Dependency> dependencies, String inheritedSc
ArtifactRepository buildArtifactRepository( Repository repository )
throws InvalidRepositoryException;
//!!jvz Change this to use a file
ArtifactRepository createLocalRepository( String url, String repositoryId )
throws IOException;
ArtifactRepository createLocalRepository( File localRepository )
throws InvalidRepositoryException;
ArtifactResolutionResult resolve( ArtifactResolutionRequest request );
//MetadataResolutionResult resolveMetadata( MetadataResolutionRequest request );