17

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.

like this:

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?

This question is formatted like a quote because it was already asked on Gradle Forums. But it's left without answer. I'm using Gradle 4.10.2 and the problem is still actual. I decided to draw more attention to this problem here.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259

2 Answers2

32

Just test the existence of buildScan

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

The same can be done in a kotlin build file like this - (with thanks to this answer):

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

Please see Connecting to scans.gradle.com documentation for more detail.

arberg
  • 4,148
  • 4
  • 31
  • 39
ToYonos
  • 16,469
  • 2
  • 54
  • 70
  • 1
    If anyone knows how to translate this to Kotlin DSL please answer at https://stackoverflow.com/q/55725574/1127485, thanks in advance. – sschuberth Apr 17 '19 at 10:35
  • 1
    What section is this supposed to go within build.gradle (app)? – Taslim Oseni Jun 01 '21 at 12:23
  • 1
    @TaslimOseni put it in the top level build.gradle (not the one with dependencies). Put it just above `buildscript` – Chad Bingham Oct 25 '21 at 17:03
  • If you don’t have a top-level `build.gradle` yet, you should create it. Oh, and be sure [not to commit agreement to the terms of service into a project that may be built by others](https://docs.gradle.com/enterprise/gradle-plugin/#connecting_to_scans_gradle_com). – Julia Sep 24 '22 at 19:13
0

This might not be a good or proper solution, but here's one workaround: use a try/catch to swallow the error, something like:

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