0

Groovy allows definition of extra properties for the project in ext.

I wanted to define Detekt's version inside groovy's extra properties. Detekt is a static code analysis tool for Kotlin lang.

However when I do it in the folloing way:

buildscript {
    // testing, code-style, CI-tools
    ext.detect_code_analysis = '1.0.0.RC6-3' //change to 1.0.0 when available

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$gradle_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}
plugins {
    id "io.gitlab.arturbosch.detekt" version "$detect_code_analysis"
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

detekt {
    version = "$detect_code_analysis"
    profile("main") {
        input = "$projectDir/app/src/main/java"
        config = "$projectDir/detekt-config.yml"
        filters = ".*test.*,.*/resources/.*,.*/tmp/.*"
    }
}

it complaing with:

Error:(17, 0) startup failed:
        build file '/Users[...]build.gradle': 17: argument list must be exactly 1 literal non empty string

See https://docs.gradle.org/4.1/userguide/plugins.html#sec:plugins_block for information on the plugins {} block

@ line 17, column 5.
        id "io.gitlab.arturbosch.detekt" version "$detect_code_analysis"
^

1 error

3 Answers3

2

“New style” Gradle plug-in definition (without including the full dependency on buildscript block) does not allow variables in the version.

For more information refer to the section of documentation that’s referred to in the error message. There is a subsection “Limitations of the plugins dsl” that explains everything.

If you want to continue using the variable version strings you need to return to the “old way” by using the apply plugin: “xxx” syntax.

Strelok
  • 50,229
  • 9
  • 102
  • 115
  • yes, thanks - it works as expected. I needed to add classpath in buildscript.dependecies, and change `plugins` to `apply plugin: "io.gitlab.arturbosch.detekt"` – kosiara - Bartosz Kosarzycki Mar 12 '18 at 13:02
  • link: https://docs.gradle.org/4.6/userguide/plugins.html#plugins_dsl_limitations Explains the limitations and WHY they exist. – Mikezx6r Mar 13 '18 at 02:16
1

You cannot use variables in plugins {}: docs

Where «plugin version» and «plugin id» must be constant, literal

This is an open bug: Allow the plugin DSL to expand properties as the version

Bruno Wieczorek
  • 524
  • 1
  • 4
  • 8
  • OK, let's hope it gets fixed soon – kosiara - Bartosz Kosarzycki Mar 12 '18 at 12:51
  • 1
    Strictly speaking it's not a bug, but an intentional design decision. In order to support faster builds, having the plugins section be hard-coded supports that. If there's a variable involved, Gradle has to do much more work/compiling to determine if the build has changed, and do work for that. They are investigating ways of maintaining the performance while providing variables, but thus far... – Mikezx6r Mar 13 '18 at 02:11
  • 1
    link: https://docs.gradle.org/4.6/userguide/plugins.html#plugins_dsl_limitations Explains the limitations and WHY they exist. – Mikezx6r Mar 13 '18 at 02:17
0

Just as @Strelok suggested the final workaround (until bug is fixed) is to:

  • add a classpath in buildscript.dependecies
  • change plugins to apply plugin: "io.gitlab.arturbosch.detekt"

Solution:

buildscript {

    // testing, code-style, CI-tools
    ext.detect_code_analysis = '1.0.0.RC6-3' //change to 1.0.0 when available

    repositories {
        google()
        jcenter()
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$gradle_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "gradle.plugin.io.gitlab.arturbosch.detekt:detekt-gradle-plugin:$detect_code_analysis"
    }
}

apply plugin: "io.gitlab.arturbosch.detekt"

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

detekt {
    version = "$detect_code_analysis"
    profile("main") {
        input = "$projectDir/app/src/main/java"
        config = "$projectDir/detekt-config.yml"
        filters = ".*test.*,.*/resources/.*,.*/tmp/.*"
    }
}