[MRESOLVER-8] ScopeDependencySelector incorrectly de-selects direct dependencies
diff --git a/maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/selector/ScopeDependencySelector.java b/maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/selector/ScopeDependencySelector.java
index 75a8fd6..d94057a 100644
--- a/maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/selector/ScopeDependencySelector.java
+++ b/maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/selector/ScopeDependencySelector.java
@@ -8,9 +8,9 @@
* to you 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
@@ -34,14 +34,14 @@
* A dependency selector that filters transitive dependencies based on their scope. Direct dependencies are always
* included regardless of their scope. <em>Note:</em> This filter does not assume any relationships between the scopes.
* In particular, the filter is not aware of scopes that logically include other scopes.
- *
+ *
* @see Dependency#getScope()
*/
public final class ScopeDependencySelector
implements DependencySelector
{
- private final boolean transitive;
+ private final int depth;
private final Collection<String> included;
@@ -49,13 +49,13 @@
/**
* Creates a new selector using the specified includes and excludes.
- *
+ *
* @param included The set of scopes to include, may be {@code null} or empty to include any scope.
* @param excluded The set of scopes to exclude, may be {@code null} or empty to exclude no scope.
*/
public ScopeDependencySelector( Collection<String> included, Collection<String> excluded )
{
- transitive = false;
+ this.depth = 0;
this.included = clone( included );
this.excluded = clone( excluded );
}
@@ -82,7 +82,7 @@
/**
* Creates a new selector using the specified excludes.
- *
+ *
* @param excluded The set of scopes to exclude, may be {@code null} or empty to exclude no scope.
*/
public ScopeDependencySelector( String... excluded )
@@ -90,33 +90,23 @@
this( null, ( excluded != null ) ? Arrays.asList( excluded ) : null );
}
- private ScopeDependencySelector( boolean transitive, Collection<String> included, Collection<String> excluded )
+ private ScopeDependencySelector( int depth, Collection<String> included, Collection<String> excluded )
{
- this.transitive = transitive;
+ this.depth = depth;
this.included = included;
this.excluded = excluded;
}
public boolean selectDependency( Dependency dependency )
{
- if ( !transitive )
- {
- return true;
- }
-
- String scope = dependency.getScope();
- return ( included == null || included.contains( scope ) )
- && ( excluded == null || !excluded.contains( scope ) );
+ return depth < 2
+ || ( ( included == null || included.contains( dependency.getScope() ) )
+ && ( excluded == null || !excluded.contains( dependency.getScope() ) ) );
}
public DependencySelector deriveChildSelector( DependencyCollectionContext context )
{
- if ( this.transitive || context.getDependency() == null )
- {
- return this;
- }
-
- return new ScopeDependencySelector( true, included, excluded );
+ return depth >= 2 ? this : new ScopeDependencySelector( depth + 1, included, excluded );
}
@Override
@@ -132,7 +122,7 @@
}
ScopeDependencySelector that = (ScopeDependencySelector) obj;
- return transitive == that.transitive && Objects.equals( included, that.included )
+ return depth == that.depth && Objects.equals( included, that.included )
&& Objects.equals( excluded, that.excluded );
}
@@ -140,7 +130,7 @@
public int hashCode()
{
int hash = 17;
- hash = hash * 31 + ( transitive ? 1 : 0 );
+ hash = hash * 31 + depth;
hash = hash * 31 + ( included != null ? included.hashCode() : 0 );
hash = hash * 31 + ( excluded != null ? excluded.hashCode() : 0 );
return hash;
@@ -150,7 +140,7 @@
public String toString()
{
return String.format(
- "%s(included: %s, excluded: %s, transitive: %s)", getClass().getSimpleName(), included, excluded, transitive
+ "%s(included: %s, excluded: %s, depth: %d)", getClass().getSimpleName(), included, excluded, depth
);
}
diff --git a/maven-resolver-util/src/test/java/org/eclipse/aether/util/graph/selector/ScopeDependencySelectorTest.java b/maven-resolver-util/src/test/java/org/eclipse/aether/util/graph/selector/ScopeDependencySelectorTest.java
index 7d84258..f8324b5 100644
--- a/maven-resolver-util/src/test/java/org/eclipse/aether/util/graph/selector/ScopeDependencySelectorTest.java
+++ b/maven-resolver-util/src/test/java/org/eclipse/aether/util/graph/selector/ScopeDependencySelectorTest.java
@@ -31,8 +31,8 @@
public void testToString()
{
assertEquals(
- "ScopeDependencySelector(included: [foo], excluded: [bar], transitive: false)",
+ "ScopeDependencySelector(included: [foo], excluded: [bar], depth: 0)",
new ScopeDependencySelector(Collections.singleton( "foo" ), Collections.singleton( "bar" ) ).toString()
);
}
-}
\ No newline at end of file
+}