我想说:
clean.delete(fileTree("a") {
include "subdir/"
include "aFile"
})
Run Code Online (Sandbox Code Playgroud)
删除目录"subdir"和文件"aFile".但是"subdir"不会被删除.我可以明确地列出它:
clean.delete("a/subdir")
Run Code Online (Sandbox Code Playgroud)
但这比我想要的更重复.fileTree.include可以做这个工作吗?
到目前为止,我想出了:
[ "subdir",
"aFile",
...
].each { it ->
clean.delete("a/$it")
}
Run Code Online (Sandbox Code Playgroud)
但这有点尴尬.
我担心这是不可能的.在gradle论坛上查看此讨论.讨论导致了这个问题.
一个简单的测试显示
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
}
}
Run Code Online (Sandbox Code Playgroud)
遍历目录时,仅打印文件而不打印目录.你的解决方案很好,但你可以保存一些字符:
[ "subdir",
"aFile",
...
].each { clean.delete("a/$it") }
Run Code Online (Sandbox Code Playgroud)
所以fileTree只适用于目录树中的文件而不适用于目录.
| 归档时间: |
|
| 查看次数: |
7814 次 |
| 最近记录: |