Search code examples
gradlegradle-kotlin-dsl

Custom Clean / delete task in build.gradle.kts


I have an extremely basic custom build (Minecraft resource pack zipping and copying)

Directory layout

Project
- src (future Java/Kotlin sources for resource pack preprocessing)
- build (future Java/Kotlin class output folder, kept seperate from produced assets)
- rp (the 'src' folder for the resource pack assets, pre-processing.
  - assets
- rp-out (the 'build' folder for the resource pack assets, post-processing.
  - ResourcePack.zip
  - ResourcePack
    - variousAssetsToZip

and the following build.gradle.kts

plugins {
    id("base")
}

tasks.create<Copy>("copy") {
    description = "Basic copy"
    group = "custom"
    from("rp")
    into("rp-out/${project.name}")
}

tasks.create<Zip>("zip") {
    description = "Archives the resource pack output"
    group = "Archive"
    from("rp-out/${project.name}")
    destinationDir = file("rp-out")
}

tasks.create<Delete>("cleanRP") {
    group = "build"
    delete {
        file("rp-out")
        file("rp-zip")
    }
}

I expect that this would clean / delete either the folders rp-out, rp-zip themselves, or the contents.

But I'm unable to get the cleanRP task to delete the folder contents, it simply completes the task, without any seeming effect.

I'm a little familiar with gradle for Java projects, and this is the first Kotlin scripting I've done, but I have been moderately paying attention to conference talks.

How can I debug this effectively? Given the early stage of gradle kotlin-dsl How should I approach learning on my own?

(Also what's the solution to this problem?)


Solution

  • You should use delete() configuration methods instead, so tasks can be correctly chained.

    tasks.create<Delete>("cleanRP") {
        group = "rp"
        delete(
            fileTree("rp-out"), 
            fileTree("rp-zip")
        )
    }