这基本上将this question扩展到Kotlin DSL,而不是Groovy DSL:
if (hasProperty('buildScan')) {
buildScan {
termsOfServiceUrl = 'https://gradle.com/terms-of-service'
termsOfServiceAgree = 'yes'
}
}翻译成Kotlin DSL?
我运行的问题是"buildScan“扩展或com.gradle.scan.plugin.BuildScanExtension类不能静态使用,因为它们是否存在取决于是否为Gradle提供了--scan命令行参数。
我试过了
if (hasProperty("buildScan")) {
extensions.configure("buildScan") {
termsOfServiceUrl = "https://gradle.com/terms-of-service"
termsOfServiceAgree = "yes"
}
}但是正如预期的那样,termsOfServiceUrl和termsOfServiceAgree不能解析,但是我不知道在这里使用什么语法。
发布于 2019-06-17 23:42:27
Gradle Kotlin DSL提供了一个withGroovyBuilder {}实用程序扩展,可以将Groovy元编程语义附加到任何对象。请参阅official documentation。
extensions.findByName("buildScan")?.withGroovyBuilder {
setProperty("termsOfServiceUrl", "https://gradle.com/terms-of-service")
setProperty("termsOfServiceAgree", "yes")
}这最终完成了反射,就像Groovy一样,但它使脚本更整洁一些。
发布于 2019-04-17 19:18:50
这并不是很好,但是使用反射它是有效的:
if (hasProperty("buildScan")) {
extensions.configure("buildScan") {
val setTermsOfServiceUrl = javaClass.getMethod("setTermsOfServiceUrl", String::class.java)
setTermsOfServiceUrl.invoke(this, "https://gradle.com/terms-of-service")
val setTermsOfServiceAgree = javaClass.getMethod("setTermsOfServiceAgree", String::class.java)
setTermsOfServiceAgree.invoke(this, "yes")
}
}https://stackoverflow.com/questions/55725574
复制相似问题