zoukankan      html  css  js  c++  java
  • maven 根据profile,resources,filters来区分部署环境

        项目过程中,在不同的阶段,分别需要部署开发环境,测试环境,线上环境。如果都用一套配置文件,很容易弄乱,所以维持多套配置文件很有必要。

        maven提供了一组属性以供开发人员灵活搭配,可以根据环境来打包,比如测试环境:mvn package -DskipTests -P test,-P也就是指定profile里面id为test的子项配置来打包。在pom文件里面,可以指定多套配置文件,下面例子中区分了三套配置文件,不指定-P则默认为dev。其中的env相当于属性文件中的env=test,也可以多指定多个属性,看实际需要。至于为什么不在相应的属性文件中添加类似env这样的配置,下文会提到。

     1   <!-- 区分环境打包 -->
     2   <profiles>
     3       <!-- 开发环境[默认] -->
     4       <profile>
     5           <id>dev</id>
     6           <properties>
     7               <env>dev</env>
     8           </properties>
     9           <activation>
    10               <activeByDefault>true</activeByDefault>
    11           </activation>
    12       </profile>
    13       <!-- 测试环境 -->
    14       <profile>
    15           <id>test</id>
    16           <properties>
    17               <env>test</env>
    18           </properties>
    19       </profile>
    20       <!-- 线上环境 -->
    21       <profile>
    22           <id>product</id>
    23           <properties>
    24               <env>product</env>
    25           </properties>
    26       </profile>
    27   </profiles>
    View Code

        resources和filters,maven中的这两个属性通常是搭配起来使用的,filter这里可以理解为筛选器或者填充器。filters里面可以配置多个filter,但是会以最后一个为准,所以一般只配置一个filter,看下面例子。例子有${env},打包的时候,根据-P test指定,-P 后面的参数与<profile>的<id>一一对应,再根据此ID,到<profile> 的 <properties>得到env属性值,即<env>test</env>。上文指定了三套配置,所以项目里面也需要有这三个配置文件:dev.properties , test.properties , product.properties

        <filters>
            <filter>src/main/resources/${env}.properties</filter>
        </filters>

        然后是resources

    1. directory属性指定资源文件放置的目录。
    2. includes则是指定打包时,需要打到jar/war包里的配置文件。下面例子是说需要把src/main/resources目录下的所有的xml配置文件和init.properties属性文件打到包里,所以dev.properties,test.properties,product.properties这三个属性文件不会出现在[jar/war]包文件里
    3. filtering如果设置为false的话,则表示上文的filters配置失效;如果设置为true,则会根据${env}.properties里面的键值对来填充includes指定文件里的${xxxx}占位符。
        <!-- 指定资源文件目录 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- 设置为true,则init.properties会根据${env}.properties里面的配置来填充 -->
                <filtering>true</filtering>
                <includes>
                    <include>*.xml</include>
                    <include>init.properties</include>
                </includes>
            </resource>
        </resources>

         最后演示一下打包运行的结果,假设init.properties里有这么几行:

    ###########################mysql#################################################
    pool.driverClassName=${pool.driverClassName}
    pool.url=${pool.url}
    pool.username=${pool.username}
    pool.password=${pool.password}
    pool.maxWait=${pool.maxWait}
    # env的属性在这里也有效
    env=${env}

    test.properties属性文件里是如下配置,注意没有pool.maxWait配置

    ###########################mysql#################################################
    pool.driverClassName=com.mysql.jdbc.Driver
    pool.url=jdbc:mysql://192.168.100.23:3306/etmis
    pool.username=root
    pool.password=123456

    则运行打包命令[mvn package -DskipTests -P test]之后,init.properties会被填充为:

    ###########################mysql#################################################
    pool.driverClassName=com.mysql.jdbc.Driver
    pool.url=jdbc:mysql://192.168.100.23:3306/etmis
    pool.username=root
    pool.password=123456
    pool.maxWait=${pool.maxWait}
    # env的属性在这里也有效
    env=test
    pool.maxWait不会被填充,因为在test.properties属性文件里面没有这个键值对。在xml文件里面的筛选填充也是一样,会根据文件中的${xxxx}占位符来筛选处理。
    <profile>里面配置的属性和${env}.properties里面的属性如果有冲突,比如test.properties里面也配置了【env=testaaaa】,会优先使用<profile>里面的


  • 相关阅读:
    html 上传图片前预览
    php获取当月天数及当月第一天及最后一天、上月第一天及最后一天实现方法
    php 计算 pdf文件页数
    php 获取半年内每个月的订单数量, 总价, 月份
    php 获取两个数组之间不同的值
    小程序支付功能
    关于nginx的Job for nginx.service failed because the control process exited with error code.错误
    linux 安装 Apollo
    MongoDB待续。。。
    ABP vNext...待续
  • 原文地址:https://www.cnblogs.com/yucy/p/7082042.html
Copyright © 2011-2022 走看看