I am using Gradle and would like to delete all files with a certain extension. Is that something Gradle can do?
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() }
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' }
}
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()
}
}
}
Use the Gradle Delete task.
task deleteFiles(type: Delete) {
delete fileTree('dir/foo') {
include '**/*.ext'
}
}