diff --git a/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java b/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java index 283e5dfd7d..d5571849d3 100644 --- a/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java +++ b/maven-compat/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java @@ -382,7 +382,7 @@ public class DefaultArtifactResolver } catch ( ComponentLookupException e ) { - // Won't happen + throw new IllegalStateException( "Failed to lookup metadata source implementation", e ); } } diff --git a/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java b/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java index aadad8a8c4..a8f23f1a63 100644 --- a/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java +++ b/maven-core/src/main/java/org/apache/maven/project/artifact/MavenMetadataSource.java @@ -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(); + + 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 retrieveAvailableVersions( Artifact artifact, ArtifactRepository localRepository, List remoteRepositories ) throws ArtifactMetadataRetrievalException { @@ -166,4 +209,5 @@ public class MavenMetadataSource return versions; } + } diff --git a/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java b/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java index b942622337..e2670d16c7 100644 --- a/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java +++ b/maven-core/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java @@ -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() diff --git a/maven-core/src/test/java/org/apache/maven/project/ProjectClasspathTest.java b/maven-core/src/test/java/org/apache/maven/project/ProjectClasspathTest.java index 8ea4444f41..d157ca33db 100644 --- a/maven-core/src/test/java/org/apache/maven/project/ProjectClasspathTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/ProjectClasspathTest.java @@ -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 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 remoteRepositories ) + throws ArtifactMetadataRetrievalException + { + ResolutionGroup rg = super.retrieve( artifact, localRepository, remoteRepositories ); + for ( Artifact a : rg.getArtifacts() ) + { + a.setResolved( true ); + } + return rg; + } + + } + } diff --git a/maven-core/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml b/maven-core/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml new file mode 100644 index 0000000000..b6db7183bb --- /dev/null +++ b/maven-core/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml @@ -0,0 +1,78 @@ + + + + + org.apache.maven.project.MavenProjectBuilder + default + org.apache.maven.project.ProjectClasspathTest$TestMavenProjectBuilder + false + + + org.codehaus.plexus.logging.Logger + default + logger + + + org.apache.maven.project.validation.ModelValidator + default + validator + + + org.apache.maven.lifecycle.LifecycleExecutor + default + lifecycle + + + org.apache.maven.repository.RepositorySystem + default + repositorySystem + + + java.util.List + listeners + + + org.apache.maven.model.interpolator.Interpolator + default + interpolator + + + org.apache.maven.artifact.resolver.ResolutionErrorHandler + default + resolutionErrorHandler + + + + + org.apache.maven.artifact.metadata.ArtifactMetadataSource + default + org.apache.maven.project.ProjectClasspathTest$MetadataSource + + + org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager + default + repositoryMetadataManager + + + org.apache.maven.repository.RepositorySystem + default + repositorySystem + + + org.apache.maven.project.MavenProjectBuilder + default + projectBuilder + + + org.codehaus.plexus.logging.Logger + default + logger + + + + + org.apache.maven.lifecycle.LifecycleExecutor + org.apache.maven.project.EmptyLifecycleExecutor + + +