Maven的profiles

Profile做用

Profile能讓你爲一個特殊的環境自定義一個特殊的構建;profile使得不一樣環境間構建的可移植性成爲可能。 Maven中的profile是一組可選的配置,能夠用來設置或者覆蓋配置默認值。有了profile,你就能夠爲不一樣的環境定製構建。profile能夠在pom.xml中配置,並給定一個id。而後你就能夠在運行Maven的時候使用的命令行標記告訴Maven運行特定profile中的目標。ui

一個Profiles下面容許出現的元素:操作系統

<project>
    <profiles>
        <profile>
            <build>
                <defaultGoal>...</defaultGoal>
                <finalName>...</finalName>
                <resources>...</resources>
                <testResources>...</testResources>
                <plugins>...</plugins>
            </build>
            <reporting>...</reporting>
            <modules>...</modules>
            <dependencies>...</dependencies>
            <dependencyManagement>...</dependencyManagement>
            <distributionManagement>...</distributionManagement>
            <repositories>...</repositories>
            <pluginRepositories>...</pluginRepositories>
            <properties>...</properties>
        </profile>
    </profiles>
</project>

Profile激活

使用activeByDefault設置激活命令行

<profiles> 
    <profile> 
        <id>profileTest1</id> 
        <properties> 
            <hello>world</hello> 
        </properties> 
        <activation> 
            <activeByDefault>true</activeByDefault> 
        </activation> 
    </profile> 

    <profile> 
        <id>profileTest2</id> 
        <properties> 
            <hello>andy</hello> 
        </properties> 
    </profile> 
</profiles>

指定activeByDefault爲true的時候就表示當沒有指定其餘profile爲激活狀態時,該profile就默認會被激活。 因此當咱們調用mvn package的時候上面的profileTest1將會被激活,可是當咱們使用mvn package –P profileTest2的時候將激活profileTest2,而這個時候profileTest1將不會被激活。code

使用-P參數顯示的激活一個profilexml

咱們在進行Maven操做時就可使用-P參數顯示的指定當前激活的是哪個profile了。好比咱們須要在對項目進行打包的時候使用id爲profileTest1的profile,咱們就能夠這樣作:ci

mvn package –P profileTest1

當咱們使用activeByDefault或settings.xml中定義了處於激活的profile,可是當咱們在進行某些操做的時候又不想它處於激活狀態,這個時候咱們能夠這樣作:it

Mvn package –P !profileTest1

這裏假設profileTest1是在settings.xml中使用activeProfile標記的處於激活狀態的profile,那麼當咱們使用「-P !profile」的時候就表示在當前操做中該profile將不處於激活狀態。io

根據環境來激活profiletest

profile一個很是重要的特性就是它能夠根據不一樣的環境來激活,好比說根據操做系統的不一樣激活不一樣的profile,也能夠根據jdk版本的不一樣激活不一樣的profile,等等。module

<profiles> 
    <profile> 
        <id>profileTest1</id> 
        <jdk>1.5</jdk> 
    </profile> 
<profiles>

查看當前處於激活狀態的profile

咱們能夠同時定義多個profile,那麼在創建項目的過程當中,到底激活的是哪個profile呢?Maven爲咱們提供了一個指令能夠查看當前處於激活狀態的profile都有哪些,這個指定就是mvn help:active-profiles。

相關文章
相關標籤/搜索