使用gradle-script-kotlin的ant任务

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

我可以像这样扩展它吗?

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

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

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

我知道如何在Groovy中做到这一点: 如何在Gradle中应用补丁文件?

这适用于我:

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

我不确定这是否是唯一的方法。

AntBuilder从Groovy的AntBuilder扩展。 您可以使用invokeMethod将groovy(如ant.patch()的动态方法调用翻译为Kotlin,并将所需的任务作为第一个参数提供,并将属性作为第二个参数中的映射进行绑定。

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

 val patchSources by tasks.creating { doLast { ant.invokeMethod("patch", mapOf( "patchfile" to patchFile, "dir" to configDir, "strip" to 1 )) } }