0

Groovy允许ext.

我想在 groovy 的额外属性中定义 Detekt 的版本。Detekt 是 Kotlin 语言的静态代码分析工具。

但是,当我按照以下方式进行操作时:

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/.*"
    }
}

它抱怨:

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
4

3 回答 3

2
于 2018-03-12T12:32:03.677 回答
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

于 2018-03-12T12:33:29.080 回答
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/.*"
    }
}
于 2018-03-12T13:07:55.687 回答