从 Maven 配置文件激活 spring 引导配置文件 [英] activate spring boot profile from maven profile
问题描述
我想将我的 junit 测试和集成测试分开.所以我在 pom.xml 中为集成测试创建了一个单独的配置文件,如下所示:
I want to separate my junit test and integration test separate. So I created a separate profile in pom.xml for the integration test as follows:
<profiles>
<profile>
<id>integration-test</id>
<properties>
<test>IntegrationTestTrigger</test>
<spring.profiles.active>integration-test</spring.profiles.active>
</properties>
</profile>
<profiles>
当我运行 maven 命令 mvn test -Pintegration-test
时,它正在选择
标签中定义的测试类,如 <代码>IntegrationTestTrigger.但它没有设置 spring.profiles.active
属性.所以测试从默认配置文件开始.它与 maven 命令 mvn test -Dtest=IntegrationTestTrigger -Dspring.profiles.active=integration-test
The when I run the maven command mvn test -Pintegration-test
, it is picking the test class as defined in the <properties>
tag shown above as IntegrationTestTrigger
. But it is not setting the spring.profiles.active
property. So the test is starting with default profile. It is working fine with the maven command mvn test -Dtest=IntegrationTestTrigger -Dspring.profiles.active=integration-test
但根据我的组织 jenkins 设置,我需要运行 mvn test -Pintegration-test
进行集成测试,因此我无法向 mvn 命令添加任何额外的环境变量
But as per my organisations jenkins setting, I need to run mvn test -Pintegration-test
for the integration test, so I cannot add any extra environment variables to mvn command
推荐答案
中的属性用于替换资源文件夹中 .properties/.yml 文件中的属性.
The properties at the is meant for property substitution at the .properties/.yml file inside resources folder.
示例:
application.yml:
application.yml:
spring:
profiles:
active: '@spring.profiles.active@'
pom.xml:
<profile>
<id>dev</id>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
</profile>
这里,@spring.profiles.active@
将在编译期间被 dev 替换(通过 maven-resources-plugin 插件).Spring Boot 使用 @
作为 spring-boot-starter-parent pom 中的资源分隔符.您可以通过更改以下属性将其更改为任何字符
Here, the @spring.profiles.active@
will be replaced with dev during compile(by maven-resources-plugin plugin). Spring Boot uses @
as the resource delimiter at the spring-boot-starter-parent pom. You can change it to any character by changing the following property
//pom.xml
<project .....>
<properties>
<resource.delimiter>@</resource.delimiter>
...
</properties>
参见 https://github.com/gtiwari333/spring-boot-blog-app/blob/master/pom.xml#L436 一个完整的例子
See https://github.com/gtiwari333/spring-boot-blog-app/blob/master/pom.xml#L436 for an complete example
这篇关于从 Maven 配置文件激活 spring 引导配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!