o Fixed scope handling during transitive dependency resolution

git-svn-id: https://svn.apache.org/repos/asf/maven/components/branches/MNG-2766@773258 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-05-09 18:33:08 +00:00
parent 7000c68391
commit e8a45c2ce3
5 changed files with 219 additions and 35 deletions

View File

@ -382,7 +382,7 @@ public class DefaultArtifactResolver
}
catch ( ComponentLookupException e )
{
// Won't happen
throw new IllegalStateException( "Failed to lookup metadata source implementation", e );
}
}

View File

@ -17,6 +17,7 @@ package org.apache.maven.project.artifact;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@ -30,10 +31,9 @@ import org.apache.maven.artifact.repository.metadata.Metadata;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException;
import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.model.Dependency;
import org.apache.maven.project.DefaultProjectBuilderConfiguration;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
@ -87,20 +87,22 @@ public class MavenMetadataSource
project = projectBuilder.buildFromRepository( pomArtifact, configuration );
if ( !artifact.getArtifactHandler().isIncludesDependencies() )
{
ArtifactFilter filter;
if ( artifact.getScope() == null )
{
artifacts = new LinkedHashSet<Artifact>();
for ( Dependency d : project.getDependencies() )
{
filter = null;
String effectiveScope = getEffectiveScope( d.getScope(), artifact.getScope() );
if ( effectiveScope != null )
{
Artifact dependencyArtifact =
repositorySystem.createArtifact( d.getGroupId(), d.getArtifactId(), d.getVersion(),
effectiveScope, d.getType() );
artifacts.add( dependencyArtifact );
}
}
else
{
filter = new ScopeArtifactFilter( artifact.getScope() );
}
artifacts = project.createArtifacts( filter );
project.setArtifacts( artifacts );
}
}
catch ( ProjectBuildingException e )
@ -114,6 +116,47 @@ public class MavenMetadataSource
return new ResolutionGroup( pomArtifact, artifacts, remoteRepositories );
}
private String getEffectiveScope( String originalScope, String inheritedScope )
{
String effectiveScope = Artifact.SCOPE_RUNTIME;
if ( originalScope == null )
{
originalScope = Artifact.SCOPE_COMPILE;
}
if ( inheritedScope == null )
{
// direct dependency retains its scope
effectiveScope = originalScope;
}
else if ( Artifact.SCOPE_TEST.equals( originalScope ) || Artifact.SCOPE_PROVIDED.equals( originalScope ) )
{
// test and provided are not transitive, so exclude them
effectiveScope = null;
}
else if ( Artifact.SCOPE_SYSTEM.equals( originalScope ) )
{
// system scope come through unchanged...
effectiveScope = Artifact.SCOPE_SYSTEM;
}
else if ( Artifact.SCOPE_COMPILE.equals( originalScope ) && Artifact.SCOPE_COMPILE.equals( inheritedScope ) )
{
// added to retain compile scope. Remove if you want compile inherited as runtime
effectiveScope = Artifact.SCOPE_COMPILE;
}
else if ( Artifact.SCOPE_TEST.equals( inheritedScope ) )
{
effectiveScope = Artifact.SCOPE_TEST;
}
else if ( Artifact.SCOPE_PROVIDED.equals( inheritedScope ) )
{
effectiveScope = Artifact.SCOPE_PROVIDED;
}
return effectiveScope;
}
public List<ArtifactVersion> retrieveAvailableVersions( Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories )
throws ArtifactMetadataRetrievalException
{
@ -166,4 +209,5 @@ public class MavenMetadataSource
return versions;
}
}

View File

@ -79,8 +79,8 @@ public abstract class AbstractMavenProjectTestCase
return markerFile.getAbsoluteFile().getParentFile();
}
protected File getFileForClasspathResource( String resource )
throws FileNotFoundException, URISyntaxException
protected static File getFileForClasspathResource( String resource )
throws FileNotFoundException
{
ClassLoader cloader = Thread.currentThread().getContextClassLoader();
@ -91,7 +91,7 @@ public abstract class AbstractMavenProjectTestCase
throw new FileNotFoundException( "Unable to find: " + resource );
}
return new File( new URI( resourceUrl.toString().replaceAll( " ", "%20" ) ) );
return new File( URI.create( resourceUrl.toString().replaceAll( " ", "%20" ) ) );
}
protected ArtifactRepository getLocalRepository()

View File

