通过Gradle FileTree.include删除目录 [英] Deleting directories via gradle FileTree.include

查看:701
本文介绍了通过Gradle FileTree.include删除目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想说:

  clean.delete(fileTree(a){
includesubdir /
包含aFile
})

删除目录子目录和文件aFile。但是subdir不会被删除。我可以明确地列出它:

  clean.delete(a / subdir)

但这比我想要的更重复。可以fileTree.include做这个工作吗?



到目前为止,我已经拿出:

  [subdir,
aFile,
...
] .each {it - >
clean.delete(a / $ it)
}

但是这只是有点尴尬。

解决方案

恐怕这是不可能的。在Gradle论坛上查看此讨论。讨论导致这个问题

一个简单的测试显示

 任务makeDir<< {
['a','a / subdir']。each {new File(it).mkdirs()}
新文件('a / aFile')。createNewFile()

def tree = fileTree('a'){
include'subdir /'
include'aFile'
}

tree.each {档案文件 - >
println file


仅打印文件并而不是目录,因为目录被遍历。您的解决方案很好,但您可以保存一些字符:

  [subdir,
aFile,
...
] .each {clean.delete(a / $ it)}

因此,fileTree仅适用于目录树中的文件,而不适用于目录。


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.

这篇关于通过Gradle FileTree.include删除目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