Andrzej Zabost
05/23/2025, 1:15 PMbuildDir
directory in each project (including the top-level project).
but it doesn't seem to be the case when I'm testing it in my project running Gradle 8.10.2.
If I delete the custom clean
task registered in the top-level build.gradle.kts
file:
tasks.register("clean", Delete::class) {
delete(rootProject.layout.buildDirectory)
}
then run ./gradlew clean --dry-run
, I can't see the top-level clean
task on the list.
If I restore the custom clean
task in build.gradle.kts
and re-run that command, the task is listed as expected. So it doesn't seem to be true that "modern Gradle automatically includes this task for top-level project".
Can someone clarify if I'm expected to manually register a clean
task in root build.gradle.kts
or is this practice deprecated in favor of something else?Chris Lee
05/23/2025, 1:38 PM./gradlew clean
isn’t directly running the “clean” task; it runs clean for all projects (including root) in which that task exists. Any project that directly/indirectly applies the LifecycleBasePlugin
will have the clean task registered. If your root project doesn’t apply plugins it won’t have a clean task (and may not need one if it isn’t doing any work).Chris Lee
05/23/2025, 1:39 PMprivate void addClean(final ProjectInternal project) {
Provider<Directory> buildDir = project.getLayout().getBuildDirectory();
// Register at least the project buildDir as a directory to be deleted.
final BuildOutputCleanupRegistry buildOutputCleanupRegistry = project.getServices().get(BuildOutputCleanupRegistry.class);
buildOutputCleanupRegistry.registerOutputs(buildDir);
final Provider<Delete> clean = project.getTasks().register(CLEAN_TASK_NAME, Delete.class, cleanTask -> {
cleanTask.setDescription("Deletes the build directory.");
cleanTask.setGroup(BUILD_GROUP);
cleanTask.delete(buildDir);
});
buildOutputCleanupRegistry.registerOutputs(clean.map(cl -> cl.getTargetFiles()));
}
ephemient
05/23/2025, 2:06 PMcleanFoo
for every foo
taskAndrzej Zabost
05/23/2025, 3:09 PM--configuration-cache
I noticed the root's build
directory contained some configuration cache related report so, naturally, I would expect it to be cleaned after running clean
task, but like you said, it doesn't seem to be registered automatically if no plugins are applied to the root project.Vampire
05/24/2025, 10:19 PMlifecycle-base
plugin, that is enough to get the task that deletes the configured build directory exactly like with your custom task, or you register your custom task, doesn't make that much of a difference. 🙂Vampire
05/24/2025, 10:31 PMlifecycle-base
plugin that adds the ...Clean
task rule btw. 🙂Vampire
05/24/2025, 10:50 PM