Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ant tasks from gradle-script-kotlin

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?

like image 480
user7610 Avatar asked May 12 '26 22:05

user7610


2 Answers

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
    ))
  }
}
like image 110
mkobit Avatar answered May 15 '26 12:05

mkobit


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.

like image 35
user7610 Avatar answered May 15 '26 10:05

user7610



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!