![](/img/trans.png)
[英]Replace environment variables in Spring properties file other than application.properties
[英]Replace value of application.properties from other properties file
我的 Spring boot 应用程序中有以下文件应用程序属性文件。 所有属性文件都在 src/main/resources 文件夹中Spring boot 版本为 2.1.6
application.properties application-dev.properties application-tst.properties
application.properties app.name={app.name} app.common=通用值
application-dev.properties app.name=我的开发应用
application-tst.properties app.name=我的 tst 应用
Dev 和 tst 是我创建的 Maven 配置文件
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>dev</env>
</properties>
</profile>
<profile>
<id>tst</id>
<properties>
<env>tst</env>
</properties>
</profile>
</profiles>
如果我使用 dev profile 构建项目,我应该在我的 application.properties 中得到以下内容
1)mvn -Pdev 全新安装
application.properties app.name=我的开发应用app.common=通用值
2)mvn -Ptst 全新安装
application.properties app.name=我的 tst 应用 app.common=Common val
我怎样才能做到这一点?
您可以像这样使用环境变量来设置活动配置文件
mvn install -Dspring.profiles.active=dev
或者
mvn install -Dspring.profiles.active=tst
这可能不是推荐的方式,但您可以使用org.apache.maven.plugins.maven-resources-plugin
如下。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<filters>
<filter>src/main/resources/application-${env}.properties</filter>
</filters>
</build>
app.name=@app.name@
app.common=Common val
app.name=My dev app
app.name=My tst app
然后, mvn -Pdev clean install
或mvn -Ptst clean install
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.