diff --git a/maven-core/src/main/java/org/apache/maven/artifact/factory/DefaultArtifactFactory.java b/maven-core/src/main/java/org/apache/maven/artifact/factory/DefaultArtifactFactory.java index ab249cf08b..1908907abb 100644 --- a/maven-core/src/main/java/org/apache/maven/artifact/factory/DefaultArtifactFactory.java +++ b/maven-core/src/main/java/org/apache/maven/artifact/factory/DefaultArtifactFactory.java @@ -73,6 +73,13 @@ public class DefaultArtifactFactory desiredScope = Artifact.SCOPE_COMPILE; } + // vvv added to retain compile scope. Remove if you want compile inherited as runtime + else if ( Artifact.SCOPE_COMPILE.equals( scope ) && Artifact.SCOPE_COMPILE.equals( inheritedScope ) ) + { + desiredScope = Artifact.SCOPE_COMPILE; + } + // ^^^ added to retain compile scope. Remove if you want compile inherited as runtime + if ( Artifact.SCOPE_TEST.equals( scope ) || Artifact.SCOPE_TEST.equals( inheritedScope ) ) { desiredScope = Artifact.SCOPE_TEST; 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 738c777adb..90e13eb4e7 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 @@ -85,7 +85,7 @@ public class DefaultPluginManager protected PathTranslator pathTranslator; protected ArtifactRepositoryFactory artifactRepositoryFactory; - + protected MavenSettingsBuilder mavenSettingsBuilder; public DefaultPluginManager() @@ -605,7 +605,7 @@ public class DefaultPluginManager Repository pluginRepo = new Repository(); pluginRepo.setId( "plugin-repository" ); pluginRepo.setUrl( "http://repo1.maven.org" ); - + ArtifactRepository pluginRepository = artifactRepositoryFactory.createArtifactRepository( pluginRepo, settings ); remotePluginRepositories.add( pluginRepository ); @@ -628,7 +628,7 @@ public class DefaultPluginManager context.getLocalRepository(), sourceReader ); - project.getArtifacts().addAll( result.getArtifacts().values() ); + project.addArtifacts( result.getArtifacts().values() ); } // ---------------------------------------------------------------------- diff --git a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java index 25d1164f99..6e2b7d4b44 100644 --- a/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java +++ b/maven-core/src/main/java/org/apache/maven/project/DefaultMavenProjectBuilder.java @@ -78,7 +78,7 @@ public class DefaultMavenProjectBuilder private ModelDefaultsInjector modelDefaultsInjector; private ModelInterpolator modelInterpolator; - + private MavenSettingsBuilder mavenSettingsBuilder; private ArtifactRepositoryFactory artifactRepositoryFactory; @@ -198,7 +198,7 @@ public class DefaultMavenProjectBuilder ArtifactResolutionResult result = artifactResolver.resolveTransitively( project.getArtifacts(), repos, localRepository, sourceReader ); - project.getArtifacts().addAll( result.getArtifacts().values() ); + project.addArtifacts( result.getArtifacts().values() ); } ModelValidationResult validationResult = validator.validate( model ); 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 a2c3c3e35f..9fe95d513f 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 @@ -18,6 +18,7 @@ package org.apache.maven.project; */ import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DefaultArtifact; import org.apache.maven.model.Build; import org.apache.maven.model.CiManagement; import org.apache.maven.model.Contributor; @@ -40,8 +41,10 @@ import org.codehaus.plexus.util.dag.TopologicalSorter; import java.io.File; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -644,5 +647,53 @@ public class MavenProject return sortedProjects; } + + public void addArtifacts( Collection newArtifacts ) + { +// project.getArtifacts().addAll( result.getArtifacts().values() ); + // We need to override the scope if one declared it higher + // TODO: could surely be more efficient, and use the scope handler, be part of maven-artifact... + Map artifacts = new HashMap(); + for ( Iterator i = getArtifacts().iterator(); i.hasNext(); ) + { + Artifact a = (Artifact) i.next(); + artifacts.put( a.getId(), a ); + } + for ( Iterator i = newArtifacts.iterator(); i.hasNext(); ) + { + Artifact a = (Artifact) i.next(); + String id = a.getId(); + if ( artifacts.containsKey( id ) ) + { + Artifact existing = (Artifact) artifacts.get( id ); + boolean updateScope = false; + if ( Artifact.SCOPE_RUNTIME.equals( a.getScope() ) && + Artifact.SCOPE_TEST.equals( existing.getScope() ) ) + { + updateScope = true; + } + + if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) && + !Artifact.SCOPE_COMPILE.equals( existing.getScope() ) ) + { + updateScope = true; + } + + if ( updateScope ) + { + // TODO: Artifact factory? + Artifact artifact = new DefaultArtifact( existing.getGroupId(), existing.getArtifactId(), + existing.getVersion(), a.getScope(), existing.getType(), + existing.getExtension() ); + artifacts.put( id, artifact ); + } + } + else + { + artifacts.put( id, a ); + } + } + setArtifacts( new HashSet( artifacts.values() ) ); + } } 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 d41d132369..02ea9067a8 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 @@ -58,6 +58,10 @@ public class ProjectClasspathTest checkArtifactIdScope( project, "runtime", "runtime" ); checkArtifactIdScope( project, "default", "compile" ); + checkInheritedArtifactIdScope( project, "compile", "compile" ); + checkInheritedArtifactIdScope( project, "runtime", "runtime" ); + checkInheritedArtifactIdScope( project, "default", "compile" ); + // check all transitive deps of a test dependency are test, except test which is skipped artifact = getArtifact( project, "maven-test-test", "scope-test" ); assertNull( "Check no test dependencies are transitive", artifact ); @@ -71,11 +75,11 @@ public class ProjectClasspathTest // 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 compile dependency are compile scope, except for runtime and test + checkGroupIdScope( project, "compile", "compile" ); - // check all transitive deps of a default dependency are runtime scope, except for test - checkGroupIdScope( project, "default", "runtime" ); + // check all transitive deps of a default dependency are compile scope, except for runtime and test + checkGroupIdScope( project, "default", "compile" ); } private void checkGroupIdScope( MavenProject project, String scope, String scopeValue ) @@ -89,7 +93,7 @@ public class ProjectClasspathTest artifact = getArtifact( project, groupId, "scope-default" ); assertEquals( "Check scope", scopeValue, artifact.getScope() ); artifact = getArtifact( project, groupId, "scope-runtime" ); - assertEquals( "Check scope", scopeValue, artifact.getScope() ); + assertEquals( "Check scope", "runtime", artifact.getScope() ); } private void checkArtifactIdScope( MavenProject project, String scope, String scopeValue ) @@ -99,6 +103,13 @@ public class ProjectClasspathTest assertEquals( "Check scope", scopeValue, artifact.getScope() ); } + private void checkInheritedArtifactIdScope( MavenProject project, String scope, String scopeValue ) + { + String artifactId = "scope-" + scope; + Artifact artifact = getArtifact( project, "maven-inherited", artifactId ); + assertEquals( "Check scope", scopeValue, artifact.getScope() ); + } + private Artifact getArtifact( MavenProject project, String groupId, String artifactId ) { for ( Iterator i = project.getArtifacts().iterator(); i.hasNext(); ) 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 index 8ea0d8b5e2..e0022acd1d 100644 --- 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 @@ -34,6 +34,25 @@ compile + + maven-inherited + scope-default + 1.0 + + + + maven-inherited + scope-compile + 1.0 + runtime + + + + maven-inherited + scope-runtime + 1.0 + runtime + 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 index 0c168964b5..ac6beafb5d 100644 --- a/maven-core/src/test/resources/projects/scope/transitive-compile-dep.xml +++ b/maven-core/src/test/resources/projects/scope/transitive-compile-dep.xml @@ -33,6 +33,26 @@ compile + + maven-inherited + scope-compile + 1.0 + compile + + + + maven-inherited + scope-default + 1.0 + runtime + + + + maven-inherited + scope-runtime + 1.0 + test +