Gradle Kotlin DSL:在独特的地方定义Kotlin版本 [英] Gradle Kotlin DSL: Define Kotlin version in unique place

查看:426
本文介绍了Gradle Kotlin DSL:在独特的地方定义Kotlin版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了描述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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