I am using Gradle and would like to delete all files with a certain extension. Is that something Gradle can do?
Use the Gradle Delete
task.
task deleteFiles(type: Delete) {
delete fileTree('dir/foo') {
include '**/*.ext'
}
}
-
6For me this did it:
delete fileTree(dir:'dir/foo', include: '**.ext')
(Gradle 4.1, 2017) – lealceldeiro Sep 22 '17 at 19:19 -
How can I declare an external method of type delete? i.e. ext.deleteFiles = { type: Delete String... fileNames -> fileNames.each{ fileName -> delete $fileName } } – Guerino Rodella Feb 22 '18 at 13:39
-
@GuerinoRodella no need, there is already a
project.delete()
method for this purpose. – Mark Vieira Mar 21 '18 at 3:23 -
Sorry, but you explanation looks wrong. That is just configuration and does not run until
clean
task called – Hubbitus Jul 29 '19 at 16:48
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' }
}
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() }
-
3Beware that this is essentially groovy code using gradle's Project.fileTree API. The code will always be executed during the configuration phase of a gradle build, unless enclosed in a custom task block. – kevinmm Dec 5 '14 at 18:49
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()
}
}
}
-
So hard... Why not just use
doLast
? And what incorrect withclean { delete xxxx }
it is not called if you not call taskclean
– Hubbitus Jul 25 '19 at 13:26 -
Becuase the default clean task have included default delete actions. we need deleteAllActions(). self << { the action is just my situation, normally people can delete the file as they want }. clean { delete xxxx } will call delete if you not call task clean? I think the closure is called at the configuration time. – Victor Choy Jul 26 '19 at 2:58
-
Please try, if it is not clean task, it is not run. It is just task configuration – Hubbitus Jul 26 '19 at 7:36
myBuild.dependsOn('deleteFiles')
) and/or incremental builds. – kevinmm Dec 5 '14 at 18:39