From 07ad879c0fb1e1e429560a3377fd3e25332477ff Mon Sep 17 00:00:00 2001 From: jjh75607 Date: Tue, 18 Aug 2026 12:24:01 +0900 Subject: [PATCH 1/2] fix: resolve registerDependencies output lazily so a custom build directory applies --- .../spotless/RegisterDependenciesTask.java | 11 ++-- .../RegisterDependenciesTaskBuildDirTest.java | 63 +++++++++++++++++++ 2 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 plugin-gradle/src/test/java/com/diffplug/gradle/spotless/RegisterDependenciesTaskBuildDirTest.java diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java index 3c66f5427f..96dc160bbe 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/RegisterDependenciesTask.java @@ -24,6 +24,7 @@ import javax.inject.Inject; import org.gradle.api.DefaultTask; +import org.gradle.api.file.RegularFileProperty; import org.gradle.api.provider.Provider; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.Internal; @@ -66,7 +67,8 @@ void setup() { taskService = SpotlessTaskService.registerIfAbsent(getProject(), compositeBuildSuffix); usesService(taskService); getBuildEventsListenerRegistry().onTaskCompletion(taskService); - unitOutput = new File(getProject().getLayout().getBuildDirectory().getAsFile().get(), "tmp/spotless-register-dependencies"); + // lazy, so a build directory set later in the buildscript still counts + getUnitOutput().set(getProject().getLayout().getBuildDirectory().file("tmp/spotless-register-dependencies")); } List steps = new ArrayList<>(); @@ -76,15 +78,12 @@ public List getSteps() { return steps; } - File unitOutput; - @OutputFile - public File getUnitOutput() { - return unitOutput; - } + public abstract RegularFileProperty getUnitOutput(); @TaskAction public void trivialFunction() throws IOException { + File unitOutput = getUnitOutput().get().getAsFile(); Files.createParentDirs(unitOutput); Files.write(Integer.toString(1), unitOutput, StandardCharsets.UTF_8); } diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/RegisterDependenciesTaskBuildDirTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/RegisterDependenciesTaskBuildDirTest.java new file mode 100644 index 0000000000..0fedd8e099 --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/RegisterDependenciesTaskBuildDirTest.java @@ -0,0 +1,63 @@ +/* + * Copyright 2026 DiffPlug + * + * 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 + * + * http://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 com.diffplug.gradle.spotless; + +import java.io.IOException; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +class RegisterDependenciesTaskBuildDirTest extends GradleIntegrationHarness { + @Test + void unitOutputFollowsCustomBuildDirectory() throws IOException { + setFile("settings.gradle").toLines( + "rootProject.name = 'buildDirTest'", + "include 'sub'"); + setFile("build.gradle").toLines( + "plugins {", + " id 'com.diffplug.spotless'", + "}", + "repositories { mavenCentral() }", + "spotless { predeclareDeps() }", + "", + "spotlessPredeclare {", + " java { googleJavaFormat('1.17.0') }", + "}", + "", + "layout.buildDirectory = layout.projectDirectory.dir('custom-build')"); + setFile("sub/build.gradle").toLines( + "plugins {", + " id 'com.diffplug.spotless'", + "}", + "spotless {", + " java {", + " target 'src/main/java/**/*.java'", + " googleJavaFormat('1.17.0')", + " }", + "}"); + setFile("sub/src/main/java/Hello.java").toLines( + "public class Hello {}"); + + gradleRunner().withArguments("spotlessApply").build(); + + Assertions.assertThat(newFile("custom-build/tmp/spotless-register-dependencies")) + .as("register-dependencies output should follow the configured build directory") + .exists(); + Assertions.assertThat(newFile("build/tmp/spotless-register-dependencies")) + .as("nothing should be written under the default build directory") + .doesNotExist(); + } +} From addfa48a58409dbe3a9614d5488cb73e991e6971 Mon Sep 17 00:00:00 2001 From: jjh75607 Date: Tue, 18 Aug 2026 12:35:23 +0900 Subject: [PATCH 2/2] docs: changelog entry for the registerDependencies build directory fix --- plugin-gradle/CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 3913210baf..6618d9e70c 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Fixed +- `spotlessInternalRegisterDependencies` now writes its output under a build directory that is configured after the plugin is applied, instead of always under the default `build/`. ([#2114](https://github.com/diffplug/spotless/issues/2114)) ## [8.10.0] - 2026-08-17 ### Added