From a2db8487dfc2bf2712f83c5a8ff94adb8a08c190 Mon Sep 17 00:00:00 2001 From: Rene Groeschke Date: Tue, 17 Dec 2024 01:20:37 +0100 Subject: [PATCH] Fix inefficency in artifactview utils we do not want to do any dependency resolution preemptively --- .../precommit/ThirdPartyAuditPrecommitPlugin.java | 10 ++++++++++ .../gradle/internal/util/DependenciesUtils.java | 15 +++++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/ThirdPartyAuditPrecommitPlugin.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/ThirdPartyAuditPrecommitPlugin.java index c2370bd401b3..e6b5fac0767c 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/ThirdPartyAuditPrecommitPlugin.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/precommit/ThirdPartyAuditPrecommitPlugin.java @@ -12,11 +12,13 @@ import org.elasticsearch.gradle.dependencies.CompileOnlyResolvePlugin; import org.elasticsearch.gradle.internal.ExportElasticsearchBuildResourcesTask; import org.elasticsearch.gradle.internal.conventions.precommit.PrecommitPlugin; +import org.gradle.api.Action; import org.gradle.api.Project; import org.gradle.api.Task; import org.gradle.api.artifacts.Configuration; import org.gradle.api.artifacts.component.ModuleComponentIdentifier; import org.gradle.api.file.FileCollection; +import org.gradle.api.tasks.Copy; import org.gradle.api.tasks.TaskProvider; import java.io.File; @@ -50,6 +52,14 @@ public TaskProvider createTask(Project project) { } } + project.getTasks().register("testResolution", Copy.class, (Action) task -> { + task.into("testresolution"); + Configuration runtimeConfiguration = project.getConfigurations().getByName("runtimeClasspath"); + FileCollection runtimeThirdParty = thirdPartyDependenciesView(runtimeConfiguration); + runtimeConfiguration.getIncoming().beforeResolve(resolvableDependencies -> { throw new RuntimeException("This is a test"); }); + task.from(runtimeThirdParty); + }); + TaskProvider resourcesTask = project.getTasks() .register("thirdPartyAuditResources", ExportElasticsearchBuildResourcesTask.class); Path resourcesDir = project.getBuildDir().toPath().resolve("third-party-audit-config"); diff --git a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/util/DependenciesUtils.java b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/util/DependenciesUtils.java index 180578696b44..5d7386e2c215 100644 --- a/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/util/DependenciesUtils.java +++ b/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/util/DependenciesUtils.java @@ -18,6 +18,7 @@ import org.gradle.api.artifacts.result.ResolvedComponentResult; import org.gradle.api.artifacts.result.ResolvedDependencyResult; import org.gradle.api.file.FileCollection; +import org.gradle.api.provider.Provider; import org.gradle.api.specs.AndSpec; import org.gradle.api.specs.Spec; @@ -32,7 +33,7 @@ public static FileCollection createFileCollectionFromNonTransitiveArtifactsView( ) { ResolvableDependencies incoming = configuration.getIncoming(); return incoming.artifactView(viewConfiguration -> { - Set firstLevelDependencyComponents = incoming.getResolutionResult() + Provider> firstLevelDependencyComponents = incoming.getResolutionResult() .getRootComponent() .map( rootComponent -> rootComponent.getDependencies() @@ -42,10 +43,9 @@ public static FileCollection createFileCollectionFromNonTransitiveArtifactsView( .filter(dependency -> dependency.getSelected() instanceof ResolvedComponentResult) .map(dependency -> dependency.getSelected().getId()) .collect(Collectors.toSet()) - ) - .get(); + ); viewConfiguration.componentFilter( - new AndSpec<>(identifier -> firstLevelDependencyComponents.contains(identifier), componentFilter) + new AndSpec<>(identifier -> firstLevelDependencyComponents.get().contains(identifier), componentFilter) ); }).getFiles(); } @@ -59,7 +59,7 @@ public static FileCollection thirdPartyDependenciesView(Configuration configurat ResolvableDependencies incoming = configuration.getIncoming(); return incoming.artifactView(v -> { // resolve componentIdentifier for all shadowed project dependencies - Set shadowedDependencies = incoming.getResolutionResult() + Provider> shadowedDependencies = incoming.getResolutionResult() .getRootComponent() .map( root -> root.getDependencies() @@ -70,10 +70,9 @@ public static FileCollection thirdPartyDependenciesView(Configuration configurat .filter(dep -> dep.getSelected() instanceof ResolvedComponentResult) .map(dep -> dep.getSelected().getId()) .collect(Collectors.toSet()) - ) - .get(); + ); // filter out project dependencies if they are not a shadowed dependency - v.componentFilter(i -> (i instanceof ProjectComponentIdentifier == false || shadowedDependencies.contains(i))); + v.componentFilter(i -> (i instanceof ProjectComponentIdentifier == false || shadowedDependencies.get().contains(i))); }).getFiles(); } }