在buildscript中的分机不能被Gradle Kotlin DSL识别

在这些日子里,我正在尝试编写一些代码来体验Spring 5中的Spring反应特性和kotlin扩展,并且我还准备了一个Gradle Kotlin DSL build.gradle.kt来配置gradle build。

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

buildscriptext不能被Gradle检测到。

 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

什么是我的工作是在allprojects使用ext而不是buildscript ,所以在你的顶层build.gradle.kts

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

那么你可以在像这样的模块build.gradle.kts文件中使用它:

 val supportLibraryVersion = ext.get("supportLibraryVersion") as String