59

这几天在尝试写一些代码来体验Spring 5中的Spring响应式特性和kotlin扩展,还准备了一个gradle Kotlin DSL build.gradle.kt来配置gradle build。

build.gradle.kthttp://start.spring.io生成的Spring Boot 模板代码转换而来。

但是Gradle无法检测到extbuildscript

buildscript {
  ext { }
}

ext将导致 Gradle 构建错误。

为了使变量起作用classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")compile("org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlinVersion")起作用,我以艰难的方式添加了变量。

val kotlinVersion = "1.1.4"
val springBootVersion = "2.0.0.M3"

但我必须在全局顶部位置声明它们并将它们复制到buildscript.

代码:https ://github.com/hantsy/spring-reactive-sample/blob/master/kotlin-gradle/build.gradle.kts

有没有一种优雅的ext工作方式?

更新:有一些丑陋的方法:

  1. 在 Gradle Kotlin DSL 示例中,https://github.com/gradle/kotlin-dsl/tree/master/samples/project-properties声明了gradel.properties 中的属性。

    kotlinVersion = 1.1.4
    springBootVersion = 2.0.0.M3
    

    并在 build.gradle.kts 中使用它。

    buildScript{
       val kotlinVersion by project
    
    }
     val kotlinVersion by project //another declare out of buildscript block.
    
  2. 与上面类似,在 buildScript 块中声明它们:

    buildScript{
       extra["kotlinVersion"] = "1.1.4"
       extra["springBootVersion"] = "2.0.0.M3"
       val kotlinVersion: String by extra
    
    }
     val kotlinVersion: String by extra//another declare out of buildscript block.
    

如何避免val kotlinVersion: String by extra的重复?

4

8 回答 8

39

Kotlin DSL ext 已更改为 extra,可以在 buildscript 下使用。

例如:-

buildscript {
    // Define versions in a single place
    extra.apply{
        set("minSdkVersion", 26)
        set("targetSdkVersion", 27)
    }
}
于 2018-12-26T04:20:43.670 回答
21

可以在.kt文件中使用文件中定义的常量.gradle.kts

buildSrc在项目的根文件夹中创建文件夹

创建buildSrc/build.gradle.kts具有以下内容的文件

plugins {
    `kotlin-dsl`
}

repositories {
    mavenCentral()
}

创建buildSrc/src/main/kotlin/Constants.kt具有以下内容的文件

object Constants {
    const val kotlinVersion = "1.3.70"
    const val targetSdkVersion = 28
}

同步。.gradle.kts现在您可以像这样在各种文件中引用创建的常量

...
classpath(kotlin("gradle-plugin", version = Constants.kotlinVersion))
...

...
targetSdkVersion(Constants.targetSdkVersion)
...
于 2020-06-15T12:03:04.773 回答
18

对我有用的是使用extinallprojects而不是buildscript, 所以在你的顶层build.gradle.kts

allprojects {
  ext {
    set("supportLibraryVersion", "26.0.1")
  }
}

然后您可以build.gradle.kts在模块中的文件中使用它,如下所示:

val supportLibraryVersion = ext.get("supportLibraryVersion") as String
于 2017-10-04T05:21:52.543 回答
10

Kotlin 有一种新的可能性,我们可以使用:

object DependencyVersions {
    const val JETTY_VERSION = "9.4.12.v20180830"
}

dependencies{
    implementation("org.eclipse.jetty:jettyserver:${DependencyVersions.JETTY_VERSION}")
}

在这里,DependencyVersions 是我选择的名称。您可以选择其他名称,例如“MyProjectVariables”。这是一种避免使用 extra 或 ext 属性的方法。

于 2019-06-27T16:17:51.827 回答
6

kotlin-gradle-dsl 中的全局属性:
https ://stackoverflow.com/a/53594357/3557894


Kotlin 版本嵌入到 kotlin-gradle-dsl 中。
您可以使用嵌入式版本的依赖项,如下所示:

implementation(embeddedKotlin("stdlib-jdk7"))

classpath(embeddedKotlin("gradle-plugin"))
于 2018-12-03T13:38:27.257 回答
4

这些答案对我来说都不是很清楚。所以这是我的解释:

/build.gradle.kts

buildscript {
    extra.apply {
        set("compose_version", "1.0.3")
    }
    ...
}

/app/build.gradle.kts

val composeVersion = rootProject.extra["compose_version"]
implementation("androidx.compose.ui:ui:$composeVersion")
implementation("androidx.compose.material:material:$composeVersion")
于 2021-10-13T19:49:05.377 回答
2
val junitVersion by extra("4.13.2")
testImplementation("junit:junit:$junitVersion")
于 2021-12-10T10:04:49.217 回答
0

可以在 gradle.properties 中定义全局属性:

xyzVersion=1.0.0

然后在模块的 build.gradle.kts 中使用它们:

val xyzVersion: String by project
于 2021-09-18T23:51:59.650 回答