How to conditionally accept Gradle build scan plugin terms of service in Kotlin DSL?
Asked Answered
C

3

9

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

How does the Groovy DSL solution of

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

translate to Kotlin DSL?

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.

I've tried

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

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

Cedrickceevah answered 17/4, 2019 at 10:34 Comment(0)
U
10

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

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

Usually answered 17/6, 2019 at 15:42 Comment(0)
C
2

It's not exactly nice, but using reflection it works:

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")
    }
}
Cedrickceevah answered 17/4, 2019 at 11:18 Comment(0)
S
1

Updated on 2024/06/19:

After my confirmation, this is the better solution.

// add to settings.gradle.kts

plugins {
    id("com.gradle.develocity") version "3.17.5"
}
develocity {
    buildScan {
        termsOfUseUrl = "https://gradle.com/help/legal-terms-of-use"
        termsOfUseAgree = "yes"
        
        // publishing.onlyIf { false }
      // If you don't need to auto-publish the build scan, uncomment it.
       
    }
}

Or use this

Updated on 2024/06/16:

extensions.findByName("develocity")?.withGroovyBuilder {
    getProperty("buildScan")?.withGroovyBuilder {
        setProperty("termsOfUseUrl", "https://gradle.com/help/legal-terms-of-use")
        setProperty("termsOfUseAgree", "yes")
    }
}

WARNING: The following functionality has been deprecated and will be removed in the next major release of the Develocity Gradle plugin. Run with '-Ddevelocity.deprecation.captureOrigin=true' to see where the deprecated functionality is being used. For assistance with migration, see https://gradle.com/help/gradle-plugin-develocity-migration.

  • The deprecated "gradleEnterprise.buildScan.termsOfServiceUrl" API has been replaced by "develocity.buildScan.termsOfUseUrl"
  • The deprecated "gradleEnterprise.buildScan.termsOfServiceAgree" API has been replaced by "develocity.buildScan.termsOfUseAgree"
Syringe answered 16/6, 2024 at 8:37 Comment(6)
Does not work for me (presumably because buildScan is not a property but a nested extension on develocity).Cedrickceevah
@Cedrickceevah I'm using Gradle 8.8Syringe
I've tested with Gradle 8.8 as well.Cedrickceevah
@Cedrickceevah github.com/lalakii/sample/blob/master/build.gradle.ktsSyringe
@Cedrickceevah This is my sample, .\gradlew --scan on windows. I confirm that it works in the terminal.Syringe
My bad, I overlooked that in the property names also "Service" needs to be replaced with "Use". Now it works, thanks!Cedrickceevah

© 2022 - 2025 — McMap. All rights reserved.