From c813320848705d32156f08cb92d3cea52c893ab0 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Wed, 7 Feb 2024 14:37:36 +0100 Subject: [PATCH] Remove empty dependency scope from the api (#1402) --- .../java/org/apache/maven/api/DependencyScope.java | 7 +++++-- .../src/main/java/org/apache/maven/api/PathScope.java | 7 ++----- .../apache/maven/internal/impl/AbstractSession.java | 7 ++++--- .../internal/impl/DefaultDependencyCollector.java | 7 ++++--- .../apache/maven/internal/impl/DefaultSession.java | 11 ++++++++--- .../apache/maven/internal/impl/InternalSession.java | 5 +++-- 6 files changed, 26 insertions(+), 18 deletions(-) diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/DependencyScope.java b/api/maven-api-core/src/main/java/org/apache/maven/api/DependencyScope.java index 047ab1c3ab..57dfb7ebc7 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/DependencyScope.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/DependencyScope.java @@ -49,9 +49,12 @@ public enum DependencyScope { NONE("none", false), /** - * Empty scope. + * Undefined. When no scope is explicitly given, UNDEFINED will be used, but its meaning will depend on + * whether the DependencyCoordinate is used in dependency management, in which case it means the scope is not + * explicitly managed by this managed dependency, or as a real dependency, in which case, the scope + * will default to {@link #COMPILE}. */ - EMPTY("", false), + UNDEFINED("", false), /** * Compile only. diff --git a/api/maven-api-core/src/main/java/org/apache/maven/api/PathScope.java b/api/maven-api-core/src/main/java/org/apache/maven/api/PathScope.java index 35a63b0240..93b1c90545 100644 --- a/api/maven-api-core/src/main/java/org/apache/maven/api/PathScope.java +++ b/api/maven-api-core/src/main/java/org/apache/maven/api/PathScope.java @@ -55,18 +55,16 @@ public interface PathScope extends ExtensibleEnum { PathScope MAIN_COMPILE = pathScope( "main-compile", ProjectScope.MAIN, - DependencyScope.EMPTY, DependencyScope.COMPILE_ONLY, DependencyScope.COMPILE, DependencyScope.PROVIDED); - PathScope MAIN_RUNTIME = pathScope( - "main-runtime", ProjectScope.MAIN, DependencyScope.EMPTY, DependencyScope.COMPILE, DependencyScope.RUNTIME); + PathScope MAIN_RUNTIME = + pathScope("main-runtime", ProjectScope.MAIN, DependencyScope.COMPILE, DependencyScope.RUNTIME); PathScope TEST_COMPILE = pathScope( "test-compile", ProjectScope.TEST, - DependencyScope.EMPTY, DependencyScope.COMPILE, DependencyScope.PROVIDED, DependencyScope.TEST_ONLY, @@ -75,7 +73,6 @@ public interface PathScope extends ExtensibleEnum { PathScope TEST_RUNTIME = pathScope( "test-runtime", ProjectScope.TEST, - DependencyScope.EMPTY, DependencyScope.COMPILE, DependencyScope.RUNTIME, DependencyScope.PROVIDED, diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/AbstractSession.java b/maven-core/src/main/java/org/apache/maven/internal/impl/AbstractSession.java index 1b97790119..de4d19a066 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/AbstractSession.java +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/AbstractSession.java @@ -111,11 +111,12 @@ public abstract class AbstractSession implements InternalSession { public abstract ArtifactRepository toArtifactRepository(RemoteRepository repository); - public List toDependencies(Collection dependencies) { - return dependencies == null ? null : map(dependencies, this::toDependency); + public List toDependencies( + Collection dependencies, boolean managed) { + return dependencies == null ? null : map(dependencies, d -> toDependency(d, managed)); } - public abstract org.eclipse.aether.graph.Dependency toDependency(DependencyCoordinate dependency); + public abstract org.eclipse.aether.graph.Dependency toDependency(DependencyCoordinate dependency, boolean managed); public List toArtifacts(Collection artifacts) { return artifacts == null ? null : map(artifacts, this::toArtifact); diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultDependencyCollector.java b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultDependencyCollector.java index 49b5dc4bde..41adae1cd2 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultDependencyCollector.java +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultDependencyCollector.java @@ -54,12 +54,13 @@ public class DefaultDependencyCollector implements DependencyCollector { Artifact rootArtifact = request.getRootArtifact().map(session::toArtifact).orElse(null); - Dependency root = request.getRoot().map(session::toDependency).orElse(null); + Dependency root = + request.getRoot().map(d -> session.toDependency(d, false)).orElse(null); CollectRequest collectRequest = new CollectRequest() .setRootArtifact(rootArtifact) .setRoot(root) - .setDependencies(session.toDependencies(request.getDependencies())) - .setManagedDependencies(session.toDependencies(request.getManagedDependencies())) + .setDependencies(session.toDependencies(request.getDependencies(), false)) + .setManagedDependencies(session.toDependencies(request.getManagedDependencies(), true)) .setRepositories(session.toRepositories(session.getRemoteRepositories())); RepositorySystemSession systemSession = session.getSession(); diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultSession.java b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultSession.java index c686abd083..450a646283 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultSession.java +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultSession.java @@ -280,11 +280,12 @@ public class DefaultSession extends AbstractSession { } } - public org.eclipse.aether.graph.Dependency toDependency(DependencyCoordinate dependency) { + public org.eclipse.aether.graph.Dependency toDependency(DependencyCoordinate dependency, boolean managed) { + org.eclipse.aether.graph.Dependency dep; if (dependency instanceof DefaultDependencyCoordinate) { - return ((DefaultDependencyCoordinate) dependency).getDependency(); + dep = ((DefaultDependencyCoordinate) dependency).getDependency(); } else { - return new org.eclipse.aether.graph.Dependency( + dep = new org.eclipse.aether.graph.Dependency( new org.eclipse.aether.artifact.DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), @@ -294,5 +295,9 @@ public class DefaultSession extends AbstractSession { null), dependency.getScope().id()); } + if (!managed && "".equals(dep.getScope())) { + dep = dep.setScope(DependencyScope.COMPILE.id()); + } + return dep; } } diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/InternalSession.java b/maven-core/src/main/java/org/apache/maven/internal/impl/InternalSession.java index d13acfb30a..a41c087a62 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/InternalSession.java +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/InternalSession.java @@ -70,9 +70,10 @@ public interface InternalSession extends Session { org.apache.maven.artifact.repository.ArtifactRepository toArtifactRepository(RemoteRepository repository); - List toDependencies(Collection dependencies); + List toDependencies( + Collection dependencies, boolean managed); - org.eclipse.aether.graph.Dependency toDependency(DependencyCoordinate dependency); + org.eclipse.aether.graph.Dependency toDependency(DependencyCoordinate dependency, boolean managed); List toArtifacts(Collection artifacts);