Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?)

like image 394
Ryan Leach Avatar asked Aug 22 '18 12:08

Ryan Leach


2 Answers

delete is a property of the task, and uses 'setter notation' to be set.

It accepts a Set, so using the kotlin setOf syntax, you can define a set of the "file like" types that Gradle supports.

Example:

tasks.create<Delete>("cleanRP") {
    group = "rp"
    delete = setOf (
        "rp-out", "rp-zip"
    )
}
like image 108
Ryan Leach Avatar answered Oct 19 '22 19:10

Ryan Leach


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")
    )
}
like image 2
Ryan Leach Avatar answered Oct 19 '22 18:10

Ryan Leach