Gradle - Delete files with certain extension

前端 未结 4 1646
再見小時候
再見小時候 2020-12-18 17:42

I am using Gradle and would like to delete all files with a certain extension. Is that something Gradle can do?

相关标签:
4条回答
  • 2020-12-18 18:04

    There are several ways to delete files with certain extension.In general,you must select some files;then filter some of them and finally delete reminding file.For example try this :

    def tree = fileTree('${SOME_DIR}') 
    tree.include '**/*.${SOME_EXT}'
    tree.each { it.delete() }
    
    0 讨论(0)
  • 2020-12-18 18:08

    You may customize default clean task to include other directories and files for deletion like:

    clean{
      delete 'buildDir', 'generated'
    }
    

    If you want use glob, you may use fileTree for example, or any other convenient methods to list files:

    clean{
      delete 'build', 'target', fileTree(dockerBuildDir) { include '**/*.rpm' }
    }
    
    0 讨论(0)
  • clean {
      delete xxxx
    }
    

    The above clean is not so correct. Because it actually calls a method named clean by task name. The kind of method is normally used to configure the task and it happened in the configuration time not in the task-execute time.

    The following is a more reasonable way if you need modify the default clean. It's my example to delete all files except one excludes. The delete action in clean task really happen in task-execute time now.

        clean {
    
        Task self = delegate
        self.deleteAllActions()
        self << {
    
            project.delete(fileTree(dir: buildDir).exclude("**/tmp/expandedArchives/org.jacoco.agent*/**"))
    
            def emptyDirs = []
    
            project.fileTree(dir: buildDir).visit {
                File f = it.file
    
                if (f.isDirectory() ) {
                    def children = project.fileTree(f).filter { it.isFile() }.files
                    if (children.size() == 0) {
                        emptyDirs << f
                    }
                }
            }
    
            // reverse so that we do the deepest folders first
            emptyDirs.reverseEach {
                println "delete file: " + it + ", " + project.delete(it) //it.delete()
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-18 18:25

    Use the Gradle Delete task.

    task deleteFiles(type: Delete) {
        delete fileTree('dir/foo') {
            include '**/*.ext'
        }
    }
    
    0 讨论(0)
提交回复
热议问题