Gradle任务删除目录中的内容但排除jar文件

时间:2018-03-22 19:56:47

标签: gradle

我仍然在尝试学习Gradle Task创作。

我找到了以下解决方案来帮助删除目录的内容,同时保持目录不变。

但是,我可以帮助修改它以排除特定文件,例如.jar文件吗?

How to delete an empty directory (or the directory with all contents recursively) in gradle?

解决方案由Heri提供

task deleteGraphicsAssets(type: Delete) {
    def dirName = "src" 
        file( dirName ).list().each{
        f -> delete "${dirName}/${f}"
    }
}

1 个答案:

答案 0 :(得分:0)

task deleteGraphicsAssets(type: Delete) {
    doLast {
        file('src').eachFileRecurse(groovy.io.FileType.FILES) { File file ->
            //Delete all files except *.jar
            if (!file.name.endsWith('.jar')) {
                delete file
            }

        }
    }
}

另一种选择是将Filetree用于更高级的过滤器,例如:

task deleteGraphicsAssets(type: Delete) {
    delete fileTree(dir: "tempDir", exclude: "dontDelete.jar")
}