diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java index 0e68d76e0f..a30f4edeb8 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java @@ -730,7 +730,7 @@ protected void setupTask(SpotlessTask task) { spotless.getRegisterDependenciesTask().hookSubprojectTask(task); } if (getRatchetFrom() != null) { - task.setupRatchet(spotless.getRegisterDependenciesTask().gitRatchet, getRatchetFrom()); + task.setupRatchet(spotless.getRegisterDependenciesTask().getGitRatchet().get(), getRatchetFrom()); } } diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GitRatchetGradle.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GitRatchetGradle.java index b3554d57a0..5a7f402b89 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GitRatchetGradle.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/GitRatchetGradle.java @@ -22,11 +22,13 @@ import org.gradle.api.Project; import org.gradle.api.services.BuildService; import org.gradle.api.services.BuildServiceParameters; +import org.gradle.tooling.events.FinishEvent; +import org.gradle.tooling.events.OperationCompletionListener; import com.diffplug.spotless.extra.GitRatchet; /** Gradle implementation of GitRatchet. */ -public abstract class GitRatchetGradle extends GitRatchet implements BuildService { +public abstract class GitRatchetGradle extends GitRatchet implements BuildService, OperationCompletionListener { @Override protected File getDir(Project project) { return project.getProjectDir(); @@ -36,4 +38,9 @@ protected File getDir(Project project) { protected @Nullable Project getParent(Project project) { return project.getParent(); } + + @Override + public void onFinish(FinishEvent finishEvent) { + // NOOP + } } 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 b147301334..dc435a1f12 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 @@ -21,12 +21,17 @@ import java.util.ArrayList; import java.util.List; +import javax.inject.Inject; + import org.gradle.api.DefaultTask; import org.gradle.api.execution.TaskExecutionGraph; +import org.gradle.api.provider.Property; +import org.gradle.api.provider.Provider; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.Internal; import org.gradle.api.tasks.OutputFile; import org.gradle.api.tasks.TaskAction; +import org.gradle.build.event.BuildEventsListenerRegistry; import com.diffplug.common.base.Preconditions; import com.diffplug.common.io.Files; @@ -42,7 +47,7 @@ * - When this "registerDependencies" task does its up-to-date check, it queries the task execution graph to see which * SpotlessTasks are at risk of being executed, and causes them all to be evaluated safely in the root buildscript. */ -public class RegisterDependenciesTask extends DefaultTask { +public abstract class RegisterDependenciesTask extends DefaultTask { static final String TASK_NAME = "spotlessInternalRegisterDependencies"; @Input @@ -86,7 +91,9 @@ void setup() { Preconditions.checkArgument(getProject().getRootProject() == getProject(), "Can only be used on the root project"); unitOutput = new File(getProject().getBuildDir(), "tmp/spotless-register-dependencies"); rootProvisioner = new GradleProvisioner.RootProvisioner(getProject()); - gitRatchet = getProject().getGradle().getSharedServices().registerIfAbsent("GitRatchetGradle", GitRatchetGradle.class, unused -> {}).get(); + Provider gitRatchetProvider = getProject().getGradle().getSharedServices().registerIfAbsent("GitRatchetGradle", GitRatchetGradle.class, unused -> {}); + getBuildEventsListenerRegistry().onTaskCompletion(gitRatchetProvider); + getGitRatchet().set(gitRatchetProvider); } @TaskAction @@ -95,10 +102,9 @@ public void trivialFunction() throws IOException { Files.write(Integer.toString(getSteps().size()), unitOutput, StandardCharsets.UTF_8); } - GitRatchetGradle gitRatchet; - @Internal - GitRatchetGradle getGitRatchet() { - return gitRatchet; - } + public abstract Property getGitRatchet(); + + @Inject + protected abstract BuildEventsListenerRegistry getBuildEventsListenerRegistry(); }