在 `plugins` DSL 中使用 ProjectDelegate `gradle`
Using ProjectDelegate `gradle` within the `plugins` DSL
如题,这个:
plugins {
if (gradle.gradleVersion < "6.0")
id("ru.vyarus.quality").version("4.3.0")
}
returns:
Unresolved reference: gradle
gradle
也被标记为错误,将鼠标悬停在上方我得到:
'val Build_gradle.gradle: Gradle' can't be called in this context by implicit receiver. Use the explicit one if necessary
谷歌搜索,我尝试指定接收者:
plugins {
if (this@Build_gradle.gradle.gradleVersion < "6.0")
id("ru.vyarus.quality").version("4.3.0")
}
但是它触发了:
Unresolved reference: getGradle
有办法吗? (除了为此声明一个外部变量)
来自 Limitations of the plugins DSL:
This requires that plugins be specified in a way that Gradle can easily and quickly extract, before executing the rest of the build script. It also requires that the definition of plugins to use be somewhat static.
The plugins {}
block does not support arbitrary code. It is constrained, in order to be idempotent (produce the same result every time) and side effect free (safe for Gradle to execute at any time).
事实上,您的构建脚本针对 plugins
块根据 CompiledKotlinPluginsBlock
进行评估,其余部分根据 CompiledKotlinBuildScript
进行评估。所以 this@Build_gradle
并不总是指同一个对象。您可以通过在不同的地方打印超类来看到这一点:
println(this@Build_gradle::class.java.superclass)
事实证明 CompiledKotlinPluginsBlock
没有 getGradle()
方法。
很高兴知道为什么会这样,但找到解决方案并不那么容易。您不能在 plugins {}
之外添加变量,因为它无法访问。一种方法是在您的 buildSrc
文件夹中有一个静态字段,但只为此需要它会很奇怪。
这是我发现有用的东西。在你的根 build.gradle.kts
添加:
plugins {
id("ru.vyarus.quality") version "4.3.0" apply false
}
project(":yourModule") {
if (gradle.gradleVersion < "6.0") {
apply(plugin = "ru.vyarus.qualit")
}
}
我希望它不需要旧的 apply(plugin =
方式,但这是我找到的唯一解决方案。您的模块 plugins {}
.
中不需要任何特殊内容
如题,这个:
plugins {
if (gradle.gradleVersion < "6.0")
id("ru.vyarus.quality").version("4.3.0")
}
returns:
Unresolved reference: gradle
gradle
也被标记为错误,将鼠标悬停在上方我得到:
'val Build_gradle.gradle: Gradle' can't be called in this context by implicit receiver. Use the explicit one if necessary
谷歌搜索,我尝试指定接收者:
plugins {
if (this@Build_gradle.gradle.gradleVersion < "6.0")
id("ru.vyarus.quality").version("4.3.0")
}
但是它触发了:
Unresolved reference: getGradle
有办法吗? (除了为此声明一个外部变量)
来自 Limitations of the plugins DSL:
This requires that plugins be specified in a way that Gradle can easily and quickly extract, before executing the rest of the build script. It also requires that the definition of plugins to use be somewhat static.
The
plugins {}
block does not support arbitrary code. It is constrained, in order to be idempotent (produce the same result every time) and side effect free (safe for Gradle to execute at any time).
事实上,您的构建脚本针对 plugins
块根据 CompiledKotlinPluginsBlock
进行评估,其余部分根据 CompiledKotlinBuildScript
进行评估。所以 this@Build_gradle
并不总是指同一个对象。您可以通过在不同的地方打印超类来看到这一点:
println(this@Build_gradle::class.java.superclass)
事实证明 CompiledKotlinPluginsBlock
没有 getGradle()
方法。
很高兴知道为什么会这样,但找到解决方案并不那么容易。您不能在 plugins {}
之外添加变量,因为它无法访问。一种方法是在您的 buildSrc
文件夹中有一个静态字段,但只为此需要它会很奇怪。
这是我发现有用的东西。在你的根 build.gradle.kts
添加:
plugins {
id("ru.vyarus.quality") version "4.3.0" apply false
}
project(":yourModule") {
if (gradle.gradleVersion < "6.0") {
apply(plugin = "ru.vyarus.qualit")
}
}
我希望它不需要旧的 apply(plugin =
方式,但这是我找到的唯一解决方案。您的模块 plugins {}
.