Gradle Kotlin DSL无法识别buildscript中的ext [英] ext in buildscript can not be recognised by Gradle Kotlin DSL
问题描述
这几天,我正在尝试编写一些代码来体验Spring 5中的Spring反应特性和kotlin扩展,并且我还准备了一个gradle Kotlin DSL build.gradle.kt来配置gradle构建.
In these days, I am trying to write some codes to experience the Spring reactive features and kotlin extension in Spring 5, and I also prepared a gradle Kotlin DSL build.gradle.kt to configure the gradle build.
build.gradle.kt
是从 http://start.spring.io 生成的Spring Boot模板代码转换而来的
The build.gradle.kt
is converted from Spring Boot template codes generated by http://start.spring.io.
但是Gradle无法检测到buildscript
中的ext
.
But the ext
in the buildscript
can not be detected by Gradle.
buildscript {
ext { }
}
ext
将导致Gradle构建错误.
The ext
will cause Gradle build error.
为了使classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
和compile("org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlinVersion")
中的变量起作用,我以困难的方式添加了变量.
To make the variables in classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
and compile("org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlinVersion")
work, I added the variables in the hard way.
val kotlinVersion = "1.1.4"
val springBootVersion = "2.0.0.M3"
但是我必须在全局顶部声明它们,并在buildscript
中重复它们.
But I have to declare them in global top location and duplicate them in the buildscript
.
代码: https://github.com/hantsy/spring-reactive-sample/blob/master/kotlin-gradle/build.gradle.kts
是否有一种使ext
工作正常的方法?
Is there a graceful approach to make ext
work?
更新:有一些丑陋的方法:
Update: There are some ugly approaches:
-
从Gradle Kotlin DSL示例中, https://github.com/gradle/kotlin-dsl/tree/master/samples/project-properties ,在gradel.properties中声明属性.
From Gradle Kotlin DSL example, https://github.com/gradle/kotlin-dsl/tree/master/samples/project-properties, declares the properties in gradel.properties.
kotlinVersion = 1.1.4
springBootVersion = 2.0.0.M3
并在build.gradle.kts中使用它.
And use it in build.gradle.kts.
buildScript{
val kotlinVersion by project
}
val kotlinVersion by project //another declare out of buildscript block.
与上述类似,在buildScript块中声明它们:
Similar with above declare them in buildScript block:
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:多余的字符串?
推荐答案
随着Kotlin DSL ext的增加,它可以在buildscript下使用.
With Kotlin DSL ext has been changed to extra and it can be used under buildscript.
例如:-
buildscript {
// Define versions in a single place
extra.apply{
set("minSdkVersion", 26)
set("targetSdkVersion", 27)
}
}
这篇关于Gradle Kotlin DSL无法识别buildscript中的ext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!