diff --git a/maven-artifact/src/main/java/org/apache/maven/artifact/AbstractArtifactComponent.java b/maven-artifact/src/main/java/org/apache/maven/artifact/AbstractArtifactComponent.java index 3aeb47ad80..6621426a52 100644 --- a/maven-artifact/src/main/java/org/apache/maven/artifact/AbstractArtifactComponent.java +++ b/maven-artifact/src/main/java/org/apache/maven/artifact/AbstractArtifactComponent.java @@ -23,6 +23,7 @@ import org.apache.maven.artifact.repository.ArtifactRepository; import org.codehaus.plexus.logging.AbstractLogEnabled; /** + * @todo refactor away * @author Jason van Zyl * @version $Id$ */ diff --git a/maven-artifact/src/main/java/org/apache/maven/artifact/DefaultArtifact.java b/maven-artifact/src/main/java/org/apache/maven/artifact/DefaultArtifact.java index 2bd37e2a2d..60b87a19b4 100644 --- a/maven-artifact/src/main/java/org/apache/maven/artifact/DefaultArtifact.java +++ b/maven-artifact/src/main/java/org/apache/maven/artifact/DefaultArtifact.java @@ -57,7 +57,6 @@ public class DefaultArtifact /** @todo this should be replaced by type handler */ public DefaultArtifact( String groupId, String artifactId, String version, String type, String extension ) { - // TODO: default should be runtime, except in currently building POM where it is compile. this( groupId, artifactId, version, SCOPE_COMPILE, type, extension ); } diff --git a/maven-artifact/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java b/maven-artifact/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java index 8704a274c4..0d251688a5 100644 --- a/maven-artifact/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java +++ b/maven-artifact/src/main/java/org/apache/maven/artifact/resolver/DefaultArtifactResolver.java @@ -11,6 +11,7 @@ import org.apache.maven.artifact.resolver.filter.ArtifactFilter; import org.apache.maven.artifact.resolver.transform.ArtifactRequestTransformation; import org.apache.maven.wagon.TransferFailedException; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -19,6 +20,9 @@ import java.util.List; import java.util.Map; import java.util.Set; +/** + * @todo get rid of {@link AbstractArtifactComponent} and then create an AbstractArtifactResolver that does the transitive boilerplate + */ public class DefaultArtifactResolver extends AbstractArtifactComponent implements ArtifactResolver @@ -175,11 +179,7 @@ public class DefaultArtifactResolver ArtifactMetadataSource source ) throws ArtifactResolutionException { - Set s = new HashSet(); - - s.add( artifact ); - - return resolveTransitively( s, remoteRepositories, localRepository, source ); + return resolveTransitively( Collections.singleton( artifact ), remoteRepositories, localRepository, source ); } @@ -242,7 +242,6 @@ public class DefaultArtifactResolver try { - // TODO: need to convert scope compile -> runtime using scope handler referencedDependencies = source.retrieve( newArtifact, localRepository, remoteRepositories ); } catch ( ArtifactMetadataRetrievalException e ) diff --git a/maven-core/src/main/java/org/apache/maven/artifact/MavenMetadataSource.java b/maven-core/src/main/java/org/apache/maven/artifact/MavenMetadataSource.java index f9e809d159..80cd0947e4 100644 --- a/maven-core/src/main/java/org/apache/maven/artifact/MavenMetadataSource.java +++ b/maven-core/src/main/java/org/apache/maven/artifact/MavenMetadataSource.java @@ -79,12 +79,12 @@ public class MavenMetadataSource if ( mavenProjectBuilder != null ) { MavenProject project = mavenProjectBuilder.build( metadataArtifact.getFile(), localRepository ); - artifacts = createArtifacts( project.getDependencies(), localRepository ); + artifacts = createArtifacts( project.getDependencies(), artifact.getScope(), localRepository ); } else { Model model = reader.read( new FileReader( metadataArtifact.getFile() ) ); - artifacts = createArtifacts( model.getDependencies(), localRepository ); + artifacts = createArtifacts( model.getDependencies(), artifact.getScope(), localRepository ); } } catch ( ArtifactResolutionException e ) @@ -99,27 +99,40 @@ public class MavenMetadataSource return artifacts; } - public Set createArtifacts( List dependencies, ArtifactRepository localRepository ) + protected Set createArtifacts( List dependencies, String scope, ArtifactRepository localRepository ) { Set projectArtifacts = new HashSet(); for ( Iterator i = dependencies.iterator(); i.hasNext(); ) { Dependency d = (Dependency) i.next(); - Artifact artifact = createArtifact( d, localRepository ); + Artifact artifact = createArtifact( d, scope, localRepository ); projectArtifacts.add( artifact ); } return projectArtifacts; } - public Artifact createArtifact( Dependency dependency, ArtifactRepository localRepository ) + protected Artifact createArtifact( Dependency dependency, String scope, ArtifactRepository localRepository ) { // TODO: duplicated with the ArtifactFactory, localRepository not used (should be used here to resolve path? Artifact artifact = new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), - dependency.getScope(), + transitiveScope( dependency.getScope(), scope ), dependency.getType(), dependency.getType() ); return artifact; } + + private String transitiveScope( String desiredScope, String artifactScope ) + { + // TODO: scope handler + if ( Artifact.SCOPE_TEST.equals( artifactScope ) || Artifact.SCOPE_TEST.equals( desiredScope ) ) + { + return Artifact.SCOPE_TEST; + } + else + { + return Artifact.SCOPE_RUNTIME; + } + } } diff --git a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java index 9afc74845b..3aedb6f434 100644 --- a/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java +++ b/maven-core/src/main/java/org/apache/maven/plugin/DefaultPluginManager.java @@ -209,7 +209,8 @@ public class DefaultPluginManager String version = "1.0-SNAPSHOT"; - Artifact pluginArtifact = new DefaultArtifact( "maven", artifactId, version, "plugin", "jar" ); + Artifact pluginArtifact = new DefaultArtifact( "maven", artifactId, version, DefaultArtifact.SCOPE_RUNTIME, + "plugin", "jar" ); addPlugin( pluginArtifact, session ); } diff --git a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java index 3f139cf8e8..8d29e6aa6e 100644 --- a/maven-core/src/main/java/org/apache/maven/project/MavenProject.java +++ b/maven-core/src/main/java/org/apache/maven/project/MavenProject.java @@ -177,7 +177,7 @@ public class MavenProject Artifact a = (Artifact) i.next(); // TODO: let the scope handler deal with this - if ( a.getScope() == null || Artifact.SCOPE_COMPILE.equals( a.getScope() ) ) + if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) ) { list.add( a.getPath() ); } @@ -198,9 +198,8 @@ public class MavenProject if ( isAddedToClasspath( a ) ) { // TODO: let the scope handler deal with this - if ( a.getScope() == null || Artifact.SCOPE_TEST.equals( a.getScope() ) || - Artifact.SCOPE_COMPILE.equals( a.getScope() ) || - Artifact.SCOPE_RUNTIME.equals( a.getScope() ) ) + if ( Artifact.SCOPE_TEST.equals( a.getScope() ) || Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_RUNTIME.equals( + a.getScope() ) ) { list.add( a.getPath() ); } @@ -222,8 +221,7 @@ public class MavenProject if ( isAddedToClasspath( a ) ) { // TODO: let the scope handler deal with this - if ( a.getScope() == null || Artifact.SCOPE_COMPILE.equals( a.getScope() ) || - Artifact.SCOPE_RUNTIME.equals( a.getScope() ) ) + if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_RUNTIME.equals( a.getScope() ) ) { list.add( a.getPath() ); } diff --git a/maven-core/src/test/java/org/apache/maven/project/ProjectClasspathArtifactResolver.java b/maven-core/src/test/java/org/apache/maven/project/ProjectClasspathArtifactResolver.java new file mode 100644 index 0000000000..ef2911e470 --- /dev/null +++ b/maven-core/src/test/java/org/apache/maven/project/ProjectClasspathArtifactResolver.java @@ -0,0 +1,98 @@ +package org.apache.maven.project; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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. + */ + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.MavenMetadataSource; +import org.apache.maven.artifact.handler.manager.ArtifactHandlerNotFoundException; +import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; +import org.apache.maven.artifact.metadata.ArtifactMetadataSource; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.resolver.ArtifactResolutionException; +import org.apache.maven.artifact.resolver.ArtifactResolutionResult; +import org.apache.maven.artifact.resolver.ArtifactResolver; +import org.apache.maven.artifact.resolver.DefaultArtifactResolver; +import org.apache.maven.artifact.resolver.filter.ArtifactFilter; +import org.apache.maven.model.Model; +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; + +import java.io.InputStreamReader; +import java.util.Set; + +public class ProjectClasspathArtifactResolver extends DefaultArtifactResolver +{ + private static class Source extends MavenMetadataSource + { + public Source( ArtifactResolver artifactResolver ) + { + super( artifactResolver ); + } + + public Set retrieve( Artifact artifact, ArtifactRepository localRepository, Set remoteRepositories ) + throws ArtifactMetadataRetrievalException + { + MavenXpp3Reader reader = new MavenXpp3Reader(); + Model model = null; + try + { + String scope = artifact.getArtifactId().substring( "scope-".length() ); + String name = "/projects/scope/transitive-" + scope + "-dep.xml"; + model = reader.read( new InputStreamReader( getClass().getResourceAsStream( name ) ) ); + } + catch ( Exception e ) + { + throw new ArtifactMetadataRetrievalException( e ); + } + return createArtifacts( model.getDependencies(), artifact.getScope(), localRepository ); + } + } + + public Artifact resolve( Artifact artifact, Set remoteRepositories, ArtifactRepository localRepository ) + throws ArtifactResolutionException + { + return artifact; + } + + protected void setLocalRepositoryPath( Artifact artifact, ArtifactRepository localRepository ) + throws ArtifactHandlerNotFoundException + { + } + + public ArtifactResolutionResult resolveTransitively( Set artifacts, Set remoteRepositories, + ArtifactRepository localRepository, + ArtifactMetadataSource source, ArtifactFilter filter ) + throws ArtifactResolutionException + { + return super.resolveTransitively( artifacts, remoteRepositories, localRepository, new Source( this ), filter ); + } + + public ArtifactResolutionResult resolveTransitively( Set artifacts, Set remoteRepositories, + ArtifactRepository localRepository, + ArtifactMetadataSource source ) + throws ArtifactResolutionException + { + return super.resolveTransitively( artifacts, remoteRepositories, localRepository, new Source( this ) ); + } + + public ArtifactResolutionResult resolveTransitively( Artifact artifact, Set remoteRepositories, + ArtifactRepository localRepository, + ArtifactMetadataSource source ) + throws ArtifactResolutionException + { + return super.resolveTransitively( artifact, remoteRepositories, localRepository, new Source( this ) ); + } +} 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 new file mode 100644 index 0000000000..c0b05eb162 --- /dev/null +++ b/maven-core/src/test/java/org/apache/maven/project/ProjectClasspathTest.java @@ -0,0 +1,107 @@ +package org.apache.maven.project; + +/* + * Copyright 2001-2005 The Apache Software Foundation. + * + * Licensed 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. + */ + +import org.apache.maven.MavenTestCase; +import org.apache.maven.artifact.Artifact; + +import java.io.File; +import java.lang.reflect.Field; +import java.util.Iterator; + +/** + * @todo relocate to maven-artifact in entirety + */ +public class ProjectClasspathTest extends MavenTestCase +{ + + private String dir = "src/test/resources/projects/scope/"; + + public void testProjectClasspath() + throws Exception + { + File f = getTestFile( dir + "project-with-scoped-dependencies.xml" ); + + // XXX: Because this test fails, we resort to crude reflection hacks, see PLX-108 for the solution +// assertEquals( ProjectClasspathArtifactResolver.class.getName(), getContainer().lookup( ArtifactResolver.ROLE ) ); + MavenProjectBuilder builder = (MavenProjectBuilder) getContainer().lookup( MavenProjectBuilder.ROLE ); + Field declaredField = builder.getClass().getDeclaredField( "artifactResolver" ); + boolean acc = declaredField.isAccessible(); + declaredField.setAccessible( true ); + declaredField.set( builder, new ProjectClasspathArtifactResolver() ); + declaredField.setAccessible( acc ); + // XXX: end hack + + MavenProject project = getProject( f, true ); + + Artifact artifact; + + assertNotNull( "Test project can't be null!", project ); + + checkArtifactIdScope( project, "test", "test" ); + checkArtifactIdScope( project, "compile", "compile" ); + checkArtifactIdScope( project, "runtime", "runtime" ); + checkArtifactIdScope( project, "default", "compile" ); + + // check all transitive deps of a test dependency are test scope + checkGroupIdScope( project, "test", "test" ); + + // check all transitive deps of a runtime dependency are runtime scope, except for test + checkGroupIdScope( project, "runtime", "runtime" ); + + // check all transitive deps of a compile dependency are runtime scope, except for test + checkGroupIdScope( project, "compile", "runtime" ); + + // check all transitive deps of a default dependency are runtime scope, except for test + checkGroupIdScope( project, "default", "runtime" ); + } + + private void checkGroupIdScope( MavenProject project, String scope, String scopeValue ) + { + Artifact artifact; + String groupId = "maven-test-" + scope; + artifact = getArtifact( project, groupId, "scope-compile" ); + assertEquals( "Check scope", scopeValue, artifact.getScope() ); + artifact = getArtifact( project, groupId, "scope-test" ); + assertEquals( "Check scope", "test", artifact.getScope() ); + artifact = getArtifact( project, groupId, "scope-default" ); + assertEquals( "Check scope", scopeValue, artifact.getScope() ); + artifact = getArtifact( project, groupId, "scope-runtime" ); + assertEquals( "Check scope", scopeValue, artifact.getScope() ); + } + + private void checkArtifactIdScope( MavenProject project, String scope, String scopeValue ) + { + String artifactId = "scope-" + scope; + Artifact artifact = getArtifact( project, "maven-test", artifactId ); + assertEquals( "Check scope", scopeValue, artifact.getScope() ); + } + + private Artifact getArtifact( MavenProject project, String groupId, String artifactId ) + { + for ( Iterator i = project.getArtifacts().iterator(); i.hasNext(); ) + { + Artifact a = (Artifact) i.next(); + if ( artifactId.equals( a.getArtifactId() ) && a.getGroupId().equals( groupId ) ) + { + return a; + } + } + fail( "Dependency " + artifactId + " not found" ); + return null; + } +} 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..0e09686de0 --- /dev/null +++ b/maven-core/src/test/resources/org/apache/maven/project/ProjectClasspathTest.xml @@ -0,0 +1,8 @@ + + + + org.apache.maven.artifact.ArtifactResolver + org.apache.maven.project.ProjectClasspathArtifactResolver + + + \ No newline at end of file diff --git a/maven-core/src/test/resources/projects/scope/project-with-scoped-dependencies.xml b/maven-core/src/test/resources/projects/scope/project-with-scoped-dependencies.xml new file mode 100644 index 0000000000..56366b5279 --- /dev/null +++ b/maven-core/src/test/resources/projects/scope/project-with-scoped-dependencies.xml @@ -0,0 +1,39 @@ + + + + maven + maven-project-test + Maven + 1.0-beta-9 + + + + maven-test + scope-default + 1.0 + + + + maven-test + scope-test + 1.0 + test + + + + maven-test + scope-runtime + 1.0 + runtime + + + + maven-test + scope-compile + 1.0 + compile + + + + + diff --git a/maven-core/src/test/resources/projects/scope/transitive-compile-dep.xml b/maven-core/src/test/resources/projects/scope/transitive-compile-dep.xml new file mode 100644 index 0000000000..7c387743ff --- /dev/null +++ b/maven-core/src/test/resources/projects/scope/transitive-compile-dep.xml @@ -0,0 +1,38 @@ + + + + maven-test + scope-compile + 1.0 + + + + maven-test-compile + scope-default + 1.0 + + + + maven-test-compile + scope-test + 1.0 + test + + + + maven-test-compile + scope-runtime + 1.0 + runtime + + + + maven-test-compile + scope-compile + 1.0 + compile + + + + + diff --git a/maven-core/src/test/resources/projects/scope/transitive-default-dep.xml b/maven-core/src/test/resources/projects/scope/transitive-default-dep.xml new file mode 100644 index 0000000000..cc4caa068b --- /dev/null +++ b/maven-core/src/test/resources/projects/scope/transitive-default-dep.xml @@ -0,0 +1,38 @@ + + + + maven-test + scope-default + 1.0 + + + + maven-test-default + scope-default + 1.0 + + + + maven-test-default + scope-test + 1.0 + test + + + + maven-test-default + scope-runtime + 1.0 + runtime + + + + maven-test-default + scope-compile + 1.0 + compile + + + + + diff --git a/maven-core/src/test/resources/projects/scope/transitive-runtime-dep.xml b/maven-core/src/test/resources/projects/scope/transitive-runtime-dep.xml new file mode 100644 index 0000000000..0b4bfff68b --- /dev/null +++ b/maven-core/src/test/resources/projects/scope/transitive-runtime-dep.xml @@ -0,0 +1,38 @@ + + + + maven-test + scope-runtime + 1.0 + + + + maven-test-runtime + scope-default + 1.0 + + + + maven-test-runtime + scope-test + 1.0 + test + + + + maven-test-runtime + scope-runtime + 1.0 + runtime + + + + maven-test-runtime + scope-compile + 1.0 + compile + + + + + diff --git a/maven-core/src/test/resources/projects/scope/transitive-test-dep.xml b/maven-core/src/test/resources/projects/scope/transitive-test-dep.xml new file mode 100644 index 0000000000..9a5f52f2f7 --- /dev/null +++ b/maven-core/src/test/resources/projects/scope/transitive-test-dep.xml @@ -0,0 +1,38 @@ + + + + maven-test + scope-test + 1.0 + + + + maven-test-test + scope-default + 1.0 + + + + maven-test-test + scope-test + 1.0 + test + + + + maven-test-test + scope-runtime + 1.0 + runtime + + + + maven-test-test + scope-compile + 1.0 + compile + + + + + diff --git a/maven-model/maven.mdo b/maven-model/maven.mdo index c8628f9d5f..d985ab4b8b 100644 --- a/maven-model/maven.mdo +++ b/maven-model/maven.mdo @@ -807,9 +807,7 @@ 4.0.0 The scope of the dependency - build, compile, test, runtime String - + compile