Groovy允许在ext
中为项目定义额外属性。
我想在groovy的额外属性中定义Detekt的版本。 Detekt是Kotlin lang的静态代码分析工具。
然而,当我以下列方式进行时:
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
答案 0 :(得分:2)
“新样式”Gradle插件定义(不包括对buildscript
块的完全依赖)不允许版本中的变量。
有关详细信息,请参阅错误消息中引用的文档部分。有一个小节“插件的限制dsl”解释了一切。
如果要继续使用变量版本字符串,则需要使用apply plugin: “xxx”
语法返回“旧方法”。
答案 1 :(得分:1)
您无法在plugins {}
:docs
其中«插件版本»和«插件ID»必须是常量,文字
这是一个开放的错误:Allow the plugin DSL to expand properties as the version
答案 2 :(得分:0)
正如@Strelok建议的最终解决方法(直到bug修复)是:
plugins
更改为apply plugin: "io.gitlab.arturbosch.detekt"
解决方案:
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/.*"
}
}