Skip to content

Commit

Permalink
Fix inefficency in artifactview utils
Browse files Browse the repository at this point in the history
we do not want to do any dependency resolution preemptively
  • Loading branch information
breskeby committed Dec 17, 2024
1 parent fc8d335 commit a2db848
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,6 +52,14 @@ public TaskProvider<? extends Task> createTask(Project project) {
}
}

project.getTasks().register("testResolution", Copy.class, (Action<Copy>) 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<ExportElasticsearchBuildResourcesTask> resourcesTask = project.getTasks()
.register("thirdPartyAuditResources", ExportElasticsearchBuildResourcesTask.class);
Path resourcesDir = project.getBuildDir().toPath().resolve("third-party-audit-config");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,7 +33,7 @@ public static FileCollection createFileCollectionFromNonTransitiveArtifactsView(
) {
ResolvableDependencies incoming = configuration.getIncoming();
return incoming.artifactView(viewConfiguration -> {
Set<ComponentIdentifier> firstLevelDependencyComponents = incoming.getResolutionResult()
Provider<Set<ComponentIdentifier>> firstLevelDependencyComponents = incoming.getResolutionResult()
.getRootComponent()
.map(
rootComponent -> rootComponent.getDependencies()
Expand All @@ -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();
}
Expand All @@ -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<ComponentIdentifier> shadowedDependencies = incoming.getResolutionResult()
Provider<Set<ComponentIdentifier>> shadowedDependencies = incoming.getResolutionResult()
.getRootComponent()
.map(
root -> root.getDependencies()
Expand All @@ -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();
}
}

0 comments on commit a2db848

Please sign in to comment.