使用Maven filter和profile隔离不一样环境的配置文件

##背景html

在平常的项目开发过程当中, 会有不少的配置文件, 而项目对应有多套环境(开发、测试、预发布、生产), 不一样的环境使用的配置属性又不同, 好比数据库链接属性、服务器日志文件路径、缓存服务器地址等等, 若是咱们每次打包部署到不一样的环境都要频繁的修改配置文件的话, 很是的麻烦, 并且势必会增长必定的风险。那么如何解决这个问题呢?数据库

##解决方案 使用 Maven 的 filtering 和 profile 功能, 咱们只须要在 pom 文件中作一些简单的配置就能够隔离不一样环境的配置文件, 很是方便。apache

##实现原理 ###filtering 主要用来替换项目中资源文件(.xml、.properties)当中由 ${...} 标记的变量, 好比 ${zk.hosts}。若是在配置配置文件中配置了 zk.hosts=a.b.c.d 的话, 那么使用 Maven 编译项目的时候, 会自动的把 ${zk.hosts} 替换为 a.b.c.d。 ###代码示例 新建一个用于测试的 Maven 项目, POM 文件内容以下:缓存

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jackiehff.codeproject</groupId>
    <artifactId>maven-sample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>maven-sample</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

</project>

在 src/main/resources 目录下新建一个 config.properties 文件, 文件内容以下:服务器

Hello ${your.name}!

####使用-D选项 在命令行编译时, 可使用 -D 选项指定变量的值, 以下:maven

mvn resources:resources -Dyour.name="world"

执行完以后 target/classes/config.properties 中文件内容为:ide

Hello world!

能够看到 your.name 变量的值变成了 world。 ####使用预约义项目属性 还能够在 POM 文件中的 <properties> 元素下指定自定义变量和它们的值。好比能够在 <properties> 元素下新增一个 <your.name> 元素, 并指定 <your.name> 元素的值。测试

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <your.name>world</your.name>
</properties>

执行 mvn resources:resources 命令, 能够发现 target/classes/config.properties 中文件内容也变成了:ui

Hello world!

####使用属性文件 能够把属性配置放置在一个单独的 .properties 文件中。例如咱们在 src/main/resources 目录下面建立一个 test.properties, 内容以下:url

your.name=world

接着须要在配置文件中指定 test.properties 的路径, 以下:

<build>
   <resources>
      <resource>
          <directory>src/main/resources</directory>
          <filtering>true</filtering>
      </resource>
   </resources>
   <filters>
      <filter>src/main/resources/test.properties</filter>
   </filters>
</build>

执行 mvn resources:resources 命令, 能够发现 target/classes/config.properties 中文件内容也变成了:

Hello world!

###profile Maven profile 可使用操做系统信息、jdk信息以及属性值等做为依据, 来激活相应的 profile。好比须要为开发环境和生产环境定义不一样的 your.name 属性值, 咱们在 src/main/resources 目录下建立两个属性文件 config-dev.properties 和 config-online.properties。

config-dev.properties 文件内容以下:

your.name=dev

config-online.properties 文件内容以下:

your.name=online

接着修改 POM 文件以下:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <filters>
        <filter>src/main/resources/config-${env}.properties</filter>
    </filters>
</build>

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <env>dev</env>
        </properties>
        <activation>
        <!-- 默认激活开发环境配制,使用config-dev.properties来替换 config.properties 文件中的 ${your.name} -->
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>

    <profile>
        <id>online</id>
        <properties>
            <env>online</env>
        </properties>
    </profile>
</profiles>

执行 mvn clean compile 命令, 能够看到 target/classes/config.properties 中文件内容变成了:

Hello dev!

在命令行编译项目时, 也可使用 -P 参数指定须要激活的 profile 的 id, 好比下面命令将会激活 id 为 dev 的 profile:

mvn clean compile -Pdev

若是想激活 id 为 online 的 profile, 只须要作以下修改:

mvn clean compile -Ponline

假如不指定 -P 参数的话,则会使用 activeByDefault=true 的 profile。

##参考资料 http://maven.apache.org/guides/introduction/introduction-to-profiles.html https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

相关文章
相关标签/搜索