PR: MNG-1048

make sure the scope in the current POM wins over all

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@293514 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brett Leslie Porter 2005-10-04 04:02:46 +00:00
parent 38269cca84
commit 71625bbd82
5 changed files with 59 additions and 36 deletions

View File

@ -74,7 +74,8 @@ public class AntResolutionListener
public void updateScopeCurrentPom( Artifact artifact, String scope )
{
updateScope( artifact, scope );
project.log( indent + artifact.getId() + " (not setting scope to: " + scope + "; local scope " +
artifact.getScope() + " wins)" );
}
public void selectVersionFromRange( Artifact artifact )

View File

@ -68,7 +68,8 @@ public class DebugResolutionListener
public void updateScopeCurrentPom( Artifact artifact, String scope )
{
updateScope( artifact, scope );
logger.debug( indent + artifact.getId() + " (not setting scope to: " + scope + "; local scope " +
artifact.getScope() + " wins)" );
}
public void updateScope( Artifact artifact, String scope )

View File

@ -69,10 +69,9 @@ public class WarningResolutionListener
// TODO: better way than static? this might hide messages in a reactor
if ( !ignoredArtifacts.contains( artifact ) )
{
logger.warn( "\n\tArtifact " + artifact.getId() + " has scope '" + artifact.getScope() +
"' replaced with '" + scope + "'\n" +
"\tas a dependency has given a broader scope. If this is not intended, use -X to locate the dependency,\n" +
"\tor force the desired scope using dependencyManagement.\n" );
logger.warn( "\n\tArtifact " + artifact.getId() + " retains local scope '" + artifact.getScope() +
"' overriding broader scope '" + scope + "'\n" +
"\tgiven by a dependency. If this is not intended, modify or remove the local scope.\n" );
ignoredArtifacts.add( artifact );
}
}

View File

@ -280,43 +280,41 @@ public class DefaultArtifactCollector
}
}
private void checkScopeUpdate( ResolutionNode node, ResolutionNode previous, List listeners )
private void checkScopeUpdate( ResolutionNode farthest, ResolutionNode nearest, List listeners )
{
boolean updateScope = false;
Artifact newArtifact = node.getArtifact();
Artifact previousArtifact = previous.getArtifact();
Artifact farthestArtifact = farthest.getArtifact();
Artifact nearestArtifact = nearest.getArtifact();
if ( Artifact.SCOPE_RUNTIME.equals( newArtifact.getScope() ) && (
Artifact.SCOPE_TEST.equals( previousArtifact.getScope() ) ||
Artifact.SCOPE_PROVIDED.equals( previousArtifact.getScope() ) ) )
if ( Artifact.SCOPE_RUNTIME.equals( farthestArtifact.getScope() ) && (
Artifact.SCOPE_TEST.equals( nearestArtifact.getScope() ) ||
Artifact.SCOPE_PROVIDED.equals( nearestArtifact.getScope() ) ) )
{
updateScope = true;
}
if ( Artifact.SCOPE_COMPILE.equals( newArtifact.getScope() ) &&
!Artifact.SCOPE_COMPILE.equals( previousArtifact.getScope() ) )
if ( Artifact.SCOPE_COMPILE.equals( farthestArtifact.getScope() ) &&
!Artifact.SCOPE_COMPILE.equals( nearestArtifact.getScope() ) )
{
updateScope = true;
}
// current POM rules all
if ( nearest.getDepth() < 2 && updateScope )
{
updateScope = false;
fireEvent( ResolutionListener.UPDATE_SCOPE_CURRENT_POM, listeners, nearest, farthestArtifact );
}
if ( updateScope )
{
int event;
if ( previous.getDepth() < 2 )
{
event = ResolutionListener.UPDATE_SCOPE_CURRENT_POM;
}
else
{
event = ResolutionListener.UPDATE_SCOPE;
}
fireEvent( event, listeners, previous, newArtifact );
fireEvent( ResolutionListener.UPDATE_SCOPE, listeners, nearest, farthestArtifact );
// previously we cloned the artifact, but it is more effecient to just update the scope
// if problems are later discovered that the original object needs its original scope value, cloning may
// again be appropriate
previousArtifact.setScope( newArtifact.getScope() );
nearestArtifact.setScope( farthestArtifact.getScope() );
}
}

View File

@ -35,6 +35,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -249,7 +250,20 @@ public class DefaultArtifactCollectorTest
ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, b.artifact} ) );
assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, b.artifact} ), res.getArtifacts() );
assertEquals( "Check version", "3.8.1", getArtifact( "junit", res.getArtifacts() ).getVersion() );
assertEquals( "Check scope", Artifact.SCOPE_COMPILE, getArtifact( "junit", res.getArtifacts() ).getScope() );
assertEquals( "Check scope", Artifact.SCOPE_TEST, getArtifact( "junit", res.getArtifacts() ).getScope() );
}
public void testResolveLocalWithNewerVersionButLesserScopeResolvedFirst()
throws ArtifactResolutionException, InvalidVersionSpecificationException
{
ArtifactSpec b = createArtifact( "junit", "3.8.1", Artifact.SCOPE_TEST );
ArtifactSpec a = createArtifact( "commons-logging", "1.0" );
a.addDependency( "junit", "3.7" );
ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, b.artifact} ) );
assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, b.artifact} ), res.getArtifacts() );
assertEquals( "Check version", "3.8.1", getArtifact( "junit", res.getArtifacts() ).getVersion() );
assertEquals( "Check scope", Artifact.SCOPE_TEST, getArtifact( "junit", res.getArtifacts() ).getScope() );
}
public void testResolveNearestWithRanges()
@ -380,7 +394,9 @@ public class DefaultArtifactCollectorTest
ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, c.artifact} ) );
assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, modifiedC} ), res.getArtifacts() );
Artifact artifact = getArtifact( "c", res.getArtifacts() );
assertEquals( "Check scope", Artifact.SCOPE_COMPILE, artifact.getScope() );
// local wins now, and irrelevant if not local as test/provided aren't transitive
// assertEquals( "Check scope", Artifact.SCOPE_COMPILE, artifact.getScope() );
assertEquals( "Check scope", Artifact.SCOPE_TEST, artifact.getScope() );
}
public void testResolveRuntimeScopeOverTestScope()
@ -396,21 +412,25 @@ public class DefaultArtifactCollectorTest
ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, c.artifact} ) );
assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, modifiedC} ), res.getArtifacts() );
Artifact artifact = getArtifact( "c", res.getArtifacts() );
assertEquals( "Check scope", Artifact.SCOPE_RUNTIME, artifact.getScope() );
// local wins now, and irrelevant if not local as test/provided aren't transitive
// assertEquals( "Check scope", Artifact.SCOPE_RUNTIME, artifact.getScope() );
assertEquals( "Check scope", Artifact.SCOPE_TEST, artifact.getScope() );
}
public void testResolveCompileScopeOverRuntimeScope()
throws ArtifactResolutionException, InvalidVersionSpecificationException
{
ArtifactSpec a = createArtifact( "a", "1.0" );
ArtifactSpec c = createArtifact( "c", "3.0", Artifact.SCOPE_RUNTIME );
ArtifactSpec root = createArtifact( "root", "1.0" );
ArtifactSpec a = root.addDependency( "a", "1.0" );
root.addDependency( "c", "3.0", Artifact.SCOPE_RUNTIME );
a.addDependency( "c", "2.0", Artifact.SCOPE_COMPILE );
Artifact modifiedC = createArtifact( "c", "3.0", Artifact.SCOPE_COMPILE ).artifact;
ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, c.artifact} ) );
assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, modifiedC} ), res.getArtifacts() );
ArtifactResolutionResult res = collect( createSet( new Object[]{root.artifact} ) );
assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, root.artifact, modifiedC} ),
res.getArtifacts() );
Artifact artifact = getArtifact( "c", res.getArtifacts() );
assertEquals( "Check scope", Artifact.SCOPE_COMPILE, artifact.getScope() );
}
@ -428,7 +448,9 @@ public class DefaultArtifactCollectorTest
ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, c.artifact} ) );
assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, modifiedC} ), res.getArtifacts() );
Artifact artifact = getArtifact( "c", res.getArtifacts() );
assertEquals( "Check scope", Artifact.SCOPE_COMPILE, artifact.getScope() );
// local wins now, and irrelevant if not local as test/provided aren't transitive
// assertEquals( "Check scope", Artifact.SCOPE_COMPILE, artifact.getScope() );
assertEquals( "Check scope", Artifact.SCOPE_PROVIDED, artifact.getScope() );
}
public void testResolveRuntimeScopeOverProvidedScope()
@ -444,7 +466,9 @@ public class DefaultArtifactCollectorTest
ArtifactResolutionResult res = collect( createSet( new Object[]{a.artifact, c.artifact} ) );
assertEquals( "Check artifact list", createSet( new Object[]{a.artifact, modifiedC} ), res.getArtifacts() );
Artifact artifact = getArtifact( "c", res.getArtifacts() );
assertEquals( "Check scope", Artifact.SCOPE_RUNTIME, artifact.getScope() );
// local wins now, and irrelevant if not local as test/provided aren't transitive
// assertEquals( "Check scope", Artifact.SCOPE_RUNTIME, artifact.getScope() );
assertEquals( "Check scope", Artifact.SCOPE_PROVIDED, artifact.getScope() );
}
public void testProvidedScopeNotTransitive()
@ -591,7 +615,7 @@ public class DefaultArtifactCollectorTest
private static Set createSet( Object[] x )
{
return new HashSet( Arrays.asList( x ) );
return new LinkedHashSet( Arrays.asList( x ) );
}
private class ArtifactSpec