天天看點

Maven簡介(三)——profile介紹

4       profile介紹

4.1     profile簡介

profile可以讓我們定義一系列的配置資訊,然後指定其激活條件。這樣我們就可以定義多個profile,然後每個profile對應不同的激活條件和配置資訊,進而達到不同環境使用不同配置資訊的效果。比如說,我們可以通過profile定義在jdk1.5以上使用一套配置資訊,在jdk1.5以下使用另外一套配置資訊;或者有時候我們可以通過作業系統的不同來使用不同的配置資訊,比如windows下是一套資訊,linux下又是另外一套資訊,等等。具體的激活條件有哪些我在後文會講到。

4.2     profile的定義位置

對于使用Maven3,我們可以有多個地方定義profile。定義的地方不同,它的作用範圍也不同。

(1)    針對于特定項目的profile配置我們可以定義在該項目的pom.xml中。

(2)    針對于特定使用者的profile配置,我們可以在使用者的settings.xml檔案中定義profile。該檔案在使用者家目錄下的“.m2”目錄下。

(3)    全局的profile配置。全局的profile是定義在Maven安裝目錄下的“conf/settings.xml”檔案中的。

4.3     profile中能定義的資訊

profile中能夠定義的配置資訊跟profile所處的位置是相關的。以下就分兩種情況來讨論,一種是定義在settings.xml中,另一種是定義在pom.xml中。

4.3.1  profile定義在settings.xml中

當profile定義在settings.xml中時意味着該profile是全局的,它會對所有項目或者某一使用者的所有項目都産生作用。因為它是全局的,是以在settings.xml中隻能定義一些相對而言範圍寬泛一點的配置資訊,比如遠端倉庫等。而一些比較細緻一點的需要根據項目的不同來定義的就需要定義在項目的pom.xml中。具體而言,能夠定義在settings.xml中的資訊有<repositories>、<pluginRepositories>和<properties>。定義在<properties>裡面的鍵值對可以在pom.xml中使用。

4.3.2  profile定義在pom.xml中

定義在pom.xml中的profile可以定義更多的資訊。主要有以下這些:

l  <repositories>

l  <pluginRepositories>

l  <dependencies>

l  <plugins>

l  <properties>

l  <dependencyManagement>

l  <distributionManagement>

l  還有build元素下面的子元素,主要包括:

<defaultGoal>

<resources>

<testResources>

<finalName>

4.4     profile的激活方式

Maven給我們提供了多種不同的profile激活方式。比如我們可以使用-P參數顯示的激活一個profile,也可以根據環境條件的設定讓它自動激活等。下面将對它們一一進行介紹:

4.4.1  使用activeByDefault設定激活

先看下面一個配置

Xml代碼  

  1. <profiles>  
  2.         <profile>  
  3.              <id>profileTest1</id>  
  4.              <properties>  
  5.                     <hello>world</hello>  
  6.              </properties>  
  7.              <activation>  
  8.                     <activeByDefault>true</activeByDefault>  
  9.              </activation>  
  10.         </profile>  
  11.              <id>profileTest2</id>  
  12.                     <hello>andy</hello>  
  13.  </profiles>  
<profiles>
         <profile>
              <id>profileTest1</id>
              <properties>
                     <hello>world</hello>
              </properties>
              <activation>
                     <activeByDefault>true</activeByDefault>
              </activation>
         </profile>
       
&lt;profile&gt;
          &lt;id&gt;profileTest2&lt;/id&gt;
          &lt;properties&gt;
                 &lt;hello&gt;andy&lt;/hello&gt;
          &lt;/properties&gt;
     &lt;/profile&gt;
           
</profiles>

        我們可以在profile中的activation元素中指定激活條件,當沒有指定條件,然後指定activeByDefault為true的時候就表示當沒有指定其他profile為激活狀态時,該profile就預設會被激活。是以當我們調用mvn package的時候上面的profileTest1将會被激活,但是當我們使用mvn package –P profileTest2的時候将激活profileTest2,而這個時候profileTest1将不會被激活。

4.4.2  在settings.xml中使用activeProfiles指定處于激活狀态的profile

我們可以在settings.xml中使用activeProfiles來指定需要激活的profile,這種方式激活的profile将所有情況下都處于激活狀态。比如現在我們定義了如下兩個profile

  1.        <profile>  
  2.             <id>profileTest1</id>  
  3.             <properties>  
  4.                    <hello>world</hello>  
  5.             </properties>  
  6.        </profile>  
  7.             <id>profileTest2</id>  
  8.                    <hello>andy</hello>  
  9. </profiles>  
<profiles>
         <profile>
              <id>profileTest1</id>
              <properties>
                     <hello>world</hello>
              </properties>
         </profile>
       
&lt;profile&gt;
          &lt;id&gt;profileTest2&lt;/id&gt;
          &lt;properties&gt;
                 &lt;hello&gt;andy&lt;/hello&gt;
          &lt;/properties&gt;
     &lt;/profile&gt;
           
</profiles>

       這裡的profile可以是定義在settings.xml中的,也可以是定義在pom.xml中的。這個時候如果我們需要指定profileTest1為激活狀态,那麼我們就可以在settings.xml中定義activeProfiles,具體定義如下:

  1. <activeProfiles>  
  2.      <activeProfile>profileTest1</activeProfile>  
  3. </activeProfiles>  
<activeProfiles>
       <activeProfile>profileTest1</activeProfile>
  </activeProfiles>
      

       考慮這樣一種情況,我們在activeProfiles下同時定義了多個需要激活的profile。這裡還拿上面的profile定義來舉例,我們定義了同時激活profileTest1和profileTest2。

  1.      <activeProfile>profileTest2</activeProfile>  
