Man*_*gor 4 java gradle kotlin gradle-kotlin-dsl
我正在尝试在具有以下限制的项目中开始使用Gradle的Kotlin DSL:
Groovy提供的功能:
结果(仅基于我的示例):
我尝试使用Gralde KTS复制相同内容,但遇到以下困难:
build.gradle.kts包括根)中使用它们的共同点。使用Groovy,我只能使用like之类的变量springBootVersion,但是使用Kotlin Script,我必须在每个模块文件中创建相同的属性。包含文件样本:
apply plugin: 'kotlin'
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
Run Code Online (Sandbox Code Playgroud)
Gradle模块样本:
apply from: "${rootDir}/gradle/include/kotlin-common-include.gradle"
dependencies {
compile project(':my.project.libraries.common')
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion
}
Run Code Online (Sandbox Code Playgroud)
问题:
springBootVersion或Constants.springBootVersion通过编译时检查将它们包括在内?附加链接:
当前(5.3版)的Kotlin DSL中存在一些限制,这些限制阻止了到处进行编译时检查。为了使Kotlin DSL正常工作,它必须在Gradle API之上添加扩展,并且不能神奇地做到这一点。首先,您需要阅读Kotlin DSL Primer页面以了解它。
如何提取适用于包含脚本的插件(以避免Gradle模块脚本过载)?
一种方法是将预编译的构建脚本与Kotlin DSL插件一起使用。为此,您需要将脚本移到$rootDir/buildSrc项目中。这里看起来可能像这样:
// $rootDir/buildSrc/build.gradle.kts
plugins {
`kotlin-dsl`
}
repositories {
maven {
url = uri("http://host:8082/artifactory/...")
}
}
Run Code Online (Sandbox Code Playgroud)
然后像这样放置普通脚本:
// $rootDir/buildSrc/src/main/kotlin/common.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.3.21"
}
tasks.compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
tasks.compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以将此脚本应用于这样的插件:
// $rootDir/build.gradle.kts
subprojects {
apply(id = "common")
}
Run Code Online (Sandbox Code Playgroud)
如何使用没有公共全局存储库访问权限的预编译脚本插件?
为预编译脚本插件配置自定义存储库与通常的构建脚本没有什么不同。那样做:
// $rootDir/buildSrc/settings.gradle.kts
pluginManagement {
repositories.maven {
url = uri("http://host:8082/artifactory/...")
}
}
Run Code Online (Sandbox Code Playgroud)
如果您不想使用预编译的插件,另一种解决方法是显式配置扩展。您可以这样做:
// $rootDir/gradle/common.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
Run Code Online (Sandbox Code Playgroud)
在主脚本中:
// $rootDir/build.gradle.kts
subprojects {
apply {
plugin(KotlinPlatformJvmPlugin::class)
from("common.gradle.kts")
}
}
Run Code Online (Sandbox Code Playgroud)
如何仅通过使用springBootVersion或Constants.springBootVersion之类的东西进行编译时检查,将所有公共常数(例如依赖项版本)放入单独的文件中,以将它们包括在内?
目前尚无好的方法。您可以使用其他属性,但不能保证编译时检查。像这样:
// $rootDir/dependencies.gradle.kts
// this will try to take configuration from existing ones
val compile by configurations
val api by configurations
dependencies {
compile("commons-io:commons-io:1.2.3")
api("some.dep")
}
// This will put your version into extra extension
extra["springBootVersion"] = "1.2.3"
Run Code Online (Sandbox Code Playgroud)
您可以像这样使用它:
// $rootDir/build.gradle.kts
subprojects {
apply {
plugin<JavaLibraryPlugin>()
from("$rootDir/dependencies.gradle.kts")
}
Run Code Online (Sandbox Code Playgroud)
在您的模块中:
// $rootDir/module/build.gradle.kts
// This will take existing dependency from extra
val springBootVersion: String by extra
dependencies {
compile("org.spring:boot:$springBootVersion")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1197 次 |
| 最近记录: |