我有一个Spring Boot项目,使用gradle,有三个Spring配置文件:
-
"development"(我正在开发的时候)
-
"test"(因为我正在运行测试)
-
"production"(适用于在 生产环境 中部署[至heroku])
因此,我有四个应用程序* .yml文件:
-
application.yml(包含所有共享默认值)
-
application-development.yml
-
application-test.yml
-
application-production.yml
这些环境都很好 . 问题是当我将代码部署到heroku时,heroku运行'gradle build'(反过来运行'gradle test'),而heroku没有设置环境变量的选项 . 因此我无法设置有效的配置文件 . 因此,当它运行测试时,它使用的是application.yml而没有application-test.yml中的覆盖 . 因此测试显然失败了 .
到目前为止,我唯一的解决方案是将所有application-test.yml默认值放入application.yml,然后在其他配置文件中再次覆盖它们,但这显然远非理想 .
有办法:
-
从gradle中设置活动的Spring配置文件(注意我没有't have edit access to the ' build ' tasks as they come from the ' spring boot gradle plugin')?
-
或者Spring Boot中有没有办法在运行测试时设置默认的活动Spring配置文件?
2 回答
如果您使用
@WebIntegrationTest
或@IntegrationTest
,可以使用注释将配置文件设置为属性值 .看起来像这样:
认为joshiste的答案是正确的 .
无论如何我猜你也可以像这样设置
SPRING_PROFILES_ACTIVE
环境变量: