I want to append some extra delete functionality to the clean task (for Java builds).
So I try adding the following to my gradle build script.
clean.doLast{
delete ('test.txt')
}
When I tun the "clean" task my sample file doesn't get deleted ... I also don't get any error message indicated what happened.
If I try the following:
task deleteStuff(type: Delete) {
delete 'test.txt'
}
Things do work.
Can I not add (via doLast) delete functionality to tasks? What is the proper way of doing this (without hacking in Ant tasks).
The long answer: Your first example mixes the two ways to delete files in Gradle. The first one is to use a task of type Delete , the second one is to invoke the method delete of the type Project .
Gradle adds the task rule clean<Taskname> to our projects when we apply the base plugin. This task is able to remove any output files or directories we have defined for our task. For example we can assign an output file or directory to our task with the outputs property.
You can call . \gradlew --stop in a Terminal and it will kill all gradle processes in Android Studio for Windows OS. You Can find terminal at left bottom of your android studio for windows. Save this answer.
In these two code snippets, you aren't calling Project.delete()
but Delete.delete()
. In other words, you are configuring the Delete
task. Doing this after the Delete
task has executed (as in the first snippet) is too late.
In the case of a Delete
task, there is no good reason to add a delete
operation with doLast
. Your second snippet is clearly preferable. For other tasks, the doLast
approach will work because they don't have a delete
method. Or you can disambiguate with project.delete()
.
If the reason is to avoid spending time in the preparation phase (which is executed for every task), doFirst can be used to set up the Delete-task. So using clean.doFirst instead of clean.doLast in the example above will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With