zoukankan      html  css  js  c++  java
  • 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的关系

  • 相关阅读:
    拥抱函数式编程 I
    关于CMS的那点事 I
    常用正规表达式
    javascript source map 的使用
    架构师修炼 后记
    CSS 天坑 I
    架构师修炼 III
    架构师修炼 II
    win10,VM14 安装cnetos6.9 虚拟机黑屏和只有光标闪烁解决办法
    C/C++数组初始化
  • 原文地址:https://www.cnblogs.com/suolu/p/7473280.html
Copyright © 2011-2022 走看看