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).
In these two code snippets, you aren’t calling
Project.delete()butDelete.delete(). In other words, you are configuring theDeletetask. Doing this after theDeletetask has executed (as in the first snippet) is too late.In the case of a
Deletetask, there is no good reason to add adeleteoperation withdoLast. Your second snippet is clearly preferable. For other tasks, thedoLastapproach will work because they don’t have adeletemethod. Or you can disambiguate withproject.delete().