43

为了描述 Gradle 构建脚本,我们可以通过build.gradle.kts文件使用 Kotlin。在构建部分和构建部分中全局定义要使用Kotlin 版本是一个常见问题(在给定情况下使用不同版本是相当罕见的)。dependenciesplugin

考虑以下代码(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 version(在本例中为 1.2.30)定义了两次:dependencyVersionpluginVersion,它们通常没有区别。由于 DSL 限制,无法pluginVersion从块外plugins访问或dependencyVersion从块内访问plugins

如何将版本字符串"1.2.30"提取到一个地方?

4

6 回答 6

30

在 Gradle 的更高版本中,您不再需要指定kotlin(stdlib|reflect|test)依赖项的版本,Kotlin 插件会自动为您配置它们。

至于将依赖提取到一个地方,主要有两种模式:

  • 定义要在其中的对象中共享的常量buildSrc/src/main/kotlin/并在构建脚本中使用该对象,代码 frombuildSrc可用于整个脚本,包括plugins
  • 使用系统属性,您可以gradle.properties通过在其名称前加上前缀来定义系统属性,systemProp.并且您可以通过 访问系统属性System.getProperties(),例如:

    // build.gradle.kts
    plugins {
      val kotlinVersion by System.getProperties()
      println("Kotlin version is $kotlinVersion")
    }
    
    // gradle.properties
    systemProp.kotlinVersion=1.2.20
    
于 2018-01-18T11:41:42.227 回答
21

我刚刚偶然发现的是在我的build.gradle.kts.

我不得不:

  • 在其根目录中创建一个名为buildSrcwithsrc/main/kotlin和 a的模块。build.gradle.kts
  • (过时的)include("buildSrc")settings.gradle.kts

buildSrc/build.gradle.kts是非常小的:

plugins {
    `kotlin-dsl`
}

repositories {
    jcenter()
}

buildSrc/src/main/kotlin我添加了一个Config.kt

const val GROUP_ID = "my-company"
const val VERSION = "0.1.0-SNAPSHOT"

const val POM_NAME = "my-library-name"
const val POM_DESCRIPTION = "A library doing stuff."
const val POM_URL = "https://github.com/${GROUP_ID}/${POM_NAME}/"
const val POM_SCM_URL = POM_URL
const val POM_SCM_CONNECTION = "scm:git:git://github.com/${GROUP_ID}/${POM_NAME}.git"
const val POM_SCM_DEV_CONNECTION = "scm:git:ssh://git@github.com/${GROUP_ID}/${POM_NAME}.git"
const val POM_LICENCE_NAME = "The Apache Software License, Version 2.0"
const val POM_LICENCE_URL = "http://www.apache.org/licenses/LICENSE-2.0.txt"
const val POM_LICENCE_DIST = "repo"
const val POM_DEVELOPER_ID = "me"
const val POM_DEVELOPER_NAME = "meeee"
const val POM_DEVELOPER_EMAIL = "me@foo.com"

还有一个Dependencies.kt

@file:Suppress("MemberVisibilityCanBePrivate")

object Jvm {
    const val version = "1.8"
}

object Kotlin {
    const val version = "1.3.50"
    const val stdlibJdk8 = "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$version"
    const val jvmId = "jvm"
    const val kaptId = "kapt"
}

object MavenPublish {
    const val id = "maven-publish"
}

object Arrow {
    const val version = "0.10.1"
    const val core = "io.arrow-kt:arrow-core:$version"
    const val syntax = "io.arrow-kt:arrow-syntax:$version"
    const val optics = "io.arrow-kt:arrow-optics:$version"
    const val fx = "io.arrow-kt:arrow-fx:$version"
    const val meta = "io.arrow-kt:arrow-meta:$version"
}

object Versions {
    const val version = "0.27.0"
    const val versions = "com.github.ben-manes:gradle-versions-plugin:$version"
    const val id = "com.github.ben-manes.versions"
}

所以我可以在我的根目录中使用build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin(Kotlin.jvmId) version Kotlin.version
    kotlin(Kotlin.kaptId) version Kotlin.version
    id(Versions.id) version Versions.version
    id(MavenPublish.id)
}

