如何将系统属性传递给 Gradle 任务 [英] How to pass system property to Gradle task
问题描述
我正在使用 Gradle spring-boot
插件,我需要为测试运行选择一个 spring 活动配置文件.
I'm using Gradle spring-boot
plugin and I need to select a spring active profile for the test run.
如何将 spring.profiles.active
系统属性传递给 bootRun
插件的任务?
How do I pass spring.profiles.active
system property to the bootRun
plugin's task?
已经失败的:
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
}
并且一些命令行魔法也失败了:
and some command line magic also fails:
./gradle -Dspring.profiles.active=local bootRun
有人能帮我解决我的麻烦吗?
Could someone kindly help me solve my troubles?
从答案和评论中更新:
我可以通过执行以下操作来设置 systemProperty 并将其传递给 spring 容器:
I'm able to set the systemProperty and pass it to the spring container by doing :
run {
systemProperty "spring.profiles.active", "local"
}
但是,当我执行此操作时,正在为 bootRun
任务和 bootRunLocal
任务设置本地配置文件.我需要一种方法来为 bootRunLocal
任务设置此属性并从 bootRunLocal
调用 booRun
任务.
However, when I do this, the local profile is being set for both bootRun
task and bootRunLocal
task. I need a way to set this property for bootRunLocal
task and call booRun
task from bootRunLocal
.
这听起来可能很简单,但我从 Maven 的结构化世界中获得了平静.
That might sound very simple, but I come with peace from the structured world of Maven.
推荐答案
task local {
run { systemProperty "spring.profiles.active", "local" }
}
bootRun.mustRunAfter local
然后运行gradle命令:
Then run gradle command as:
gradle bootRun local
这篇关于如何将系统属性传递给 Gradle 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!