Maven插件之portable-config-maven-plugin(不一样环境打包)

大型项目中,分开发环境、测试环境、生产环境等;html

不一样环境,配置不一样,或数据源,或服务器,或数据库等;mysql

 

问题来了,如何使用Maven针对不一样的环境来打包呢?git

 

Maven提供了Profile的概念,用来解决此类问题,其原理很简单,就是使用变量替换;举个例子来讲明,测试项目目录结构以下图所示:github

好比开发环境和生产环境的数据库不一样,db.properties配置文件内容以下:sql

 

[html]  view plain  copy
 在CODE上查看代码片派生到个人代码片
  1. #测试库  
  2. db.url=192.10.2.168  
  3. db.username=dbtest  
  4. db.password=dbtest  
  5. #正式库  
  6. #db.url=192.20.1.11  
  7. #db.username=admin  
  8. #db.password=comfreesecurity  


默认开启测试环境;生产环境打包时,须要手动修改该配置文件.juvenxu说过,手动意味着低效率,高错误率!!shell

 

Maven提供的Profile功能,可解决以上问题:数据库

一、在pom.xml文件中定义两个不一样的Profile,分别定义开发环境和生产环境的数据库信息:apache

[html]  view plain  copy
 在CODE上查看代码片派生到个人代码片
  1. <profiles>  
  2.     <profile>  
  3.         <id>kaifa</id>  
  4.         <properties>  
  5.             <db.url>192.10.2.168</db.url>  
  6.             <db.username>dbtest</db.username>  
  7.             <db.password>dbtest</db.password>  
  8.         </properties>  
  9.     </profile>  
  10.       
  11.     <profile>  
  12.         <id>shengchan</id>  
  13.         <properties>  
  14.             <db.url>192.20.1.11</db.url>  
  15.             <db.username>admin</db.username>  
  16.             <db.password>comfreesecurity</db.password>  
  17.         </properties>  
  18.     </profile>  
  19. </profiles>  

 

二、将原来的配置文件内容修改以下:服务器

[html]  view plain  copy
 在CODE上查看代码片派生到个人代码片
  1. db.url=${db.url}  
  2. db.username=${db.username}  
  3. db.password=${db.password}  


三、须要开启资源文件过滤,代码以下:app

