Reputation: 1023
As part of Gradle delete task, I would like to delete all tar files beginning with CSW
and ending with .tar.gz
from a directory. How can I achieve it with Groovy?
I tried something like below:
delete (file('delivery/').eachDirMatch(~/^CSW$.*.gz/))
But it doesn't work. How do I employ the regex in Groovy. As compared to shell it is something like rm -rf CSW*.tar.gz
Upvotes: 1
Views: 1433
Reputation: 4482
I will answer with a gradle solution as you refer to gradle in your question. Something along the lines of:
myTask(type: Delete) {
delete fileTree(dir: 'delivery' , include: '**/CSW*.tar.gz')
}
where the delete
method call is configuration time and configures what will be deleted when the task eventually runs. For details it might be worth looking through the gradle docs on the fileTree method.
If you need to stay pure groovy you could do something along the lines of:
new AntBuilder().fileScanner {
fileset(dir: 'delivery', includes: '**/CSW*.tar.gz')
}.each { File f ->
f.delete()
}
If this code lives in a gradle script I would recommend sticking with option one as it retains the gradle up-to-date checking and fits well within the gradle configuration vs execution time pattern.
If you really want to go regex as opposed to the above style patterns you can certainly do that, though I would personally not see much of a point given your requirements.
Upvotes: 2