@ -20,26 +20,39 @@ package org.apache.maven.project;
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.metadata.ResolutionGroup;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.project.artifact.MavenMetadataSource;
public class ProjectClasspathTest
extends AbstractMavenProjectTestCase
{
private String dir = "projects/scope/";
public void setUp() throws Exception
private static final String dir = "projects/scope/";
public void setUp()
throws Exception
{
super.setUp();
projectBuilder = lookup(MavenProjectBuilder.class, "test");
}
super.setUp();
projectBuilder = lookup( MavenProjectBuilder.class, "default" );
}
@Override
protected String getCustomConfigurationName()
{
return null;
}
public void testProjectClasspath()
throws Exception
{
File f = getFileForClasspathResource( dir + "project-with-scoped-dependencies.xml" );
MavenProject project = getProjectWithDependencies( f );
Artifact artifact;
@ -59,8 +72,8 @@ public class ProjectClasspathTest
assertNull( "Check no test dependencies are transitive", artifact );
artifact = getArtifact( project, "maven-test-test", "scope-compile" );
assertNotNull(artifact);
assertNotNull( artifact );
System.out.println( "a = " + artifact );
System.out.println( "b = " + artifact.getScope() );
assertEquals( "Check scope", "test", artifact.getScope() );
@ -107,23 +120,72 @@ public class ProjectClasspathTest
{
String artifactId = "scope-" + scope;
Artifact artifact = getArtifact( project, "maven-test", artifactId );
assertNotNull( artifact );
assertEquals( "Check scope", scopeValue, artifact.getScope() );
}
private Artifact getArtifact( MavenProject project, String groupId, String artifactId )
{
System.out.println( "[ Looking for " + groupId + ":" + artifactId + " ]");
for ( Iterator i = project.getArtifacts().iterator(); i.hasNext(); )
{
System.out.println( "[ Looking for " + groupId + ":" + artifactId + " ]" );
for ( Iterator<Artifact> i = project.getArtifacts().iterator(); i.hasNext(); )
{
Artifact a = (Artifact) i.next();
System.out.println(a.toString());
Artifact a = i.next();
System.out.println( a.toString() );
if ( artifactId.equals( a.getArtifactId() ) && a.getGroupId().equals( groupId ) )
{
System.out.println("RETURN");
System.out.println( "RETURN" );
return a;
}
}
System.out.println("Return null");
System.out.println( "Return null" );
return null;
}
public static class TestMavenProjectBuilder
extends DefaultMavenProjectBuilder
{
@Override
public MavenProject buildFromRepository( Artifact artifact, ProjectBuilderConfiguration configuration )
throws ProjectBuildingException
{
if ( "maven-test".equals( artifact.getGroupId() ) )
{
String scope = artifact.getArtifactId().substring( "scope-".length() );
try
{
artifact.setFile( getFileForClasspathResource( dir + "transitive-" + scope + "-dep.xml" ) );
}
catch ( FileNotFoundException e )
{
throw new IllegalStateException( "Missing test POM for " + artifact );
}
}
if ( artifact.getFile() == null )
{
return new MavenProject();
}
return build( artifact.getFile(), configuration );
}
}
public static class MetadataSource
extends MavenMetadataSource
{
@Override
public ResolutionGroup retrieve( Artifact artifact, ArtifactRepository localRepository,
List<ArtifactRepository> remoteRepositories )
throws ArtifactMetadataRetrievalException
{
ResolutionGroup rg = super.retrieve( artifact, localRepository, remoteRepositories );
for ( Artifact a : rg.getArtifacts() )
{
a.setResolved( true );
}
return rg;
}
}
}

View File

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?>
<plexus>
<components>
<component>
<role>org.apache.maven.project.MavenProjectBuilder</role>
<role-hint>default</role-hint>
<implementation>org.apache.maven.project.ProjectClasspathTest$TestMavenProjectBuilder</implementation>
<isolated-realm>false</isolated-realm>
<requirements>
<requirement>
<role>org.codehaus.plexus.logging.Logger</role>
<role-hint>default</role-hint>
<field-name>logger</field-name>
</requirement>
<requirement>
<role>org.apache.maven.project.validation.ModelValidator</role>
<role-hint>default</role-hint>
<field-name>validator</field-name>
</requirement>
<requirement>
<role>org.apache.maven.lifecycle.LifecycleExecutor</role>
<role-hint>default</role-hint>
<field-name>lifecycle</field-name>
</requirement>
<requirement>
<role>org.apache.maven.repository.RepositorySystem</role>
<role-hint>default</role-hint>
<field-name>repositorySystem</field-name>
</requirement>
<requirement>
<role>java.util.List</role>
<field-name>listeners</field-name>
</requirement>
<requirement>
<role>org.apache.maven.model.interpolator.Interpolator</role>
<role-hint>default</role-hint>
<field-name>interpolator</field-name>
</requirement>
<requirement>
<role>org.apache.maven.artifact.resolver.ResolutionErrorHandler</role>
<role-hint>default</role-hint>
<field-name>resolutionErrorHandler</field-name>
</requirement>
</requirements>
</component>
<component>
<role>org.apache.maven.artifact.metadata.ArtifactMetadataSource</role>
<role-hint>default</role-hint>
<implementation>org.apache.maven.project.ProjectClasspathTest$MetadataSource</implementation>
<requirements>
<requirement>
<role>org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager</role>
<role-hint>default</role-hint>
<field-name>repositoryMetadataManager</field-name>
</requirement>
<requirement>
<role>org.apache.maven.repository.RepositorySystem</role>
<role-hint>default</role-hint>
<field-name>repositorySystem</field-name>
</requirement>
<requirement>
<role>org.apache.maven.project.MavenProjectBuilder</role>
<role-hint>default</role-hint>
<field-name>projectBuilder</field-name>
</requirement>
<requirement>
<role>org.codehaus.plexus.logging.Logger</role>
<role-hint>default</role-hint>
<field-name>logger</field-name>
</requirement>
</requirements>
</component>
<component>
<role>org.apache.maven.lifecycle.LifecycleExecutor</role>
<implementation>org.apache.maven.project.EmptyLifecycleExecutor</implementation>
</component>
</components>
</plexus>