如何通过gradle任务使用spring profile运行bootRun [英] How to run bootRun with spring profile via gradle task
问题描述
我正在尝试设置gradle以启动启用了各种弹簧配置文件的bootRun
进程.
I'm trying to set up gradle to launch the bootRun
process with various spring profiles enabled.
我当前的bootRun
配置如下:
bootRun {
// pass command line options from gradle to bootRun
// usage: gradlew bootRun "-Dspring.profiles.active=local,protractor"
if (System.properties.containsKey('spring.profiles.active')) {
systemProperty "spring.profiles.active", System.properties['spring.profiles.active']
}
}
我想用gradle
任务设置系统属性,然后执行bootRun
.
I'd like to set system properties with a gradle
task, and then execute bootRun
.
我的尝试看起来像这样:
My attempt looked like this:
task bootRunDev
bootRunDev {
System.setProperty("spring.profiles.active", "Dev")
}
几个问题:
-
systemProperty
是spring boot bootRun配置的一部分吗? - 是否可以在另一个任务中设置系统属性?
- 下一步应该是什么?我需要在
bootRun
之前进行 - 还有其他我应该研究的方法吗?
bootRunDev
配置
- is
systemProperty
a part of the spring boot bootRun configuration? - is it possible to set a system property in another task?
- What should my next step be? I need to get
bootRunDev
configuration to happen beforebootRun
- Is there another approach I should look into
-埃里克
推荐答案
最简单的方法是定义默认值并允许其被覆盖.我不确定在这种情况下systemProperty的用途是什么.简单的参数即可完成工作.
Simplest way would be to define default and allow it to be overridden. I am not sure what is the use of systemProperty in this case. Simple arguments will do the job.
def profiles = 'prod'
bootRun {
args = ["--spring.profiles.active=" + profiles]
}
要运行dev:
./gradlew bootRun -Pdev
要为您的任务添加依赖项,您可以执行以下操作:
To add dependencies on your task you can do something like this:
task setDevProperties(dependsOn: bootRun) << {
doFirst {
System.setProperty('spring.profiles.active', profiles)
}
}
在Gradle中有很多方法可以实现这一目标.
There are lots of ways achieving this in Gradle.
为每个环境配置单独的配置文件.
Configure separate configuration files per environment.
if (project.hasProperty('prod')) {
apply from: 'gradle/profile_prod.gradle'
} else {
apply from: 'gradle/profile_dev.gradle'
}
每种配置都可以覆盖任务,例如:
Each configuration can override tasks for example:
def profiles = 'prod'
bootRun {
systemProperty "spring.profiles.active", activeProfile
}
在这种情况下,通过提供prod
标志来运行,就像这样:
Run by providing prod
flag in this case just like that:
./gradlew <task> -Pprod
这篇关于如何通过gradle任务使用spring profile运行bootRun的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!