如何从我的build.gradle.kts
脚本访问 ant 任务?特别是,我对这项ant.patch
任务很感兴趣。
我可以像这样扩展它吗?
task("patchSources", Patch::class) {
我可以像这样从其他任务中调用它吗?
task("patchSources") {
doLast {
ant.patch(...)
}
}
我知道如何在 Groovy 中执行此操作:如何在 Gradle 中应用补丁文件?
如何从我的build.gradle.kts
脚本访问 ant 任务?特别是,我对这项ant.patch
任务很感兴趣。
我可以像这样扩展它吗?
task("patchSources", Patch::class) {
我可以像这样从其他任务中调用它吗?
task("patchSources") {
doLast {
ant.patch(...)
}
}
我知道如何在 Groovy 中执行此操作:如何在 Gradle 中应用补丁文件?
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
))
}
}
这对我有用:
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()
}
}
}
我不确定这是否是正确的做法。