maven pom.xml中的 build說明


在Maven的pom.xml文件中,Build相關配置包含兩個部分,一個是<build>,另一個是<reporting>,這里我們只介紹<build>。

 

1. 在Maven的pom.xml文件中,存在如下兩種<build>:

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"  
  2.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
  4.   ...  
  5.   <!-- "Project Build" contains elements of the BaseBuild set and the Build set-->  
  6.   <build>...</build>  
  7.    
  8.   <profiles>  
  9.     <profile>  
  10.       <!-- "Profile Build" contains elements of the BaseBuild set only -->  
  11.       <build>...</build>  
  12.     </profile>  
  13.   </profiles>  
  14. </project>  

說明:

 

一種<build>被稱為Project Build,即是<project>的直接子元素。另一種<build>被稱為Profile Build,即是<profile>的直接子元素。

Profile Build包含了基本的build元素,而Project Build還包含兩個特殊的元素,即各種<...Directory>和<extensions>。

 

2. Profile Build和Project Build共用的基本build元素

1) 示例如下:

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. <build>  
  2.   <defaultGoal>install</defaultGoal>  
  3.   <directory>${basedir}/target</directory>  
  4.   <finalName>${artifactId}-${version}</finalName>  
  5.   ...  
  6. </build>  

說明:

  • defaultGoal,執行構建時默認的goal或phase,如jar:jar或者package等
  • directory,構建的結果所在的路徑,默認為${basedir}/target目錄
  • finalName,構建的最終結果的名字,該名字可能在其他plugin中被改變

 

2) <resources>

資源往往不是代碼,無需編譯,而是一些properties或XML配置文件,構建過程中會往往會將資源文件從源路徑復制到指定的目標路徑。

<resources>給出各個資源在Maven項目中的具體路徑。示例如下:

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. <build>  
  2.   ...  
  3.   <filters>  
  4.     <filter>filters/filter1.properties</filter>  
  5.   </filters>  
  6.   <resources>  
  7.     <resource>  
  8.       <targetPath>META-INF/plexus</targetPath>  
  9.       <filtering>false</filtering>  
  10.       <directory>${basedir}/src/main/plexus</directory>  
  11.       <includes>  
  12.         <include>configuration.xml</include>  
  13.       </includes>  
  14.       <excludes>  
  15.         <exclude>**/*.properties</exclude>  
  16.       </excludes>  
  17.     </resource>  
  18.   </resources>  
  19.   <testResources>  
  20.     ...  
  21.   </testResources>  
  22.   ...  
  23. </build>  

說明:

  • resources,build過程中涉及的資源文件
    • targetPath,資源文件的目標路徑
    • filtering,構建過程中是否對資源進行過濾,默認false
    • directory,資源文件的路徑,默認位於${basedir}/src/main/resources/目錄下
    • includes,一組文件名的匹配模式,被匹配的資源文件將被構建過程處理
    • excludes,一組文件名的匹配模式,被匹配的資源文件將被構建過程忽略。同時被includes和excludes匹配的資源文件,將被忽略。
  • filters,給出對資源文件進行過濾的屬性文件的路徑,默認位於${basedir}/src/main/filters/目錄下。屬性文件中定義若干鍵值對。在構建過程中,對於資源文件中出現的變量(鍵),將使用屬性文件中該鍵對應的值替換。
  • testResources,test過程中涉及的資源文件,默認位於${basedir}/src/test/resources/目錄下。這里的資源文件不會被構建到目標構件中

 

3) <plugins>

<plugins>給出構建過程中所用到的插件。

 

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. <build>  
  2.   ...  
  3.   <plugins>  
  4.     <plugin>  
  5.       <groupId>org.apache.maven.plugins</groupId>  
  6.       <artifactId>maven-jar-plugin</artifactId>  
  7.       <version>2.6</version>  
  8.       <extensions>false</extensions>  
  9.       <inherited>true</inherited>  
  10.       <configuration>  
  11.         <classifier>test</classifier>  
  12.       </configuration>  
  13.       <dependencies>...</dependencies>  
  14.       <executions>...</executions>  
  15.     </plugin>  
  16.   </plugins>  
  17. </build>  

說明:

 

  • groupId
  • artifactId
  • version
  • extensions,是否加載該插件的擴展,默認false
  • inherited,該插件的configuration中的配置是否可以被(繼承該POM的其他Maven項目)繼承,默認true
  • configuration,該插件所需要的特殊配置,在父子項目之間可以覆蓋或合並
  • dependencies,該插件所特有的依賴類庫
  • executions,該插件的某個goal(一個插件中可能包含多個goal)的執行方式。一個execution有如下設置:
    • id,唯一標識
    • goals,要執行的插件的goal(可以有多個),如<goal>run</goal>
    • phase,插件的goal要嵌入到Maven的phase中執行,如verify
    • inherited,該execution是否可被子項目繼承
    • configuration,該execution的其他配置參數

 

 

4) <pluginManagement>

在<build>中,<pluginManagement>與<plugins>並列,兩者之間的關系類似於<dependencyManagement>與<dependencies>之間的關系。<pluginManagement>中也配置<plugin>,其配置參數與<plugins>中的<plugin>完全一致。只是,<pluginManagement>往往出現在父項目中,其中配置的<plugin>往往通用於子項目。子項目中只要在<plugins>中以<plugin>聲明該插件,該插件的具體配置參數則繼承自父項目中<pluginManagement>對該插件的配置,從而避免在子項目中進行重復配置。

 

3. Project Build特有的<...Directory>

往往配置在父項目中,供所有父子項目使用。示例如下:

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1.   <build>  
  2.     <sourceDirectory>${basedir}/src/main/java</sourceDirectory>  
  3.     <scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>  
  4.     <testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>  
  5.     <outputDirectory>${basedir}/target/classes</outputDirectory>  
  6.     <testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>  
  7.     ...  
  8.   </build>  
  9. </project>  

 

目錄可以使用絕對路徑,如示例所示。如果使用相對路徑,則所有的相對路徑都是在${basedir}目錄下。

 

4. Project Build特有的<extensions>

 

<extensions>是執行構建過程中可能用到的其他工具,在執行構建的過程中被加入到classpath中。

也可以通過<extensions>激活構建插件,從而改變構建的過程。

通常,通過<extensions>給出通用插件的一個具體實現,用於構建過程。

<extensions>的使用示例如下:

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. <build>  
  2.   ...  
  3.   <extensions>  
  4.     <extension>  
  5.       <groupId>org.apache.maven.wagon</groupId>  
  6.       <artifactId>wagon-ftp</artifactId>  
  7.       <version>1.0-alpha-3</version>  
  8.     </extension>  
  9.   </extensions>  
  10.   ...  
  11. </build>  
  12. t;/project>  


免責聲明!

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



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