在Maven Pom中指定备用Spring application.properties [英] Specify alternative Spring application.properties in Maven Pom

查看:570
本文介绍了在Maven Pom中指定备用Spring application.properties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Maven pom文件中指定替代的application.properties?我有2个不同的Maven配置文件,并希望根据配置文件使用其他不同的属性文件.

How would I go about specifying an alternative application.properties in a Maven pom file? I have 2 different Maven profiles and would like to use different a different property file depending on the profile.

推荐答案

如果您使用的是Spring Boot,则有一种简单的方法.

If you are using Spring boot, there is an easy way of doing this.

  1. 在maven中创建两个配置文件,并在每个配置文件中设置一个要执行的Spring配置文件的属性.

  1. Create two profiles in maven, and set a property in each profile with the name of the Spring profile you want to execute.

<profile>
    <id>dev</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <!-- Default spring profile to use -->
        <spring.profiles.active>dev</spring.profiles.active>
        <!-- Default environment -->
        <environment>develop</environment>
    </properties>
</profile>

  • 在application.properties内,添加以下属性:

  • Inside your application.properties, add this property:

    spring.profiles.active = $ {spring.profiles.active}

    spring.profiles.active=${spring.profiles.active}

    1. 使用此模式application- profile .properties为每个配置文件创建application.property.例如:
    1. Create an application.property for each profile, using this pattern application-profile.properties. For example:

    application-dev.properties
    application-prod.properties

    application-dev.properties
    application-prod.properties

    1. 请确保在资源插件中进行主动过滤:

    
          ...
          <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
          </resource>
         ...
    

    另一种方法是在执行maven的过程中创建一个名为 activeprofile.properties 的文件. Spring Boot查找此文件以加载活动配置文件.您可以按以下方式创建此文件:

    Another way is to create a file during the execution of maven called activeprofile.properties. Spring boot looks this file to load the active profile. You can create this file as follows:

       <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <configuration>
                            <target>
                                <echo message="spring.profiles.active=${spring.profiles.active}" file="target/classes/config/activeprofile.properties" />
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                </configuration>
            </plugin>
    

    这篇关于在Maven Pom中指定备用Spring application.properties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