如何自动接受 `build --scan` 的 Gradle ToS,并且仍然设法在没有扫描的情况下进行 运行 构建?

How to accept Gradle ToS for `build --scan` automatically and still manage to run build without a scan?

I am using Gradle 4.6 which allows me to run build scans using the --scan option without having to explicitly apply or download extra plugins which is great. However this forces me to add the buildScan Terms of Service acceptance in my build.gradle file.

像这样:

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

When I subsequently run gradle build without the --scan option I get the following error message:

> Could not find method buildScan() for arguments…

I don’t want to have to modify the build.gradle file every time I want/dont want a scan. I don’t want to apply the plugin explicitly (firewall issues) and I don’t get the chance to accept the of Terms of Service on the command line which I have also seen documented.

Can anyone tell me what I am doing wrong?

这个问题的格式类似于引述,因为它已经在 Gradle Forums 上被问过。但是没有答案。我正在使用 Gradle 4.10.2,但问题仍然存在。我决定在这里提请更多人注意这个问题。

这可能不是一个好的或适当的解决方案,但这里有一个解决方法:使用 try/catch 来消除错误,例如:

try {
    buildScan {
        termsOfServiceUrl = 'https://gradle.com/terms-of-service'
        termsOfServiceAgree = 'yes'
    }
} catch (MissingMethodException e){
   // This isn't the exception you're looking for
}

只是测试 buildScan

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

你只需要在buid.gradle(Project)文件中添加上面的代码块 并且构建扫描将自动发布。

在 Jenkins 中,您将 link 包含构建扫描的结果。