Search code examples
antkotlinpatchgradle-kotlin-dsl

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?


Solution

  • 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.