3

如何从我的build.gradle.kts脚本访问 ant 任务?特别是,我对这项ant.patch任务很感兴趣。

我可以像这样扩展它吗?

task("patchSources", Patch::class) {

我可以像这样从其他任务中调用它吗?

task("patchSources") {
    doLast {
        ant.patch(...)
    }
}

我知道如何在 Groovy 中执行此操作:如何在 Gradle 中应用补丁文件?

4

2 回答 2

3

AntBuilder从 Groovy 的AntBuilder. 您可以将动态方法调用从 groovy likeant.patch()转换为 Kotlin,方法是使用invokeMethod并提供所需的任务作为第一个参数,并将要绑定的属性作为第二个参数中的映射。

例如,对于您的Patch用例(可用的属性文档),Kotlin 可能如下所示:

val patchSources by tasks.creating {
  doLast {
    ant.invokeMethod("patch", mapOf(
        "patchfile" to patchFile,
        "dir" to configDir,
        "strip" to 1
    ))
  }
}
于 2017-12-15T14:23:24.063 回答
2

这对我有用:

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()
        }
    }
}

我不确定这是否是正确的做法。

于 2016-11-25T10:55:15.037 回答