我正在使用 Gradle spring-boot
插件,我需要为测试运行选择一个 spring 事件配置文件。
如何将 spring.profiles.active
系统属性传递给 bootRun
插件的任务?
已经失败的事情:
task bootRunLocal {
systemProperty "spring.profiles.active", "local"
System.setProperty("spring.profiles.active", "local")
tasks.bootRun.execute() // I suspect that this task is executed in a separate JVM
}
一些命令行魔法也失败了:
./gradle -Dspring.profiles.active=local bootRun
有人可以帮我解决我的烦恼吗?
答案和评论的更新:
我可以设置 systemProperty 并将其传递给 spring 容器:
run {
systemProperty "spring.profiles.active", "local"
}
但是,当我这样做时,会为 bootRun
任务和 bootRunLocal
任务设置本地配置文件。我需要一种方法来为 bootRunLocal
任务设置此属性并从 bootRunLocal
调用 booRun
任务。
这听起来很简单,但我从 Maven 的结构化世界中获得了平静。
最佳答案
我知道我来晚了……但我最近遇到了这个确切的问题。我试图在命令行上将 spring.profiles.active 和 spring.config.location 设置为系统属性来启动 bootRun。
所以,要让你的命令行“神奇”发挥作用,只需将它添加到你的 build.gradle 中
bootRun {
systemProperties System.properties
}
然后从命令行运行...
gradle -Dspring.profiles.active=local bootRun
将本地设置为事件配置文件,无需定义单独的任务来简单地添加环境变量。
关于spring - 如何将系统属性传递给 Gradle 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23367507/