1. 内置属性
  2. ${basedir}表示项目根目录,即包含pom.xml文件的目录;
  3. ${version}表示项目版本;
  4. ${project.basedir}同${basedir};
  5. ${project.baseUri}表示项目文件地址;
  6. ${maven.build.timestamp}表示项目构件开始时间;
  7. ${maven.build.timestamp.format}表示属性${maven.build.timestamp}的展示格式,默认值为yyyyMMdd-HHmm,可自定义其格式,其类型可参考java.text.SimpleDateFormat
  8. <filters>
  9. <filter>${basedir}/../config/${env}.properties</filter>
  10. </filters>
  11. <profiles>
  12. <!-- dev-->
  13. <profile>
  14. <id>dev</id>
  15. <properties>
  16. <env>dev</env>
  17. </properties>
  18. <activation>
  19. <activeByDefault>true</activeByDefault><!-- default setting -->
  20. </activation>
  21. </profile>
  22. <!-- test -->
  23. <profile>
  24. <id>test</id>
  25. <properties>
  26. <env>test</env>
  27. </properties>
  28. </profile>
  29. </profiles>

一、什么是Maven多环境配置?

在开发的过程中,经常需要面对不同的运行环境(开发环境、测试环境、生产环境、内网环境、外网环境等等),在不同的环境中,相关的配置一般不一样,比如数据源配置、日志文件配置、以及一些软件运行过程中的基本配置。每次在不同环境部署程序时,都需要修改相应的配置文件,使之完成环境的配置。这么做存在一个比较大的问题:每次修改配置非常麻烦,而且配置错误会产生不可预估的影响,比如,在发布生产环境时用的开发环境的配置还好,但如果在开发环境下用生产环境的数据,将会造成生产数据的污染,导致生产环境崩溃。这就是多环境配置!

二、怎么用?

在maven中实现多环境的构建可移植性需要使用profile,通过不同的环境激活不同的profile来达到构建的可移植性。

  • 项目目录结构

Maven多环境配置 - 图1
Paste_Image.png

  • 代码
    jdbc.properties

    1. jdbc.driver=${jdbc_driver}
    2. jdbc.url=${jdbc_url}
    3. jdbc.username=${jdbc_username}
    4. jdbc.password=${jdbc_password}

    filter-dev-env.properties

    1. jdbc_driver=com.mysql.jdbc.Driver
    2. jdbc_url=jdbc:mysql://127.0.0.1:3306/dev
    3. jdbc_username=root
    4. jdbc_password=123456

    filter-pro-env.properties

    1. jdbc_driver=com.mysql.jdbc.Driver
    2. jdbc_url=jdbc:mysql://127.0.0.1:3306/pro
    3. jdbc_username=root
    4. jdbc_password=123456

    filter-test-env.properties

    1. jdbc_driver=com.mysql.jdbc.Driver
    2. jdbc_url=jdbc:mysql://127.0.0.1:3306/test
    3. jdbc_username=root
    4. jdbc_password=123456

    pom.xml

    1. <profiles>
    2. <profile>
    3. <!-- 本地开发环境 -->
    4. <id>dev</id>
    5. <properties>
    6. <profiles.active>dev</profiles.active>
    7. </properties>
    8. <!-- 默认是本地开发环境 -->
    9. <activation>
    10. <activeByDefault>true</activeByDefault>
    11. </activation>
    12. </profile>
    13. <profile>
    14. <!-- 测试环境 -->
    15. <id>test</id>
    16. <properties>
    17. <profiles.active>test</profiles.active>
    18. </properties>
    19. </profile>
    20. <profile>
    21. <!-- 生产环境 -->
    22. <id>pro</id>
    23. <properties>
    24. <profiles.active>pro</profiles.active>
    25. </properties>
    26. </profile>
    27. </profiles>
    28. <build>
    29. <!-- maven模块化的话最好从父类继成取,打成包的命名 -->
    30. <finalName>${artifactId}-${version}</finalName>
    31. <!-- 使用指定的filter进行过滤,在执行mvn命令的时候带上-Ppro就代表生产环境,就会加载生产环境的properties,-Pdev就代表开发环境(默认) -->
    32. <filters>
    33. <filter>src/main/resources/multiEnv/filter-${profiles.active}-env.properties</filter>
    34. </filters>
    35. <!-- 配置需要被替换的资源文件路径, jdbc.properties -->
    36. <resources>
    37. <resource>
    38. <!--
    39. 资源文件位置src/main/resources/,这下面的资源文件的${}会全部被替换成filter中的标签内容。
    40. directory指定的value会作为classes的资源跟目录,
    41. 比如指定:src/main/resources/,则classes下会出现jdbc等包,
    42. 若指定:src/main/resources/jdbc/,则classes下直接出现jdbc包下的文件,不会额外出现jdbc等其他包结构。因为他把jdbc作为了根目录
    43. -->
    44. <directory>src/main/resources/</directory>
    45. <!-- 在某个resource中如果设置filtering为true,将会根据输入参数动态修改相关内容。 -->
    46. <filtering>true</filtering>
    47. <!-- 排除标签 -->
    48. <excludes>
    49. <!--
    50. exclude可以排除指定文件,支持通配符 ,匹配项不会生成到classes目录下,路径是以directory开始的
    51. 在这里就是directory(src/main/resources/)/multiEnv/filter-*-env.properties
    52. -->
    53. <exclude>multiEnv/filter-*-env.properties</exclude>
    54. <!-- **/*.xml 代表 directory(src/main/resources/)目录以及所有子目录的xml文件-->
    55. <!--
    56. <exclude>**/*.xml</exclude>
    57. <exclude>**/*.properties</exclude>
    58. -->
    59. </excludes>
    60. <!-- 包含标签 -->
    61. <!--
    62. <includes>
    63. <include></include>
    64. </includes>
    65. -->
    66. </resource>
    67. </resources>
    68. </build>
  • 如何运行?
    打包时,自己肯定知道是生产环境还是部署环境,所以只需要在打包的时候加上参数即可,如下:
    打本地开发环境包mvn clean package -Pdev
    打部署上线环境包mvn clean package -Ppro
    打测试环境包mvn clean package -Ptest

  • 执行完命令后可以发现classes下的jdbc.properties变成了如下
    1. jdbc.driver=com.mysql.jdbc.Driver
    2. jdbc.url=jdbc:mysql://127.0.0.1:3306/pro
    3. jdbc.username=root
    4. jdbc.password=123456
    上面配置文件随着打包不同内容不同,会将filter-dev-env.properties、filter-pro-env.properties、filter-test-env.properties三个文件的内容分别注入

作者:编程界的小学生
链接:https://www.jianshu.com/p/5650e5738d30
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。