本文介绍了如何在Kotlin DSL中有条件地接受Gradle构建扫描插件服务条款?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这基本上将此问题扩展到Kotlin DSL,而不是Groovy DSL:

This basically extends this question to Kotlin DSL instead of Groovy DSL:

Groovy DSL解决方案

if (hasProperty('buildScan')) {
    buildScan {
        termsOfServiceUrl = 'https://gradle.com/terms-of-service'
        termsOfServiceAgree = 'yes'
    }
}

要翻译成Kotlin DSL吗?

translate to Kotlin DSL?

我正在运行的问题是"buildScan"扩展名或com.gradle.scan.plugin.BuildScanExtension类无法静态使用,因为它们是否存在,取决于是否向Gradle提供了--scan命令行参数.

The problem I'm running is that the "buildScan" extension or the com.gradle.scan.plugin.BuildScanExtension class cannot statically be used as they are either present or not present depending on whether the --scan command line argument was provided to Gradle or not.

我尝试过

if (hasProperty("buildScan")) {
    extensions.configure("buildScan") {
        termsOfServiceUrl = "https://gradle.com/terms-of-service"
        termsOfServiceAgree = "yes"
    }
}

但是按预期termsOfServiceUrltermsOfServiceAgree不能解析,但是我不知道在这里使用什么语法.

but as expected termsOfServiceUrl and termsOfServiceAgree do not resolve, however I'm clueless what syntax to use here.

推荐答案

Gradle Kotlin DSL提供了withGroovyBuilder {}实用程序扩展,该扩展将Groovy元编程语义附加到任何对象.请参见官方文档.

The Gradle Kotlin DSL provides a withGroovyBuilder {} utility extension that attaches the Groovy metaprogramming semantics to any object. See the official documentation.

extensions.findByName("buildScan")?.withGroovyBuilder {
  setProperty("termsOfServiceUrl", "https://gradle.com/terms-of-service")
  setProperty("termsOfServiceAgree", "yes")
}

这最终像Groovy一样进行了反射,但是使脚本更加整洁.

This ends up doing reflection, just like Groovy, but it keeps the script a bit more tidy.

这篇关于如何在Kotlin DSL中有条件地接受Gradle构建扫描插件服务条款?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 04:54