Hey. I stumbled upon this answer <https://stackove...
# community-support
a
Hey. I stumbled upon this answer https://stackoverflow.com/a/78936660/6761823: > Modern Gradle Behavior: > • Gradle now automatically includes a clean task that targets the
buildDir
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:
Copy code
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?
c
Running
./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).
1
For reference, the default clean task is defined as:
Copy code
private 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()));
    }
e
the base plugin also adds a task rule so that there will be a
cleanFoo
for every
foo
task
a
When I was playing with
--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.
👍 1
v
Yeah, you can just apply the
lifecycle-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. 🙂
🙌 1
And it is also the
lifecycle-base
plugin that adds the
...Clean
task rule btw. 🙂
Actually, I consider this a bug or at least worth a feature request, I created https://github.com/gradle/gradle/issues/33609 for it. 🙂
🙌 1