From bee6e34bc797bb3ed47e955a866620f249e7085f Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 17 Jan 2025 11:03:14 +0000 Subject: [PATCH] Add a task to warm Gradle's caches A new warmCaches task is added to each smoke test. This task downloads all of the dependencies on the classpaths of the test's main, test, and appTest source sets. It is intended for us in GitHub Actions to make more efficient use of its cache and increase the likelihood of a cache hit when running each individual set of smoke tests. --- .../aot/gradle/AotSmokeTestPlugin.java | 17 +++++- .../aot/gradle/tasks/WarmCaches.java | 52 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 gradle/plugins/aot-smoke-test-plugin/src/main/java/org/springframework/aot/gradle/tasks/WarmCaches.java diff --git a/gradle/plugins/aot-smoke-test-plugin/src/main/java/org/springframework/aot/gradle/AotSmokeTestPlugin.java b/gradle/plugins/aot-smoke-test-plugin/src/main/java/org/springframework/aot/gradle/AotSmokeTestPlugin.java index 8a6091e2..0c5b5c8f 100644 --- a/gradle/plugins/aot-smoke-test-plugin/src/main/java/org/springframework/aot/gradle/AotSmokeTestPlugin.java +++ b/gradle/plugins/aot-smoke-test-plugin/src/main/java/org/springframework/aot/gradle/AotSmokeTestPlugin.java @@ -1,5 +1,5 @@ /* - * Copyright 2022-2024 the original author or authors. + * Copyright 2022-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -65,6 +65,7 @@ import org.springframework.aot.gradle.tasks.StartApplication; import org.springframework.aot.gradle.tasks.StartJvmApplication; import org.springframework.aot.gradle.tasks.StartNativeApplication; import org.springframework.aot.gradle.tasks.StopApplication; +import org.springframework.aot.gradle.tasks.WarmCaches; import org.springframework.boot.gradle.plugin.SpringBootPlugin; import org.springframework.boot.gradle.tasks.bundling.BootJar; @@ -181,6 +182,20 @@ public class AotSmokeTestPlugin implements Plugin { DependencyHandler dependencies = project.getRootProject().getDependencies(); dependencies.add(smokeTests.getName(), dependencies.project(Map.of("path", project.getPath(), "configuration", smokeTests.getName()))); + project.getTasks().register("warmCaches", WarmCaches.class, (warmCaches) -> { + sourceSets + .matching((sourceSet) -> List + .of(SourceSet.MAIN_SOURCE_SET_NAME, SourceSet.TEST_SOURCE_SET_NAME, APP_TEST_SOURCE_SET_NAME) + .contains(sourceSet.getName())) + .all((sourceSet) -> { + warmCaches.addDependencies( + project.getConfigurations().getByName(sourceSet.getAnnotationProcessorConfigurationName())); + warmCaches.addDependencies( + project.getConfigurations().getByName(sourceSet.getCompileClasspathConfigurationName())); + warmCaches.addDependencies( + project.getConfigurations().getByName(sourceSet.getRuntimeClasspathConfigurationName())); + }); + }); } private void enableJavaCompileLinting(Project project, SourceSetContainer sourceSets) { diff --git a/gradle/plugins/aot-smoke-test-plugin/src/main/java/org/springframework/aot/gradle/tasks/WarmCaches.java b/gradle/plugins/aot-smoke-test-plugin/src/main/java/org/springframework/aot/gradle/tasks/WarmCaches.java new file mode 100644 index 00000000..b7849ff0 --- /dev/null +++ b/gradle/plugins/aot-smoke-test-plugin/src/main/java/org/springframework/aot/gradle/tasks/WarmCaches.java @@ -0,0 +1,52 @@ +/* + * Copyright 2022-2025 the original author or authors. + * + * Licensed 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 + * + * https://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 KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.aot.gradle.tasks; + +import org.gradle.api.DefaultTask; +import org.gradle.api.file.ConfigurableFileCollection; +import org.gradle.api.file.FileCollection; +import org.gradle.api.tasks.Classpath; +import org.gradle.api.tasks.TaskAction; + +/** + * Task to warm Gradle's caches by downloading the configured dependencies. + * + * @author Andy Wilkinson + */ +public class WarmCaches extends DefaultTask { + + private final ConfigurableFileCollection dependencies; + + public WarmCaches() { + this.dependencies = getProject().getObjects().fileCollection(); + } + + public void addDependencies(FileCollection dependencies) { + this.dependencies.from(dependencies); + } + + @TaskAction + void execute() { + this.dependencies.getFiles(); + } + + @Classpath + FileCollection getDependencies() { + return this.dependencies; + } + +}