简体   繁体   English

从其他属性文件替换 application.properties 的值

[英]Replace value of application.properties from other properties file

I have below file application properties file in my Spring boot application.我的 Spring boot 应用程序中有以下文件应用程序属性文件。 All properties file are in src/main/resources folder Spring boot version is 2.1.6所有属性文件都在 src/main/resources 文件夹中Spring boot 版本为 2.1.6

application.properties application-dev.properties application-tst.properties application.properties application-dev.properties application-tst.properties

application.properties app.name={app.name} app.common=Common val application.properties app.name={app.name} app.common=通用值
application-dev.properties app.name=My dev app application-dev.properties app.name=我的开发应用
application-tst.properties app.name=My tst app application-tst.properties app.name=我的 tst 应用

Dev and tst are maven profile i have created 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>

If i am building the project with dev profile ,i shouuld get the following in my application.properties如果我使用 dev profile 构建项目,我应该在我的 application.properties 中得到以下内容

1)mvn -Pdev clean install 1)mvn -Pdev 全新安装

application.properties app.name=My dev app app.common=Common val application.properties app.name=我的开发应用app.common=通用值

2)mvn -Ptst clean install 2)mvn -Ptst 全新安装

application.properties app.name=My tst app app.common=Common val application.properties app.name=我的 tst 应用 app.common=Common val

How can i achieve this ?我怎样才能做到这一点?

You can use the environment variable to set the active profile like this您可以像这样使用环境变量来设置活动配置文件

mvn install -Dspring.profiles.active=dev

or或者

mvn install -Dspring.profiles.active=tst

This is probably not the recommended way, but you can use org.apache.maven.plugins.maven-resources-plugin as below.这可能不是推荐的方式,但您可以使用org.apache.maven.plugins.maven-resources-plugin如下。

pom.xml pom.xml

    <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>

application.properties应用程序属性

app.name=@app.name@
app.common=Common val

application-dev.properties应用程序-dev.properties

app.name=My dev app

application-tst.properties应用程序-tst.properties

app.name=My tst app

and then, mvn -Pdev clean install or mvn -Ptst clean install然后, mvn -Pdev clean installmvn -Ptst clean install

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM