How can I access ant tasks from my build.gradle.kts script? In particular, I am interested in the ant.patch task.
Can I extend it, like so?
task("patchSources", Patch::class) {
Can I invoke it from other task, like this?
task("patchSources") {
doLast {
ant.patch(...)
}
}
I know how to do it in Groovy: How do I apply a patch file in Gradle?
AntBuilder extends from Groovy's AntBuilder. You can translate the dynamic method invocations from groovy like ant.patch() to Kotlin by using invokeMethod and providing the desired task as the first argument and the properties to bind as a map in the second argument.
For example, for your Patch use case (available properties documentation) the Kotlin could look like this:
val patchSources by tasks.creating {
doLast {
ant.invokeMethod("patch", mapOf(
"patchfile" to patchFile,
"dir" to configDir,
"strip" to 1
))
}
}
This works for me:
import org.apache.tools.ant.taskdefs.Patch
val patchConfigTask = task("patchConfig") {
dependsOn(unzipTask)
doLast {
val resources = projectDir.resolve("src/main/resources")
val patchFile = resources.resolve("config.patch")
Patch().apply {
setPatchfile(patchFile)
setDir(buildDir.resolve("config/"))
setStrip(1) // gets rid of the a/ b/ prefixes
execute()
}
}
}
I am not sure if it's the one-right-way-to-do-it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With