如何避免在 gradle 中复制粘贴?
How to avoid copy'n'paste in gradle?
我的 spring-boot 应用程序有一个 build.gradle 文件。我有一些环境细节想为某些 gradle 任务更改。专门针对 gradle 任务 'test'、'runSmokeTest' 和 'bootRun'。在所有任务中,我都必须进行相同的调用,所以我更愿意从中提取一个方法。或者一个任务。但每当我这样做时,突然 gradle 不再找到我需要的功能。
这些是我需要拨打的电话:
systemProperties System.properties
systemProperty "spring.cloud.config.failFast", "false"
if (project.hasProperty("TEAM_ENCRYPT_KEY"))
environment "ENCRYPT_KEY", "$TEAM_ENCRYPT_KEY"
当通过复制粘贴直接包含在 bootRun
任务、test
任务和 runSmokeTest
任务中时,代码工作得很好。我不想复制代码。我尝试了以下方法从bootRun任务中提取它们,但是Gradle一直抱怨他没有找到函数systemProperty
和environment
。同样,如果我使用 Intellij 集成功能 'extract Method':
task specialConfiguration() {
systemProperties System.properties
systemProperty "spring.cloud.config.failFast", "false"
if (project.hasProperty("TEAM_ENCRYPT_KEY"))
environment "ENCRYPT_KEY", "$TEAM_ENCRYPT_KEY"
}
bootRun {
dependsOn 'specialConfiguration'
}
如何从 3 个任务中提取出这么短的代码来避免重复代码?
Gradle keeps complaining that he does not find the functions systemProperty
and environment
这是 Kotlin DSL 大放异彩的典型例子。您会在任何给定时间确切地知道什么 methods/properties 可用,因为它是一种强类型语言,与 Groovy.
不同
话虽如此,当您执行以下操作时:
task specialConfiguration() {
systemProperties System.properties
systemProperty "spring.cloud.config.failFast", "false"
if (project.hasProperty("TEAM_ENCRYPT_KEY"))
environment "ENCRYPT_KEY", "$TEAM_ENCRYPT_KEY"
}
bootRun {
dependsOn 'specialConfiguration'
}
你是:
- 正在声明一个名为
specialConfiguration
的任务。
- 未指定类型,因此类型为 DefaultTask。
- 配置
bootRun
任务依赖于 specialConfiguration
我认为您假设 dependsOn
就像 "configuring" 一项任务,而实际上它只是向任务添加依赖项。参见 Adding dependencies to a task。
我假设 runSmokeTest
是 Test
. So tasks test
, runSmokeTest
, and bootRun
all implement the JavaForkOptions 接口类型,这是 systemProperties(..)
、systemProperty(.., ..)
和 environment(.., ..)
方法的来源。
话虽如此,由于您知道要配置的三个任务,并且它们都实现了 JavaForkOptions
并且以某种方式,您可以执行 (Kotlin DSL):
import org.springframework.boot.gradle.tasks.run.BootRun
// Assuming this is a Test task type
tasks.register("runSmokeTest", Test::class)
// Define a new Action (configuration)
val taskConfig = Action<JavaForkOptions> {
systemProperties(System.getProperties() as Map<String, Any>)
systemProperty("spring.cloud.config.failFast", false)
if (project.hasProperty("TEAM_ENCRYPT_KEY")) {
environment("ENCRYPT_KEY", project.property("TEAM_ENCRYPT_KEY")!!)
}
}
// Configure all three tasks
tasks.named("test", Test::class, taskConfig)
tasks.named("runSmokeTest", Test::class, taskConfig)
tasks.named("bootRun", BootRun::class, taskConfig)
Francisco Mateo 回答的 Groovy 版本,以防有人需要。:
Closure<JavaForkOptions> configAction = {
systemProperties System.properties
systemProperty "spring.cloud.config.failFast", "false"
if (project.hasProperty("MOBTECH_ENCRYPT_KEY"))
it.environment "ENCRYPT_KEY", "$MOBTECH_ENCRYPT_KEY"
}
# Configure the tasks with it
bootRun (configAction)
我的 spring-boot 应用程序有一个 build.gradle 文件。我有一些环境细节想为某些 gradle 任务更改。专门针对 gradle 任务 'test'、'runSmokeTest' 和 'bootRun'。在所有任务中,我都必须进行相同的调用,所以我更愿意从中提取一个方法。或者一个任务。但每当我这样做时,突然 gradle 不再找到我需要的功能。
这些是我需要拨打的电话:
systemProperties System.properties
systemProperty "spring.cloud.config.failFast", "false"
if (project.hasProperty("TEAM_ENCRYPT_KEY"))
environment "ENCRYPT_KEY", "$TEAM_ENCRYPT_KEY"
当通过复制粘贴直接包含在 bootRun
任务、test
任务和 runSmokeTest
任务中时,代码工作得很好。我不想复制代码。我尝试了以下方法从bootRun任务中提取它们,但是Gradle一直抱怨他没有找到函数systemProperty
和environment
。同样,如果我使用 Intellij 集成功能 'extract Method':
task specialConfiguration() {
systemProperties System.properties
systemProperty "spring.cloud.config.failFast", "false"
if (project.hasProperty("TEAM_ENCRYPT_KEY"))
environment "ENCRYPT_KEY", "$TEAM_ENCRYPT_KEY"
}
bootRun {
dependsOn 'specialConfiguration'
}
如何从 3 个任务中提取出这么短的代码来避免重复代码?
Gradle keeps complaining that he does not find the functions
systemProperty
andenvironment
这是 Kotlin DSL 大放异彩的典型例子。您会在任何给定时间确切地知道什么 methods/properties 可用,因为它是一种强类型语言,与 Groovy.
不同话虽如此,当您执行以下操作时:
task specialConfiguration() {
systemProperties System.properties
systemProperty "spring.cloud.config.failFast", "false"
if (project.hasProperty("TEAM_ENCRYPT_KEY"))
environment "ENCRYPT_KEY", "$TEAM_ENCRYPT_KEY"
}
bootRun {
dependsOn 'specialConfiguration'
}
你是:
- 正在声明一个名为
specialConfiguration
的任务。 - 未指定类型,因此类型为 DefaultTask。
- 配置
bootRun
任务依赖于specialConfiguration
我认为您假设 dependsOn
就像 "configuring" 一项任务,而实际上它只是向任务添加依赖项。参见 Adding dependencies to a task。
我假设 runSmokeTest
是 Test
. So tasks test
, runSmokeTest
, and bootRun
all implement the JavaForkOptions 接口类型,这是 systemProperties(..)
、systemProperty(.., ..)
和 environment(.., ..)
方法的来源。
话虽如此,由于您知道要配置的三个任务,并且它们都实现了 JavaForkOptions
并且以某种方式,您可以执行 (Kotlin DSL):
import org.springframework.boot.gradle.tasks.run.BootRun
// Assuming this is a Test task type
tasks.register("runSmokeTest", Test::class)
// Define a new Action (configuration)
val taskConfig = Action<JavaForkOptions> {
systemProperties(System.getProperties() as Map<String, Any>)
systemProperty("spring.cloud.config.failFast", false)
if (project.hasProperty("TEAM_ENCRYPT_KEY")) {
environment("ENCRYPT_KEY", project.property("TEAM_ENCRYPT_KEY")!!)
}
}
// Configure all three tasks
tasks.named("test", Test::class, taskConfig)
tasks.named("runSmokeTest", Test::class, taskConfig)
tasks.named("bootRun", BootRun::class, taskConfig)
Francisco Mateo 回答的 Groovy 版本,以防有人需要。:
Closure<JavaForkOptions> configAction = {
systemProperties System.properties
systemProperty "spring.cloud.config.failFast", "false"
if (project.hasProperty("MOBTECH_ENCRYPT_KEY"))
it.environment "ENCRYPT_KEY", "$MOBTECH_ENCRYPT_KEY"
}
# Configure the tasks with it
bootRun (configAction)