java Spring 中的 Maven 配置文件和应用程序 yml 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34615466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven profiles and application yml file in Spring
提问by Dexter
I have a Spring project that I build using the following command
我有一个使用以下命令构建的 Spring 项目
mvn clean install -Pdevelopment
which worked perfectly by selecting the appropriate properties file based on maven profile
通过根据 Maven 配置文件选择适当的属性文件,它完美地工作
Our current application is now updated to have both application.yml file and properties file
我们当前的应用程序现在更新为具有 application.yml 文件和属性文件
Yml file provides the ability to create properties based on spring profiles
Yml 文件提供了基于弹簧配置文件创建属性的能力
#DEV
spring:
profiles: profile1
environment:
property1: AAA
property2: BBB
---
#PROD
spring:
profiles: profile2
environment:
property1: CCC
property2: DDD
---
This works fine with spring profiles using -Dspring.profiles.active=profile1
这适用于弹簧轮廓使用 -Dspring.profiles.active=profile1
Is there a way to read maven profiles (instead of spring profiles) and set properties accordingly?
有没有办法读取 maven 配置文件(而不是 spring 配置文件)并相应地设置属性?
采纳答案by Martin Hansen
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:
由于您不再想使用弹簧配置文件,因此您只需要application.yml
. 从你的例子来看,它可能看起来像这样:
environment:
property1: @property1@
property2: @property2@
Then make profiles in your pom.xml
or settings.xml
然后在您的pom.xml
或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;