[html]  view plain  copy
 在CODE上查看代码片派生到个人代码片
    1. <resources>  
    2.     <resource>  
    3.         <directory>${project.basedir}/src/main/resources</directory>  
    4.         <filtering>true</filtering>  
    5.     </resource>  
    6. </resources>  添加<filtering>true</filtering>,容许使用变量替换资源文件;
      四、使用Maven命令打包时,指定Profile进行打包,命令以下:

      mvn package -Pkaifa

      mvn package -Pshengchan

      如此便可。

       

      此命令用的多了,就会发现,两个环境必选其一,若是能设置其一个为默认开启,就不用每次都手动指定了,这个需求很现实,毕竟开发环境须要持续不断的编译、打包、部署等,而上线,则是一段时间才会运行一次的;所以,默认启用开发环境是最优的方案,Maven支持默认启用某个Profile,只需在<profile>内添加以下代码便可:

      [html]  view plain  copy
       在CODE上查看代码片派生到个人代码片
      1. <activation>  
      2.     <activeByDefault>true</activeByDefault>  
      3. </activation>  

      此处须要注意:一旦显式指定某个Profile,则该配置无效!

       

      在实际开发中使用以上方式操做时,天然而然的会提出如下的问题:假如配置文件的信息不少,那么Profile的内容会很臃肿,不便于管理,若是能将配置信息从Profile抽取出来,独立放置,再根据不一样的Profile去调用,如此就更好了!

      Maven针对以上需求,确实有解决方案,就是使用<filters>标签,针对不一样的环境,使用不一样的文件替换原来配置文件中的变量。

      项目根目录下新建以下目录和文件:

      db.properties问标准的属性文件,kaifa/db.properties和shengchan/db.properties文件内容分别以下:


    7. [html]  view plain  copy
       在CODE上查看代码片派生到个人代码片
      1. db.url=192.10.2.168  
      2. db.username=dbtest  
      3. db.password=dbtest  

       

      [html]  view plain  copy
       在CODE上查看代码片派生到个人代码片
      1. db.url=192.20.1.11  
      2. db.username=admin  
      3. db.password=confreesecurity  

      将Profile中的属性信息抽取到了db.properties文件中,同时在Profile中添加<filters></filters>部分,修改后的代码以下:

       

       

      [html]  view plain  copy
       在CODE上查看代码片派生到个人代码片
        1. <profiles>  
        2.     <profile>  
        3.         <id>kaifa</id>  
        4.         <activation>  
        5.             <activeByDefault>true</activeByDefault>  
        6.         </activation>  
        7.         <build>  
        8.             <filters>  
        9.                 <filter>${basedir}/filters/kaifa/db.properties</filter>  
        10.             </filters>  
        11.         </build>  
        12.     </profile>  
        13.       
        14.     <profile>  
        15.         <id>shengchan</id>  
        16.         <build>  
        17.             <filters>  
        18.                 <filter>${basedir}/filters/shengchan/db.properties</filter>  
        19.             </filters>  
        20.         </build>  
        21.     </profile>  
        22. </profiles>  

      添加了<filters><filter>...</filter></filters>部分,使用指定的文件内容替换原文件中的变量;

      如此以后,使用Maven命令进行构建便可。

          细心的人会发现,以上Profile中的filters部分,除了使用的目录名称不一样以外,其余代码所有相同,重复!!!

          若是再多几个环境的话,代码冗余可想而知,所以须要优化,其实方法很简单,仍是使用变量替换,修改后的pom.xml文件内容以下:

       

      [html]  view plain  copy
       在CODE上查看代码片派生到个人代码片
      1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
      3.   
      4.     <modelVersion>4.0.0</modelVersion>  
      5.   
      6.     <groupId>com.test</groupId>  
      7.     <artifactId>Profile</artifactId>  
      8.     <version>0.0.1-SNAPSHOT</version>  
      9.       
      10.     <build>  
      11.         <resources>  
      12.             <resource>  
      13.                 <directory>${project.basedir}/src/main/resources</directory>  
      14.                 <filtering>true</filtering>  
      15.             </resource>  
      16.         </resources>  
      17.         <filters>  
      18.             <filter>${basedir}/filters/${filters.env}/db.properties</filter>  
      19.         </filters>  
      20.     </build>  
      21.     <profiles>  
      22.         <profile>  
      23.             <id>kaifa</id>  
      24.             <activation>  
      25.                 <activeByDefault>true</activeByDefault>  
      26.             </activation>  
      27.             <properties>  
      28.                 <filters.env>kaifa</filters.env>  
      29.             </properties>  
      30.         </profile>  
      31.   
      32.         <profile>  
      33.             <id>shengchan</id>  
      34.             <properties>  
      35.                 <filters.env>shengchan</filters.env>  
      36.             </properties>  
      37.         </profile>  
      38.     </profiles>  
      39. </project>  

          使用-P参数时,会激活<filters.env>属性,从而使用指定的过滤文件进行变量替换。



          其实,以上方式使用久了,仍是会有些想法,既然用变量,也就是说,必须使用Maven命令以后,才能部署到Tomcat等服务器中,屡次重复的操做,仍是有至关多的时间浪费在maven命令上,尤为在改动不多的代码的状况下;

          此时又会提出新的需求,可否在不使用maven命令的状况下便可进行平常开发;测试环境(或生产环境)打包时,使用Maven命令和-P参数指定环境进行打包呢?

       

      很幸运,Juven Xu--国内Maven第一人--为咱们提供了这样的一个插件portable-config-maven-plugin,使用该插件,能够在不改变原有代码的基础上,进行不一样环境的打包。

      ------如下应该算做最佳实践了

      portable-config-maven-plugin插件的原理是内容替换(而不是变量替换);

       

       

      插件代码地址:https://github.com/juven/portable-config-maven-plugin,目前最新版本为1.1.4;
      该插件使用方法以下:

      假设src/main/resources/db.properties文件代码以下:

       

      [html]  view plain  copy
       在CODE上查看代码片派生到个人代码片
      1. database.jdbc.username=dev  
      2. database.jdbc.password=dev_pwd  

          对于测试环境,建立一个属性替换文件src/main/portable/test.xml,代码以下:

       

       

      [html]  view plain  copy
       在CODE上查看代码片派生到个人代码片
      1. <?xml version="1.0" encoding="utf-8" ?>  
      2. <portable-config>  
      3.     <config-file path="WEB-INF/classes/db.properties">  
      4.         <replace key="database.jdbc.username">test</replace>  
      5.         <replace key="database.jdbc.password">test_pwd</replace>  
      6.     </config-file>  
      7. </portable-config>  

          此文件为替换内容描述文件,<config-file>标签的path属性的值是基于war包的
      相对路径
          ,指须要被内容替换的文件;

          配置portable-config-maven-plugin插件(
      该插件默认绑定到package声明周期
          ):

       

       

      [html]  view plain  copy
       在CODE上查看代码片派生到个人代码片
      1. <plugin>  
      2.     <groupId>com.juvenxu.portable-config-maven-plugin</groupId>  
      3.     <artifactId>portable-config-maven-plugin</artifactId>  
      4.     <version>1.1.4</version>  
      5.     <executions>  
      6.         <execution>  
      7.             <goals>  
      8.                 <goal>replace-package</goal>  
      9.             </goals>  
      10.         </execution>  
      11.     </executions>  
      12.     <configuration>  
      13.         <portableConfig>src/main/portable/test.xml</portableConfig>  
      14.     </configuration>  
      15. </plugin>  

          或在命令行指定替换内容描述文件:mvn clean package -DportableConfig="src/main/portable/test.xml"

          打包完成后,db.properties文件内容会被替换。



      该插件目前支持的内容替换文件格式有:

      .properties

       

       

      [html]  view plain  copy
       在CODE上查看代码片派生到个人代码片
      1. database.jdbc.username=dev  
      2. database.jdbc.password=dev_pwd  
      3. 使用  
      4. <replace key="database.jdbc.username">test</replace>  
      5. <replace key="database.jdbc.password">test_pwd</replace>  
      6. 替换为:  
      7. database.jdbc.username=test  
      8. database.jdbc.password=test_pwd  

       

      .xml(xml元素和属性可使用xPath进行替换)

       

      [html]  view plain  copy
       在CODE上查看代码片派生到个人代码片
      1. <?xml version="1.0" encoding="UTF-8"?>  
      2. <server>  
      3.     <port>8080</port>  
      4.     <hosts>  
      5.         <host id="1">localhost</host>  
      6.         <host id="2">localhost</host>  
      7.     </hosts>  
      8.     <mode value="debug" />  
      9. </server>  
      10. 使用  
      11. <replace xpath="/server/port">80</replace>  
      12. <replace xpath="//host/[@id='1']">192.168.1.1</replace>  
      13. <replace xpath="//host/[@id='2']">192.168.1.2</replace>  
      14. <replace xpath="/server/mode/@value">run</replace>  
      15. 替换为:  
      16. <?xml version="1.0" encoding="UTF-8"?>  
      17. <server>  
      18.     <port>80</port>  
      19.     <hosts>  
      20.         <host id="1">192.168.1.1</host>  
      21.         <host id="2">192.168.1.2</host>  
      22.     </hosts>  
      23.     <mode value="run"/>  
      24. </server>  

       

      .sh

       

      [html]  view plain  copy
       在CODE上查看代码片派生到个人代码片
      1. 无引号、单引号、双引号、输出的shell变量可被替换  
      2. BIN_HOME=/tmp/bin  
      3. OUT_HOME="/tmp/out"  
      4. LOG_HOME='/tmp/log'  
      5. export APP_HOME="/tmp/app"  
      6. 使用  
      7. <replace key="BIN_HOME">/home/juven/bin</replace>  
      8. <replace key="OUT_HOME">/home/juven/out</replace>  
      9. <replace key="LOG_HOME">/home/juven/log</replace>  
      10. <replace key="APP_HOME">/home/juven/app</replace>  
      11. 替换为:  
      12. BIN_HOME=/home/juven/bin  
      13. OUT_HOME="/home/juven/out"  
      14. LOG_HOME='/home/juven/log'  
      15. export APP_HOME="/home/juven/app"  

       

      相似.properties格式

       

      [html]  view plain  copy
       在CODE上查看代码片派生到个人代码片
      1. 假若有个key=value类型的配置文件,但扩展名不是.properties,可按照如下方式指定:  
      2. <?xml version="1.0" encoding="utf-8" ?>  
      3. <portable-config>  
      4.     <config-file path="db.ini" type=".properties">  
      5.         <replace key="mysql.host">192.168.1.100</replace>  
      6.     </config-file>  
      7. </portable-config>  
      8. 使用type属性强制指定文件类型。  


       

      对该插件的介绍到此为止。

      回头看看,发现内容和标题不符,说是介绍portable-config-maven-plugin插件,

      却花了大篇幅的内容介绍了Maven其余的标签使用,不过以上内容总有相同之处--针对不一样环境打包的一些解决方法

相关文章
相关标签/搜索