天天看點

Maven:Maven Profile。針對不同環境的profile激活profileprofile的種類

不同環境的建構很可能是不同的,典型的情況就是資料庫的配置。除此之外,有些環境可能需要配置插件使用本地檔案,或者使用特殊版本的依賴,或者需要一個特殊的建構名稱。要想使得一個建構不做任何修改就能在任何環境下運作,往往是不可能的。為了能讓建構在各個環境下友善的移植,Maven引入了profile的概念。profile能夠在建構的時候修改POM的一個子集,或者添加額外的配置元素。使用者可以使用很多方式激活profile,以實作建構在不同環境下的移植。

針對不同環境的profile

下面為測試環境和産品環境的profile,如下所示。

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <db.driver>com.mysql.jdbc.Driver</db.driver>
            <db.url>jdbc:mysql://localhost:3306/test</db.url>
            <db.username>dev</db.username>
            <db.password>dev-pwd</db/password>
        </properties>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <db.driver>com.mysql.jdbc.Driver</db.driver>
            <db.url>jdbc:mysql://localhost:3306/test</db.url>
            <db.username>test</db.username>
            <db.password>test-pwd</db/password>
        </properties>
    </profile>
</profiles>
           

同樣的屬性在兩個profile中的值是不一樣的,dev profile 提供了開發環境資料庫的配置,而test profile提供的是測試環境資料庫的配置。

現在,開發人員可以在使用mvn指令的時候在後面加上-Pdev激活dev profile,而測試人員可以使用-Ptest激活test profile。

激活profile

為了盡可能友善使用者,Maven支援很多種激活Profile的方式。

指令行激活

使用者可以使用mvn指令行參數-P加上profile的id來激活profile,多個id之間以逗号分隔。例如,下面的指令激活了dev-x和dev-y兩個profile:

mvn clean install -Pdev -x,dev -y
           

settings檔案顯式激活

如果使用者希望某個profile預設一直處于激活狀态,就可以配置settings.xml檔案的activeProfiles元素,表示其配置的profile對于所有項目都處于激活狀态,如下所示。

<settings>
...
    <activeProfiles>
        <activeProfile>dev-x</activeProfile>
    </activeProfiles>
...
</settings>
           

系統屬性激活

使用者可以配置當某系統屬性存在的時候,自動激活profile,如下所示。

<profiles>
    <profile>
        <activation>
            <property>
                <name>test</name>
            </property>
        </activation>
    </profile>
...
</profiles>
           

可以進一步配置當某系統屬性test存在,且值等于x的時候激活profile,如下所示。

<profiles>
    <profile>
        <activation>
            <property>
                <name>test</name>
                <value>x</value>
            </property>
        </activation>
    </profile>
...
</profiles>
           

不要忘了,使用者可以在指令行聲明系統屬性。例如:

mvn clean install -Dtest=x
           

是以,這其實也是一種從指令行激活profile的方法,而且多個profile完全可以使用同一個系統屬性來激活。

作業系統環境激活

Profile還可以自動根據作業系統環境激活,如果建構在不同的作業系統有差異,使用者完全可以将這些差異寫進profile,然後配置他們自動基于作業系統環境激活,如下所示。

<profiles>
    <profile>
        <activation>
            <os>
                <name>Windows XP</name>
                <family>Windows</family>
                <arch>x86</arch>
                <version>5.1.2600</version>
            </os>
        </activation>
    ...
    </profile>
</profiles>
           

這裡family的值包括Windows、UNIX和Mac等,而其他幾項name、arch、version,使用者可以通過檢視環境中的系統屬性os.name、os.arch、os.version獲得。

檔案存在與否激活

Maven能夠根據項目中某個檔案存在與否來決定是否激活profile,如下所示。

<profiles>
    <profile>
        <activation>
            <file>
                <missing>x.properties</missing>
                <exists>y.properties</exists>
            </file>
        </activation>
        ...
    </profile>
</profiles>
           

預設激活

使用者可以在定義profile的時候指定其預設激活,如下所示。

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        ...
    </profile>
</profiles>
           

使用activeByDefault元素使用者可以指定profile自動激活。不過需要注意的是,如果POM中有任何一個profile通過以上其他任意一種方式被激活了,所有的預設激活配置都會失效。

如果項目中有很多的profile,他們的激活方式各異,使用者怎麼知道哪些profile被激活了呢?maven-help-plugin提供了一個目标幫助使用者了解目前激活的profile:

mvn help:active-profile
           

maven-help-plugin還有另外一個目标用來列出目前所有的profile:

mvn help:all-profiles
           

profile的種類

根據具體的需要,可以在以下位置聲明profile:

  • pom.xml:很顯然,pom.xml中聲明的profile隻對目前項目有效。
  • 使用者settings.xml:使用者目錄下.m2/settings.xml中的profile對本機上該使用者所有的Maven項目有效。
  • 全局settings.xml:Maven安裝目錄下conf/settings.xml中的profile對本機上所有的Maven項目有效。

為了不影響其他使用者且友善更新Maven,使用者應該選擇配置使用者範圍的settings.xml,避免修改全局範圍的settings.xml檔案。也正是因為這個原因,一般不會在全局的settings.xml檔案中添加profile。