group = GROUP_ID
version = VERSION

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    implementation(Kotlin.stdlibJdk8)
    implementation(Arrow.core)
    implementation(Arrow.syntax)
    kapt(Arrow.meta)
}

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = Jvm.version
}

publishing {
    publications {
        create<MavenPublication>("mavenJava") {
            @Suppress("UnstableApiUsage")
            pom {
                name.set(POM_NAME)
                description.set(POM_DESCRIPTION)
                url.set(POM_URL)
                licenses {
                    license {
                        name.set(POM_LICENCE_NAME)
                        url.set(POM_LICENCE_URL)
                        distribution.set(POM_LICENCE_DIST)
                    }
                }
                developers {
                    developer {
                        id.set(POM_DEVELOPER_ID)
                        name.set(POM_DEVELOPER_NAME)
                        email.set(POM_DEVELOPER_EMAIL)
                    }
                }
                scm {
                    connection.set(POM_SCM_CONNECTION)
                    developerConnection.set(POM_SCM_DEV_CONNECTION)
                    url.set(POM_SCM_URL)
                }
            }
        }
    }
}

我对此感到非常满意,但是当它自动增加时,version我可能会退回以将其维护在gradle.properties.


编辑: 不再需要(并且允许)添加buildSrcsettings.gradle.kts,相反,如果存在,它将自动被拾取。

于 2019-10-16T09:19:23.600 回答
7

您可以从插件类中提取版本:

import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper

plugins {
    kotlin("jvm") version "1.2.0"
}

val kotlinVersion = plugins.getPlugin(KotlinPluginWrapper::class.java).kotlinPluginVersion
于 2017-12-01T07:30:52.430 回答
2

有一种可用的解决方法,它搜索为kotlin 插件定义的版本并将其分配给外部变量。下面演示了这一点:

val kotlinVersion: String? by extra {
    buildscript.configurations["classpath"]
            .resolvedConfiguration.firstLevelModuleDependencies
            .find { it.moduleName == "kotlin-gradle-plugin" }?.moduleVersion
}

plugins {
    kotlin("jvm").version("1.2.30")
    //more
}

kotlinVersion然后可以在dependencies没有更多麻烦的情况下使用该变量。

于 2017-12-01T07:13:35.377 回答
2

回答@s1m0nw1 评论(评论太长):不,你不能在 buildSrc/build.gradle 中使用 buildSrc/src 的东西。我在编写基于 android 的插件时遇到了这个问题,我需要 buildsSrc 中的 android gradle 插件依赖项,但我也在项目中声明了这个依赖项。所以我有两个不同的地方和两个版本要维护。

我通过在 buildSrc 目录中创建 gradle.properties 文件解决了这个问题。在其中我创建了道具androidGradlePluginVersion=3.6.0-rc02

buildSrc/build.gradle:

val androidGradlePluginVersion: String by project
dependencies {
    implementation("com.android.tools.build:gradle:$androidGradlePluginVersion")

buildSrc/src/.../Versions.kt:

var ANDROID_PLUGIN = loadAndroidGradlePluginVersion()

用于道具:

val GRADLE_PROPERTIES = "buildSrc/gradle.properties"
val ANDROID_PLUGIN_VERSION_PROP = "androidGradlePluginVersion"

fun loadAndroidGradlePluginVersion(): String {
    Properties().apply { load(FileInputStream(GRADLE_PROPERTIES)) }.let {
        return it.getProperty(ANDROID_PLUGIN_VERSION_PROP)
    }

    error("Provide $ANDROID_PLUGIN_VERSION_PROP in $GRADLE_PROPERTIES")
} 
于 2020-02-05T08:13:33.410 回答
1

一旦您为 Kotlin 插件定义了一个版本,所有其他 Kotlin 库都将使用相同的版本,并且不需要特定的版本集。

因此,您需要设置版本的唯一地方是定义插件的类路径时,例如:

buildscript {
    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }

    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31")
    }
}

如果您随后出于其他目的需要该版本(例如在 a 中resolutionStrategy或仅用于信息),您可以从org.jetbrains.kotlin.config.KotlinCompilerVersion.VERSION

例如:

println("Kotlin version used is ${org.jetbrains.kotlin.config.KotlinCompilerVersion.VERSION}")
于 2021-07-25T03:21:56.870 回答