如何在 gradle 构建中定义可重用块
How to define reusable blocks in gradle build
我正在为一个 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.
我正在为一个 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.