使用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

相關文章
相關標籤/搜索