How can I delete a file from inside a task?

I’m trying to make a custom task, written in Kotlin, that generates a file through a commandline command. The problem is that this the external commandline program can’t handle if the generated file already exists.

So, what I am trying to do inside my @TaskAction is to delete the contents of the output directory before running the command.

I tried to do the following, but I get Gradle import errors and Project resolve errors.

project.delete(
    fileTree("${project.buildDir}/jpackageAppImage")
)

When I look in the docs for delete, it says that the input to that function can be

Any type of object accepted by files(Object...)

When I look at the docs for files, it says it accepts a a FileTree

Can anyone help me out with this?

try

project.delete(
    files("${project.buildDir}/jpackageAppImage")
)

Or define your own task

task deleteMyFile(type: Delete) {
    paths files("${project.buildDir}/jpackageAppImage")
}

The first one

project.delete(
    files("${project.buildDir}/jpackageAppImage")
)

Causes the same problem :expressionless:

this should work

task myDeleteTask(type: Delete) {
   delete files("${buildDir}/jpackageAppImage")
}

Running it as a separate task worked. However, I don’t want to have to remember to create a separate delete task every time I want to create an installer task.

tasks.register<Delete>("deleteJpackageImage") {
    delete(files("${project.buildDir}/jpackageAppImage"))
}

I’d really like to be able to just have the Installer task work without any outside logic. Also, I’d like to understand why delete inside of my task is failing so that I can have a better understanding of Gradle.

For Delete task you need to comply with gradle Api that’s why you had to create a new task that overrides the Delete default task from gradle.

if you want to register a task that works with install task you can do like this

task myDeleteTask(type: Delete) {
   delete files("${buildDir}/jpackageAppImage")
}

myDeleteTask.mustRunAfter installerTask

installerTask is your custom installer task.

mustRunAfter is a gradle Api that allows to you chain tasks

You can do as follows

task installerTask() {
    //.... your task logic goes here
    doLast {
        delete("${buildDir}/jpackageAppImage")
    }
}

For Delete task you need to comply with gradle Api that’s why you had to create a new task that overrides the Delete default task from gradle.

So Project.delete() can only be used inside of tasks that inherit from org.gradle.api.tasks.Delete?

project.Delete() cannot be called from outside a task or a buildScript. It also requires a path to your File or Dir

project.Delete() cannot be called from outside a task or a buildScript. It also requires a path to your File or Dir

Right, so why doesn’t it work when I do this inside a custom task inside my build.gradle.kts

open class WindowsInstaller () : DefaultTask() {

...

@TaskAction
fun generateWindowsInstaller() {

    project.delete(
        fileTree("${project.buildDir}/jpackageAppImage")
    )

    ...
}
...

}

Unless I am misunderstanding something, according to the docs it should work.

It looks like my mistake was that I needed to do project.fileTree instead of just fileTree.

open class WindowsInstaller () : DefaultTask() {

...

@TaskAction
fun generateWindowsInstaller() {

    project.delete(
        project.fileTree("${project.buildDir}/jpackageAppImage")
    )

    ...
}
...

}