我正在为一个 kotlin 项目编写一个 gradle 构建,我想在其中重用相同的 kotlinOptions
在多项任务中。
目前我的构建脚本看起来像这样,如 kotlinOptions
每个任务都是一样的,我不想一遍又一遍地写它们。
compileKotlin {
kotlinOptions {
allWarningsAsErrors = true
freeCompilerArgs = ["-Xjvm-default=enable", "-Xjsr305=strict"]
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
allWarningsAsErrors = true
freeCompilerArgs = ["-Xjvm-default=enable", "-Xjsr305=strict"]
jvmTarget = "1.8"
}
}
compileIntegrationTestKotlin {
kotlinOptions {
allWarningsAsErrors = true
freeCompilerArgs = ["-Xjvm-default=enable", "-Xjsr305=strict"]
jvmTarget = "1.8"
}
}
相反,我想定义它们一次并在我需要的任何地方重用该定义。
我还尝试了以下操作(如 Alexs answer 中所建议)
ext.optionNameHere = {
allWarningsAsErrors = true
freeCompilerArgs = ["-Xjvm-default=enable", "-Xjsr305=strict"]
jvmTarget = "1.8"
}
compileKotlin { kotlinOptions = ext.optionNameHere }
compileTestKotlin { kotlinOptions = ext.optionNameHere }
compileIntegrationTestKotlin { kotlinOptions = ext.optionNameHere }
这导致以下错误消息:
> Cannot get property 'kotlinOptions' on extra properties extension as it does not exist
最佳答案
我为我的特定问题找到了解决方案(仅适用于 kotlin 编译部分)。
我希望有一个更通用的方法。虽然这可能会帮助其他人。
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
// ...
}
}
来自 kotlin docs .
关于gradle - 如何在 gradle build 中定义可重用的 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50932802/