maven多环境参数配置

maven中properties加载顺序

  1. <build><filters></filters></build>中的配置
  2. pom.xml中的<properties>
  3. mvn -Dproperty=value中定义的property

相同的key的property以最后一个文件中配置为准

不同环境不同配置文件

<profiles>
        <profile>
            <id>develop</id>
            <activation>
                <!--设置默认值 -->
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <resources>
                    <resource>
                        <!--将develop文件夹下都复制到resource中 -->
                        <directory>profiles/develop</directory>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </build>
        </profile>
        <profile>
            <id>release</id>
            <build>
                <resources>
                    <resource>
                        <directory>profiles/release</directory>
                    </resource>
                    <resource>
                        <directory>src/main/resources</directory>
                    </resource>
                </resources>
            </build>
        </profile>
        <profile>

不同环境相同配置文件,不同值

  • 方法一:通过配置文件
<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>sit</id>
            <properties>
                <env>sit</env>
            </properties>
        </profile>
    </profiles>
    
<build>
        <!--设置需要替换值的文件 -->
        <filters>
            <filter>
                src/main/filter/filter-${env}.properties
            </filter>
        </filters>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!--对这里的文件进行解析 -->
                <filtering>true</filtering>
            </resource>
        </resources>
</build>
  • 方法二
<profiles>
        <profile>
            <id>dev</id>
            <properties>
                <!--不同环境不同参数在此配置 -->
                <env>dev</env>
                <remoteconfig>true</remoteconfig>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>sit</id>
            <properties>
                <remoteconfig>false</remoteconfig>
                <env>sit</env>
            </properties>
        </profile>
    </profiles>
    
<build>
        <!--要解析一定要设filtering为true -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!--对这里的文件进行解析 -->
                <filtering>true</filtering>
            </resource>
        </resources>
</build>

resources

与是用来圈定和排除某一文件目录下的文件是否是工程资源的。如果与划定的范围存在冲突时,以划定的范围为准。

    <resources>
        <!-- 需要解析jdbc和mail两个文件 -->
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>jdbc.properties</include>
                <include>mail.properties</include>
            </includes>
        </resource>
        <!-- 需要不需要解析jdbc和mail两个文件 -->
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <excludes>
                <exclude>jdbc.properties</exclude>
                <exclude>mail.properties</exclude>
            </excludes>
        </resource>
    </resources>

其中第一段<resource>配置声明:在src/main/resources目录下,仅jdbc.propertiesmail.properties两个文件是资源文件,然后,这两个文件需要被过滤。而第二段<resource>配置声明:同样在src/main/resources目录下,除jdbc.propertiesmail.properties两个文件外的其他文件也是资源文件,但是它们不会被过滤。

引用: 用 maven filter管理不同环境的配置文件关于Maven resource配制中include与exclude的关系

转载于:https://www.cnblogs.com/suolu/p/7473280.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值