How do I extend gradle's clean task to delete a file?
Gradlebuild.gradleGradle TaskGradle Problem Overview
So far I've added the following to my build.gradle
apply plugin: 'base'
clean << {
delete '${rootDir}/api-library/auto-generated-classes/'
println '${rootDir}/api-library/auto-generated-classes/'
}
However, not only is my file not deleted, but the print statement shows that ${rootDir}
is not being converted to the root directory of my project.
Why won't this work, what concepts am I missing?
Gradle Solutions
Solution 1 - Gradle
You just need to use double quotes. Also, drop the <<
and use doFirst
instead if you are planning to do the deletion during execution. Something like this:
clean.doFirst {
delete "${rootDir}/api-library/auto-generated-classes/"
println "${rootDir}/api-library/auto-generated-classes/"
}
Gradle build scripts are written in Groovy DSL. In Groovy you need to use double quotes for string interpolation (when you are using ${}
as placeholders). Take a look at here.
Solution 2 - Gradle
<<
is equivalent for clean.doLast
.
doFirst
and doLast
are ordering the operations at the execution phase,
which is seldom relevant for delete operations.
In this case you don't need any of them. The clean task from base is of type [Delete][1], so you simply need to pass it a closure to tell it at configuration time what to delete when it executes:
clean {
delete 'someFile'
}
AS [mushfek0001][2] correctly points it out in his answer, you should use double quotes for variable interpolation to work:
clean {
delete "${buildDir}/someFile"
}
You need to have at least the base plugin applied for this to work, most other plugins, like the Java plugin either apply base or declare their own
clean
task of type delete [Delete][1] task. The error you would get if you don't have this is a missing clean
method one.
apply plugin: 'base'
[1]: https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/Delete.html "Gradle Delete Task" [2]: https://stackoverflow.com/a/29813360/740641
Solution 3 - Gradle
In order to extend the clean task, you can use
clean.doFirst {}
or
clean.doLast {}
These will allow you to inject your own actions into the clean process. In order to delete files and directories you can use the "file" API which doesn't require any additional plugins.
Here is an example that will delete both a file and a directory as the last step in the clean task:
clean.doLast {
file('src/main/someFile.txt').delete()
file('src/main/libs').deleteDir()
}
Solution 4 - Gradle
Below one works for me (I prefer to use dependsOn
),
task customCleanUp(type:Delete) {
delete "your_folder", "your_file"
}
tasks.clean.dependsOn(tasks.customCleanUp)
Solution 5 - Gradle
Gradle Kotlin Script analogue:
tasks {
getByName<Delete>("clean") {
delete.add("logs") // add accepts argument with Any type
}
}
Solution 6 - Gradle
There are options in gradle where you can extend a particular task ("clean" in your case) with doFirst (before the actual task), doLast (after the actual task).
You just need to replace single quotes with double quotes.
clean.doFirst {
delete "${rootDir}/api-library/auto-generated-classes"
println "${rootDir}/api-library/auto-generated-classes"
}
Solution 7 - Gradle
Another solution for Kotlin DSL (build.gradle.kts):
tasks.clean {
delete += listOf(
"${rootDir}/logs.txt",
projectDir.list()?.first { it/* name */ .contains("temp") }
)
}
This deletes a file called logs.txt at the project root and deletes any directory/file that has temp
in its name in this subproject (the folder containing this build file).