<activeProfiles>
       <activeProfile>profileTest1</activeProfile>
       <activeProfile>profileTest2</activeProfile>
  </activeProfiles>
      

       從profileTest1和profileTest2我們可以看出它們共同定義了屬性hello。那麼這個時候我在pom.xml中使用屬性hello的時候,它到底取的哪個值呢?是根據activeProfile定義的順序,後面的覆寫前面的嗎?根據我的測試,答案是非也,它是根據profile定義的先後順序來進行覆寫取值的,然後後面定義的會覆寫前面定義的。

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

假設我們現在有如下定義的profiles

  1.               <id>profileTest1</id>  
  2.               <properties>  
  3.                      <hello>world</hello>  
  4.               </properties>  
  5.               <id>profileTest2</id>  
  6.                      <hello>andy</hello>  
<profiles>
       <profile>
              <id>profileTest1</id>
              <properties>
                     <hello>world</hello>
              </properties>
       </profile>
       <profile>
              <id>profileTest2</id>
              <properties>
                     <hello>andy</hello>
              </properties>
       </profile>
<profiles>
      

       那麼當我們在進行Maven操作時就可以使用-P參數顯示的指定目前激活的是哪一個profile了。比如我們需要在對項目進行打包的時候使用id為profileTest1的profile,我們就可以這樣做:

Cmd代碼  

  1. mvn package –P profileTest1  
mvn package –P profileTest1      

       當我們使用activeByDefault或settings.xml中定義了處于激活的profile,但是當我們在進行某些操作的時候又不想它處于激活狀态,這個時候我們可以這樣做:

  1. Mvn package –P !profileTest1  
Mvn package –P !profileTest1      

       這裡假設profileTest1是在settings.xml中使用activeProfile标記的處于激活狀态的profile,那麼當我們使用“-P !profile”的時候就表示在目前操作中該profile将不處于激活狀态。

4.4.4根據環境來激活profile

profile一個非常重要的特性就是它可以根據不同的環境來激活,比如說根據作業系統的不同激活不同的profile,也可以根據jdk版本的不同激活不同的profile,等等。

4.4.4.1根據jdk來激活profile

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

       上面情況表示在jdk為1.5版本系列的時候激活profileTest1。

  1.               <jdk>[1.4,1.7)</jdk>  
<profiles>
       <profile>
              <id>profileTest1</id>
              <jdk>[1.4,1.7)</jdk>
       </profile>
<profiles>
      

       上面的情況表示在jdk為1.4、1.5和1.6的時候激活profileTest1。

4.4.4.2根據作業系統來激活profile

  1.             <activation>  
  2.               <os>  
  3.                    <name>Windows XP</name>  
  4.                    <family>Windows</family>  
  5.                    <arch>x86</arch>  
  6.                    <version>5.1.2600</version>  
  7.               </os>  
  8.             </activation>  
<profiles>
         <profile>
              <id>profileTest1</id>
              <activation>
                <os>
                     <name>Windows XP</name>
                     <family>Windows</family>
                     <arch>x86</arch>
                     <version>5.1.2600</version>
                </os>
              </activation>
         </profile>
  </profiles>
      

       上面的情況就是根據作業系統的類型來激活profileTest1。

4.4.4.3根據系統屬性來激活profile

  1.               <property>  
  2.                    <name>hello</name>  
  3.                    <value>world</value>  
  4.               </property>  
<profiles>
         <profile>
              <id>profileTest1</id>
              <activation>
                <property>
                     <name>hello</name>
                     <value>world</value>
                </property>
              </activation>
         </profile>
  </profiles>
      

上面的profileTest1将在提供了系統屬性hello,并且其值為world的時候激活。下面的做法可以激活profileTest1。

  1. mvn package –Dhello=world  
mvn package –Dhello=world      

       當是下面的這種定義形式時,profileTest1将在指定了系統屬性hello,且其值為任意值的時候被激活。

<profiles>
         <profile>
              <id>profileTest1</id>
              <activation>
                <property>
                     <name>hello</name>
                </property>
              </activation>
         </profile>
  </profiles>
      

4.4.4.4根據檔案是否存在激活profile

  1.               <file>  
  2.                    <exists>target</exists>  
  3.               </file>  
<profiles>
         <profile>
              <id>profileTest1</id>
              <activation>
                <file>
                     <exists>target</exists>
                </file>
              </activation>
         </profile>
  </profiles>
      

上面的定義表示當存在target檔案時激活profileTest1。

  1.                    <missing>target</missing>  
<profiles>
         <profile>
              <id>profileTest1</id>
              <activation>
                <file>
                     <missing>target</missing>
                </file>
              </activation>
         </profile>
  </profiles>
      

       上面的定義表示當不存在target檔案時激活profileTest1。

4.5     檢視目前處于激活狀态的profile

我們可以同時定義多個profile,那麼在建立項目的過程中,到底激活的是哪一個profile呢?Maven為我們提供了一個指令可以檢視目前處于激活狀态的profile都有哪些,這個指定就是mvn help:active-profiles。

現在假設我們的settings.xml檔案中有如下profile的定義:

<profiles>
         <profile>
              <id>profileTest1</id>
              <activation>
                <file>
                     <missing>target</missing>
                </file>
              </activation>
         </profile>
  </profiles>

<activeProfiles>

<activeProfile>profileTest1</activeProfile>

</activeProfiles>

      

       這個時候我們可以看到,我們已經定義了profileTest1始終為激活狀态,這個時候我們使用mvn help:active-profiles檢視處于激活狀态的profile時,就會列印出如下内容:

Maven簡介(三)——profile介紹

原文位址:https://www.iteye.com/blog/elim-1900568