Gradle Kotlin DSL:在独特的地方定义Kotlin版本 [英] Gradle Kotlin DSL: Define Kotlin version in unique place
问题描述
为了描述Gradle构建脚本,我们可以通过 build.gradle.kts
文件使用Kotlin。在依赖关系
以及构建插件中全局定义要使用的 Kotlin版本是一个常见问题
section(在给定的例子中使用不同版本的情况并不常见)。
For describing Gradle build scripts, we can use Kotlin via build.gradle.kts
files. It's a common problem to globally define the Kotlin version to be used, both in the dependencies
and also in the build plugin
section (It's rather uncommon to have different versions in use for the given case).
考虑下面的代码(Gradle 4.3.1):
Consider the following code (Gradle 4.3.1):
plugins {
var pluginVersion = "1.2.30"
kotlin("jvm").version(kotlinVersion)
// more
}
var dependencyVersion = "1.2.30"
dependencies {
compile(kotlin("stdlib", kotlinVersion))
compile(kotlin("reflect", kotlinVersion))
testCompile(kotlin("test", kotlinVersion))
// more
}
正如您所看到的,kotlin 版本
(1.2.30 in this case)定义为两次: dependencyVersion
和 pluginVersion
,这通常是没有不同。由于DSL限制,无法从插件
块外部访问 pluginVersion
或访问>依赖版本
来自插件
块。
As you can see, the kotlin version
(1.2.30 in this case) is defined twice: dependencyVersion
and pluginVersion
, which very often does not differ. Due to DSL restrictions, it is impossible to access the pluginVersion
from outside the plugins
block or access the dependencyVersion
from within the plugins
block.
1.2.30
被提取到一个地方吗?
How can the version string, "1.2.30"
be extracted to a single place?
推荐答案
您可以从插件类中提取版本:
You can extract the version from the plugin class:
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
plugins {
kotlin("jvm") version "1.2.0"
}
val kotlinVersion = plugins.getPlugin(KotlinPluginWrapper::class.java).kotlinPluginVersion
这篇关于Gradle Kotlin DSL:在独特的地方定义Kotlin版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!