From 906d066ccc479132f4a60beb88cf12101be95ecc Mon Sep 17 00:00:00 2001 From: dragonfsky Date: Mon, 3 Aug 2026 01:26:05 +0800 Subject: [PATCH] Support Gradle's configuration cache Closes gh-461 Signed-off-by: dragonfsky --- .../javaformat/gradle/tasks/CheckFormat.java | 14 ++++++++++++- .../javaformat/gradle/CheckTaskTests.java | 21 +++++++++++++++++++ .../gradle/testkit/GradleBuild.java | 9 +++++++- .../src/test/resources/check-bad/build.gradle | 4 +++- 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/main/java/io/spring/javaformat/gradle/tasks/CheckFormat.java b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/main/java/io/spring/javaformat/gradle/tasks/CheckFormat.java index d6504edf..9e73a3d1 100644 --- a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/main/java/io/spring/javaformat/gradle/tasks/CheckFormat.java +++ b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/main/java/io/spring/javaformat/gradle/tasks/CheckFormat.java @@ -24,6 +24,9 @@ import java.util.List; import java.util.stream.Collectors; +import javax.inject.Inject; + +import org.gradle.api.file.ProjectLayout; import org.gradle.api.tasks.CacheableTask; import org.gradle.api.tasks.OutputFile; import org.gradle.api.tasks.TaskAction; @@ -49,8 +52,15 @@ public class CheckFormat extends FormatterTask { */ public static final String DESCRIPTION = "Run Spring Java formatting checks"; + private final ProjectLayout projectLayout; + private File reportLocation; + @Inject + public CheckFormat(ProjectLayout projectLayout) { + this.projectLayout = projectLayout; + } + @TaskAction public void checkFormatting() throws IOException, InterruptedException { List problems = formatFiles().filter(FileEdit::hasEdits) @@ -59,7 +69,9 @@ public void checkFormatting() throws IOException, InterruptedException { this.reportLocation.getParentFile().mkdirs(); if (!problems.isEmpty()) { StringBuilder message = new StringBuilder("Formatting violations found in the following files:\n"); - problems.stream().forEach((f) -> message.append(" * " + getProject().relativePath(f) + "\n")); + File projectDirectory = this.projectLayout.getProjectDirectory().getAsFile(); + problems.stream() + .forEach((f) -> message.append(" * " + projectDirectory.toPath().relativize(f.toPath()) + "\n")); message.append("\nRun `format` to fix."); Files.write(this.reportLocation.toPath(), Collections.singletonList(message.toString()), StandardOpenOption.CREATE); diff --git a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java index 3f413277..5e28538a 100644 --- a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java +++ b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/CheckTaskTests.java @@ -21,6 +21,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.Arrays; import java.util.stream.Stream; @@ -128,6 +129,26 @@ void whenFirstInvocationFailsThenSecondInvocationFails() throws IOException { assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.FAILED); } + @Test + void whenUsingConfigurationCacheThenFormattingViolationsAreReported() throws IOException { + GradleBuild gradleBuild = this.gradleBuild.source("src/test/resources/check-bad") + .gradleVersion("9.1.0") + .debug(false); + BuildResult result = gradleBuild.buildAndFail("check", "--configuration-cache"); + assertFormattingViolationWithoutConfigurationCacheProblems(result); + result = gradleBuild.buildAndFail("check", "--configuration-cache"); + assertFormattingViolationWithoutConfigurationCacheProblems(result); + assertThat(result.getOutput()).contains("Reusing configuration cache."); + } + + private void assertFormattingViolationWithoutConfigurationCacheProblems(BuildResult result) { + String sourcePath = Paths.get("src", "main", "java", "simple", "Simple.java").toString(); + assertThat(result.getOutput()).contains("Formatting violations found in the following files:") + .contains(" * " + sourcePath) + .doesNotContain("Task.project"); + assertThat(result.task(":checkFormatMain").getOutcome()).isEqualTo(TaskOutcome.FAILED); + } + private void copyNormalizedFolder(Path source, Path target) throws IOException { try (Stream stream = Files.walk(source)) { stream.forEach((child) -> { diff --git a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/testkit/GradleBuild.java b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/testkit/GradleBuild.java index 1414da78..7c68d2ab 100644 --- a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/testkit/GradleBuild.java +++ b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/java/io/spring/javaformat/gradle/testkit/GradleBuild.java @@ -57,6 +57,8 @@ public class GradleBuild { private String gradleVersion; + private boolean debug = true; + private GradleVersion expectDeprecationWarnings; void before() throws IOException { @@ -109,7 +111,7 @@ public GradleRunner prepareRunner(String... arguments) throws IOException { String scriptContent = new String(Files.readAllBytes(buildFile.toPath())).replace("{version}", getSpringFormatVersion()); Files.write(buildFile.toPath(), scriptContent.getBytes(StandardCharsets.UTF_8)); - GradleRunner gradleRunner = GradleRunner.create().withProjectDir(this.projectDir).withDebug(true); + GradleRunner gradleRunner = GradleRunner.create().withProjectDir(this.projectDir).withDebug(this.debug); if (this.gradleVersion != null) { gradleRunner.withGradleVersion(this.gradleVersion); } @@ -168,6 +170,11 @@ public String getGradleVersion() { return this.gradleVersion; } + public GradleBuild debug(boolean debug) { + this.debug = debug; + return this; + } + private String getSpringFormatVersion() { return evaluateExpression( "/*[local-name()='project']/*[local-name()='parent']/*[local-name()='version']" + "/text()"); diff --git a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/resources/check-bad/build.gradle b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/resources/check-bad/build.gradle index d6ca2f32..205f3335 100644 --- a/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/resources/check-bad/build.gradle +++ b/spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/test/resources/check-bad/build.gradle @@ -7,4 +7,6 @@ buildscript { apply plugin: 'java' apply plugin: 'io.spring.javaformat' -sourceCompatibility = 1.8 +java { + sourceCompatibility = JavaVersion.VERSION_1_8 +}