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..bf6917f2 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,21 @@ 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"); + gradleBuild.prepareRunner("check", "--configuration-cache").withDebug(false).buildAndFail(); + BuildResult result = gradleBuild.prepareRunner("check", "--configuration-cache") + .withDebug(false) + .buildAndFail(); + String sourcePath = Paths.get("src", "main", "java", "simple", "Simple.java").toString(); + assertThat(result.getOutput()).contains("Reusing configuration cache.") + .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/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 +}