Spring中的Maven配置文件和应用程序yml文件 [英] Maven profiles and application yml file in Spring
问题描述
我有一个使用以下命令构建的Spring项目
I have a Spring project that I build using the following command
mvn clean install -Pdevelopment
通过根据maven配置文件选择合适的属性文件来完美地工作
which worked perfectly by selecting the appropriate properties file based on maven profile
我们当前的应用程序现已更新为同时包含application.yml文件和属性文件
Our current application is now updated to have both application.yml file and properties file
Yml文件提供了基于弹簧配置文件创建属性的功能
Yml file provides the ability to create properties based on spring profiles
#DEV
spring:
profiles: profile1
environment:
property1: AAA
property2: BBB
---
#PROD
spring:
profiles: profile2
environment:
property1: CCC
property2: DDD
---
这适用于使用 -Dspring的弹簧配置文件.profiles.active = profile1
有没有办法读取maven配置文件(而不是弹簧配置文件)并相应地设置属性?
Is there a way to read maven profiles (instead of spring profiles) and set properties accordingly?
推荐答案
由于您不再需要使用弹簧配置文件,因此在 application.yml
中只需要一个键。从你的例子中可以看出它是这样的:
Since you no longer want to use spring profiles, you only need 1 key of each in the application.yml
. Taken from your example it could look like so:
environment:
property1: @property1@
property2: @property2@
然后在 pom.xml $中创建配置文件c $ c>或
settings.xml
<profiles>
<profile>
<id>dev</id>
<properties>
<property1>AAA</property1>
<property2>BBB</property2>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<property1>CCC</property1>
<property2>DDD</property2>
</properties>
</profile>
</profiles>
在我的应用程序类中使用如下:
Used in my application class like so:
@Value("${environment.property1}")
private String profileProperty;
@Value("${environment.property2}")
private String settingsProperty;
这篇关于Spring中的Maven配置文件和应用程序yml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!