在eclipse激活maven profile配置


 profile簡介

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

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.           
  12.         <profile>  
  13.              <id>profileTest2</id>  
  14.              <properties>  
  15.                     <hello>andy</hello>  
  16.              </properties>  
  17.         </profile>  
  18.  </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

Xml代碼  
  1. <profiles>  
  2.        <profile>  
  3.             <id>profileTest1</id>  
  4.             <properties>  
  5.                    <hello>world</hello>  
  6.             </properties>  
  7.        </profile>  
  8.          
  9.        <profile>  
  10.             <id>profileTest2</id>  
  11.             <properties>  
  12.                    <hello>andy</hello>  
  13.             </properties>  
  14.        </profile>  
  15. </profiles>  

 

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

Xml代碼  
  1. <activeProfiles>  
  2.      <activeProfile>profileTest1</activeProfile>  
  3. </activeProfiles>  

 

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

Xml代碼  
  1. <activeProfiles>  
  2.      <activeProfile>profileTest1</activeProfile>  
  3.      <activeProfile>profileTest2</activeProfile>  
  4. </activeProfiles>  

 

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

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

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

Xml代碼  
  1. <profiles>  
  2.        <profile>  
  3.               <id>profileTest1</id>  
  4.               <properties>  
  5.                      <hello>world</hello>  
  6.               </properties>  
  7.        </profile>  
  8.        <profile>  
  9.               <id>profileTest2</id>  
  10.               <properties>  
  11.                      <hello>andy</hello>  
  12.               </properties>  
  13.        </profile>  
  14. <profiles>  

 

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

Cmd代碼  
  1. mvn package –P profileTest1  

 

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

Cmd代碼  
  1. 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

Xml代碼  
  1. <profiles>  
  2.        <profile>  
  3.               <id>profileTest1</id>  
  4.               <jdk>1.5</jdk>  
  5.        </profile>  
  6. <profiles>  

 

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

Xml代碼  
  1. <profiles>  
  2.        <profile>  
  3.               <id>profileTest1</id>  
  4.               <jdk>[1.4,1.7)</jdk>  
  5.        </profile>  
  6. <profiles>  

 

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

4.4.4.2根據操作系統來激活profile

Xml代碼  
  1. <profiles>  
  2.        <profile>  
  3.             <id>profileTest1</id>  
  4.             <activation>  
  5.               <os>  
  6.                    <name>Windows XP</name>  
  7.                    <family>Windows</family>  
  8.                    <arch>x86</arch>  
  9.                    <version>5.1.2600</version>  
  10.               </os>  
  11.             </activation>  
  12.        </profile>  
  13. </profiles>  

 

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

4.4.4.3根據系統屬性來激活profile

Xml代碼  
  1. <profiles>  
  2.        <profile>  
  3.             <id>profileTest1</id>  
  4.             <activation>  
  5.               <property>  
  6.                    <name>hello</name>  
  7.                    <value>world</value>  
  8.               </property>  
  9.             </activation>  
  10.        </profile>  
  11. </profiles>  

 

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

Cmd代碼  
  1. mvn package –Dhello=world  

 

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

Xml代碼  
  1. <profiles>  
  2.        <profile>  
  3.             <id>profileTest1</id>  
  4.             <activation>  
  5.               <property>  
  6.                    <name>hello</name>  
  7.               </property>  
  8.             </activation>  
  9.        </profile>  
  10. </profiles>  

 

4.4.4.4根據文件是否存在激活profile

Xml代碼  
  1. <profiles>  
  2.        <profile>  
  3.             <id>profileTest1</id>  
  4.             <activation>  
  5.               <file>  
  6.                    <exists>target</exists>  
  7.               </file>  
  8.             </activation>  
  9.        </profile>  
  10. </profiles>  

 

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

Xml代碼  
  1. <profiles>  
  2.        <profile>  
  3.             <id>profileTest1</id>  
  4.             <activation>  
  5.               <file>  
  6.                    <missing>target</missing>  
  7.               </file>  
  8.             </activation>  
  9.        </profile>  
  10. </profiles>  

 

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

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

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

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

Xml代碼  
  1. <profiles>  
  2.        <profile>  
  3.             <id>profileTest1</id>  
  4.             <activation>  
  5.               <file>  
  6.                    <missing>target</missing>  
  7.               </file>  
  8.             </activation>  
  9.        </profile>  
  10. </profiles>  
  11.   
  12. <activeProfiles>  
  13.      <activeProfile>profileTest1</activeProfile>  
  14. </activeProfiles>  

 

       這個時候我們可以看到,我們已經定義了profileTest1始終為激活狀態,這個時候我們使用mvn help:active-profiles查看處於激活狀態的profile時,就會打印出如下內容:

 

How to activate maven profile inside eclipse

Normally maven is use for project dependency management and lifecycle, so there are several developers working on it. Each has its own development environment either in windows, linux or mac. To do this we create several profiles for each developer in pom.xml. 

 <profiles>
  <profile>
   <id>windows</id>
   <properties>
    <jboss.home>C:/manaty/jboss-4.2.3.GA</jboss.home>
   </properties>
  </profile>
  <profile>
   <id>linux</id>
   <properties>
    <jboss.home>/home/project/jboss-4.2.3.GA</jboss.home>
   </properties>
  </profile>
 </profiles>

And then invoke mvn with the -P argument. Example: 

mvn install -Pwindows
mvn install -Plinux

How could we do the same inside eclipse. There are 2 ways: 1.) Right click on the project->click properties->select maven menu



 

If in case maven is not present, right click on project->Maven->Enable Maven Dependency 2.) Right click on project->Run As->Run Configurations->Maven Build->Add a new build (set profile and goal) 



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM