Han*_*tsy 26 gradle kotlin build.gradle gradle-kotlin-dsl
在这些日子里,我正在尝试编写一些代码来体验Spring 5中的Spring反应特性和kotlin扩展,并且我还准备了一个gradle Kotlin DSL build.gradle.kt来配置gradle构建.
它build.gradle.kt是由http://start.spring.io生成的Spring Boot模板代码转换而来.
但是ext在buildscriptGradle中无法检测到.
buildscript {
ext { }
}
Run Code Online (Sandbox Code Playgroud)
这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"
Run Code Online (Sandbox Code Playgroud)
但我必须在全球顶级位置声明它们并将其复制到buildscript.
代码:https://github.com/hantsy/spring-reactive-sample/blob/master/kotlin-gradle/build.gradle.kts
是否有优雅的ext工作方法?
更新:有一些丑陋的方法:
从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
Run Code Online (Sandbox Code Playgroud)
并在build.gradle.kts中使用它.
buildScript{
val kotlinVersion by project
}
val kotlinVersion by project //another declare out of buildscript block.
Run Code Online (Sandbox Code Playgroud)与上面类似,在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.
Run Code Online (Sandbox Code Playgroud)如何避免重复val kotlinVersion:String by extra?
Blu*_*ell 25
这些答案对我来说都不清楚。这是我的解释:
/build.gradle.kts:
buildscript {
extra.apply {
set("compose_version", "1.0.3")
}
...
}
Run Code Online (Sandbox Code Playgroud)
/app/build.gradle.kts:
val composeVersion = rootProject.extra["compose_version"]
implementation("androidx.compose.ui:ui:$composeVersion")
implementation("androidx.compose.material:material:$composeVersion")
Run Code Online (Sandbox Code Playgroud)
htt*_*tch 17
可以在.kt文件中使用文件中定义的常量.gradle.kts。
buildSrc在项目的根文件夹中创建文件夹
创建buildSrc/build.gradle.kts具有以下内容的文件
plugins {
`kotlin-dsl`
}
repositories {
mavenCentral()
}
Run Code Online (Sandbox Code Playgroud)
创建buildSrc/src/main/kotlin/Constants.kt具有以下内容的文件
object Constants {
const val kotlinVersion = "1.3.70"
const val targetSdkVersion = 28
}
Run Code Online (Sandbox Code Playgroud)
同步。现在您可以.gradle.kts像这样在各种文件中引用创建的常量
...
classpath(kotlin("gradle-plugin", version = Constants.kotlinVersion))
...
...
targetSdkVersion(Constants.targetSdkVersion)
...
Run Code Online (Sandbox Code Playgroud)
小智 15
随着Kotlin DSL ext的增加,它可以在buildscript下使用。
例如:-
buildscript {
// Define versions in a single place
extra.apply{
set("minSdkVersion", 26)
set("targetSdkVersion", 27)
}
}
Run Code Online (Sandbox Code Playgroud)
Per*_*den 10
我们可以使用 Kotlin 的新可能性:
object DependencyVersions {
const val JETTY_VERSION = "9.4.12.v20180830"
}
dependencies{
implementation("org.eclipse.jetty:jettyserver:${DependencyVersions.JETTY_VERSION}")
}
Run Code Online (Sandbox Code Playgroud)
在这里, DependencyVersions 是我选择的名称。您可以选择其他名称,例如“MyProjectVariables”。这是一种避免使用 extra 或 ext 属性的方法。
对我有用的是使用extin allprojects而不是buildscript,所以在你的顶级build.gradle.kts
allprojects {
ext {
set("supportLibraryVersion", "26.0.1")
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以build.gradle.kts在像这样的模块中的文件中使用它:
val supportLibraryVersion = ext.get("supportLibraryVersion") as String
Run Code Online (Sandbox Code Playgroud)
kotlin-gradle-dsl 中的全局属性:https ://stackoverflow.com/a/53594357/3557894
Kotlin 版本嵌入到 kotlin-gradle-dsl 中。
您可以使用具有嵌入式版本的依赖项,如下所示:
implementation(embeddedKotlin("stdlib-jdk7"))
classpath(embeddedKotlin("gradle-plugin"))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8001 次 |
| 最近记录: |