I want to say:
clean.delete(fileTree("a") {
include "subdir/"
include "aFile"
})
to delete the directory "subdir" and the file "aFile". But "subdir" is not deleted. I could list it explicitly:
clean.delete("a/subdir")
but that is more repetitious than I would like. Can fileTree.include be made to do this job?
So far I have come up with:
[ "subdir",
"aFile",
...
].each { it ->
clean.delete("a/$it")
}
but that is just a bit awkward.
I am afraid this is not possible. See this discussion on the gradle forum. The discussion lead to this issue.
A simple test shows
task makeDir << {
['a', 'a/subdir'].each { new File(it).mkdirs() }
new File('a/aFile').createNewFile()
def tree = fileTree('a') {
include 'subdir/'
include 'aFile'
}
tree.each {File file ->
println file
}
}
prints only the file and not the directory, as the directory is traversed. Your solution is good but you could save some chars:
[ "subdir",
"aFile",
...
].each { clean.delete("a/$it") }
So fileTree is just for files in a directory tree and not for directories.
You can specify it this way:
clean {
delete 'a/aFile', 'a/subdir'
}
No idea why fileTree
doesn't 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