From 5d022d2a243e0c1746a1355c46029720a78d38b4 Mon Sep 17 00:00:00 2001 From: Eric Haag Date: Fri, 7 Jul 2023 11:16:20 -0500 Subject: [PATCH] Fix build cache miss in runtimeHintsTest task Prior to this change, a build cache miss would occur in runtimeHintsTest tasks when executing the build from two different locations, causing the tasks to re-execute. This was due to an absolute path to the agent jar being included in the -javaagent argument supplied to the test JVM process. This change introduces a RuntimeHintsAgentArgumentProvider allowing finer grained control over the way Gradle computes the build cache key. With this change, the contents of the agent jar file are considered, rather than the absolute file location. See gh-30838 --- .../build/hint/RuntimeHintsAgentPlugin.java | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/buildSrc/src/main/java/org/springframework/build/hint/RuntimeHintsAgentPlugin.java b/buildSrc/src/main/java/org/springframework/build/hint/RuntimeHintsAgentPlugin.java index 8f481c8186..c9df0b5b73 100644 --- a/buildSrc/src/main/java/org/springframework/build/hint/RuntimeHintsAgentPlugin.java +++ b/buildSrc/src/main/java/org/springframework/build/hint/RuntimeHintsAgentPlugin.java @@ -18,9 +18,16 @@ package org.springframework.build.hint; import org.gradle.api.Plugin; import org.gradle.api.Project; +import org.gradle.api.file.ConfigurableFileCollection; import org.gradle.api.plugins.JavaPlugin; +import org.gradle.api.provider.Property; +import org.gradle.api.tasks.Classpath; +import org.gradle.api.tasks.Input; import org.gradle.api.tasks.bundling.Jar; import org.gradle.api.tasks.testing.Test; +import org.gradle.process.CommandLineArgumentProvider; + +import java.util.Collections; /** * {@link Plugin} that configures the {@code RuntimeHints} Java agent to test tasks. @@ -49,10 +56,31 @@ public class RuntimeHintsAgentPlugin implements Plugin { test.systemProperty("org.graalvm.nativeimage.imagecode", "runtime"); }); project.afterEvaluate(p -> { - Jar jar = project.getRootProject().project("spring-core-test").getTasks().withType(Jar.class).named("jar").get(); - agentTest.jvmArgs("-javaagent:" + jar.getArchiveFile().get().getAsFile() + "=" + agentExtension.asJavaAgentArgument()); + agentTest.getJvmArgumentProviders().add(createRuntimeHintsAgentArgumentProvider(project, agentExtension)); }); project.getTasks().getByName("check", task -> task.dependsOn(agentTest)); }); } + + private static RuntimeHintsAgentArgumentProvider createRuntimeHintsAgentArgumentProvider(Project project, RuntimeHintsAgentExtension agentExtension) { + Jar jar = project.getRootProject().project("spring-core-test").getTasks().withType(Jar.class).named("jar").get(); + RuntimeHintsAgentArgumentProvider agentArgumentProvider = project.getObjects().newInstance(RuntimeHintsAgentArgumentProvider.class); + agentArgumentProvider.getAgentJar().from(jar.getArchiveFile()); + agentArgumentProvider.getJavaAgentArgument().set(agentExtension.asJavaAgentArgument()); + return agentArgumentProvider; + } + + interface RuntimeHintsAgentArgumentProvider extends CommandLineArgumentProvider { + + @Classpath + ConfigurableFileCollection getAgentJar(); + + @Input + Property getJavaAgentArgument(); + + @Override + default Iterable asArguments() { + return Collections.singleton("-javaagent:" + getAgentJar().getSingleFile() + "=" + getJavaAgentArgument().get()); + } + } }