【MAVEN】maven系列--pom.xml標簽詳解


pom文件作為MAVEN中重要的配置文件,對於它的配置是相當重要。文件中包含了開發者需遵循的規則、缺陷管理系統、組織、licenses、項目信息、項目依賴性等。下面將重點介紹一下該文件的基本組成與功能。

標簽預覽

<project>
	<modelVersion>4.0.0</modelVersion>
	<!-- 基礎設置 -->
	<groupId>...</groupId>
	<artifactId>...</artifactId>
	<version>...</version>
	<packaging>...</packaging>
	<name>...</name>
	<url>...</url>
	<dependencies>...</dependencies>
	<parent>...</parent>
	<dependencyManagement>...</dependencyManagement>
	<modules>...</modules>
	<properties>...</properties>
	<!--構建設置 -->
	<build>...</build>
	<reporting>...</reporting>
	<!-- 更多項目信息 -->
	<name>...</name>
	<description>...</description>
	<url>...</url>
	<inceptionYear>...</inceptionYear>
	<licenses>...</licenses>
	<organization>...</organization>
	<developers>...</developers>
	<contributors>...</contributors>
	<!-- 環境設置-->
	<issueManagement>...</issueManagement>
	<ciManagement>...</ciManagement>
	<mailingLists>...</mailingLists> 
	<scm>...</scm>
	<prerequisites>...</prerequisites>
	<repositories>...</repositories>
	<pluginRepositories>...</pluginRepositories>
	<distributionManagement>...</distributionManagement>
	<profiles>...</profiles>
</project>

基本內容設置

groupId:項目或者組織的唯一標志 ,如cn.gov.customs生成的相對路徑為:/cn/gov/customs

artifactId:項目的通用名稱

version:項目的版本

packaging:打包機制,如pom,jar,maven-plugin,ejb,war,ear,rar,par

name:用戶描述項目的名稱,無關緊要的東西,非必要

url:開發團隊官方地址 ,非必要

classifer:分類

對於以上基本標簽,groupId,artifactId,version,packaging作為項目唯一坐標

POM依賴關系設置

對於POM文件中的關系,主要有依賴,繼承,合成等關系。

依賴關系

<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.0</version>
		<type>jar</type>
		<scope>test</scope>
		<optional>true</optional>
	</dependency>
	
	<dependency>
	<groupId>com.alibaba.china.shared</groupId>
	<artifactId>alibaba.apollo.webx</artifactId>
	<version>2.5.0</version>
	<exclusions>
		<exclusion>
			<artifactId>org.slf4j.slf4j-api</artifactId>
			<groupId>com.alibaba.external</groupId>
		</exclusion>
		....
	</exclusions>
	......
</dependencies>

以上代碼說明:groupId, artifactId, version這三個組合標示依賴的具體工程。如果在中央倉庫中沒有的依賴包,需要自行導入到本地或私有倉庫中。

具體有三種方式,簡單提一提,具體后續出專題文章。

  1. 通過本地maven 進行配置安裝 使用maven install plugin。如:mvn install:intall-file -Dfile=non-maven-proj.jar -DgroupId=som.group -DartifactId=non-maven-proj -Dversion=1。

  2. 創建自己的repositories並且部署這個包,使用類似上面的deploy:deploy-file命令。

  3. 在代碼中配置scope為system,並且指定系統路徑。

dependency介紹

dependency下面包含眾多字標簽。

type:默認為jar類型,常用的類型有:jar,ejb-client,test-jar...,可設置plugins中的extensions值為true后在增加新類型。

scope:用來指定當前包的依賴范圍

  • compile(編譯范圍),是默認的范圍,編譯范圍依賴在所有的classpath中可用,同時它們也會被打包。
  • provided(已提供范圍),只有在當JDK或者一個容器已提供該依賴之后才使用。
  • runtime(運行時范圍),在運行和測試系統的時候需要。
  • test(測試范圍),在一般的 編譯和運行時都不需要。
  • system(系統范圍),與provided類似

optional:設置指依賴是否可選,默認為false,即子項目默認都繼承,為true,則子項目必需顯示的引入,與dependencyManagement里定義的依賴類似 。

exclusions:如果X需要A,A包含B依賴,那么X可以聲明不要B依賴,只要在exclusions中聲明exclusion。

exclusion:將B從依賴樹中刪除,如上配置,alibaba.apollo.webx不想使用com.alibaba.external ,但是alibaba.apollo.webx是集成了com.alibaba.external,r所以就需要排除掉。

parent:如果一個工程作為父類工程,那就必須添加pom,子系統要繼承父類,也必須使用parent標簽。對於子系統使用如下所示:

<parent> 
	<groupId>org.codehaus.mojo</groupId> 
	<artifactId>my-parent</artifactId> 
	<version>2.0</version> 
	<relativePath>../my-parent</relativePath> 
</parent>

說明:

relativePath:為可選項,maven會首先搜索該地址,然后再搜索遠程倉庫。

dependencyManagement:用於幫助管理chidren的dependencies,優點就是可以集中管理版本。

modules:多模塊項目的標簽,順序不重要,MAVEN會自動拓展排序。使用如下所示:

<!--子模塊-->
<modules>
    <module>ygb-service-config</module>
    <module>ygb-service-bus</module>
    <module>ygb-service-policy-center</module>
    <module>ygb-service-letter-of-indemnity</module>
    <module>ygb-service-authentication-center</module>
    <module>ygb-service-eureka-center</module>
    <module>ygb-service-api-gateway</module>
    <module>ygb-service-demo</module>
    <module>ygb-service-cache-ehcache</module>
    <module>ygb-service-maven</module>
</modules>

properties:POM文件常量定義區,在文件中可以直接引用,如版本、編碼等。如下所示:

<properties>
  <file.encoding>UTF-8</file_encoding>
  <java.source.version>1.8</java_source_version>
  <java.target.version>1.8</java_target_version>
</properties>	

使用方式:${file.encoding}

MAVEN構建設置

這部分主要是對項目的構建過程進行配置,包括打包的方式、插件的安裝等。配置如下所示:

<!-- 構建管理 -->
<build>
    <!--構建工具插件管理-->
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

build模塊設置

defaultGoal :默認的目標,必須跟命令行上的參數相同,如:jar:jar,或者與時期parse相同,例如install。

directory:指定build target目標的目錄,默認為$(basedir}/target,即項目根目錄下的target。

finalName:指定去掉后綴的工程名字,例如:默認為${artifactId}-${version}。

filters:定義指定filter屬性的位置,例如filter元素賦值filters/filter1.properties,那么這個文件里面就可以定義name=value對,這個name=value對的值就可以在工程pom中通過${name}引用,默認的filter目錄是${basedir}/src/main/fiters/。

resources:描述工程中各種文件資源的位置 。

<resource> 
	<targetPath>META-INF/plexus</targetPath> 
	<filtering>false</filtering> 
	<directory>${basedir}/src/main/plexus</directory> 
	<includes> 
		<include>configuration.xml</include> 
	</includes> 
	<excludes> 
		<exclude>**/*.properties</exclude> 
	</excludes> 
</resource>

子標簽介紹:

  1. targetPath:指定build資源具體目錄,默認是base directory。
  2. filtering:指定是否將filter文件的變量值在這個resource文件有效。即上面說的filters里定義的*.property文件。例如上面就指定那些變量值在configuration文件無效,設置為false。
  3. directory:指定屬性文件的目錄,build的過程需要找到它,並且將其放到targetPath下,默認的directory是${basedir}/src/main/resources
  4. includes:指定包含文件的patterns,符合樣式並且在directory目錄下的文件將會包含進project的資源文件。
  5. excludes:指定不包含在內的patterns。
  6. testResources:包含測試資源元素。默認的測試資源路徑是${basedir}/src/test/resources,測試資源是不部署的。

plugins配置

對於打包插件的相關配置在該模塊配置。樣例如下:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-jar-plugin</artifactId> 
    <version>2.0</version> 
    <extensions>false</extensions> 
    <inherited>true</inherited> 
    <configuration> 
      <classifier>test</classifier> 
    </configuration> 
    <dependencies>...</dependencies> 
    <executions>...</executions> 
</plugin>

子標簽說明:

  1. extensions:true or false, 決定是否要load這個plugin的extensions,默認為true。
  2. inherited:是否讓子pom繼承,ture or false 默認為true。
  3. configuration:通常用於私有不開源的plugin,不能夠詳細了解plugin的內部工作原理,但使plugin滿足的properties
  4. dependencies:與pom基礎的dependencies的結構和功能都相同,只是plugin的dependencies用於plugin,而pom的denpendencies用於項目本身。
  5. dependencies:排除一些用不到的dependency或者修改dependency的版本等。
  6. executions:plugin也有很多個目標,每個目標具有不同的配置,executions就是設定plugin的目標。
  7.  <!--executions 內部標簽示意-->
    
     <execution> 
         <id>echodir</id> 
         <goals> 
           <goal>run</goal> 
         </goals> 
         <phase>verify</phase> 
         <inherited>false</inherited> 
         <configuration> 
           <tasks> 
             <echo>Build Dir: ${project.build.directory}</echo> 
           </tasks> 
         </configuration> 
     </execution> 
    

pluginManagement配置

pluginManagement的作用類似於denpendencyManagement,只是denpendencyManagement是用於管理項目jar包依賴,pluginManagement是用於管理plugin。樣例如下:

<pluginManagement> 
  <plugins> 
    <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-jar-plugin</artifactId> 
      <version>2.2</version> 
      <executions> 
        <execution> 
          <id>pre-process-classes</id> 
          <phase>compile</phase> 
          <goals> 
            <goal>jar</goal> 
          </goals> 
          <configuration> 
            <classifier>pre-process</classifier> 
          </configuration> 
        </execution> 
      </executions> 
    </plugin> 
  </plugins> 
</pluginManagement> 

與pom build里的plugins區別是,這里的plugin是列出來,然后讓子pom來決定是否引用。

子pom引用方法: 在pom的build里的plugins引用:

<plugins> 
  <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-jar-plugin</artifactId> 
  </plugin> 
</plugins>

reporting設置

reporting包含site生成階段的一些元素,某些maven plugin可以生成reports並且在reporting下配置。reporting里面的reportSets和build里面的executions的作用都是控制pom的不同粒度去控制build的過程,我們不單要配置plugins,還要配置那些plugins單獨的goals。樣例如下:

<reporting> 
    <plugins> 
      <plugin> 
        ... 
        <reportSets> 
          <reportSet> 
            <id>sunlink</id> 
            <reports> 
              <report>javadoc</report> 
            </reports> 
            <inherited>true</inherited> 
            <configuration> 
              <links> 
                <link>http://java.sun.com/j2se/1.5.0/docs/api/</link> 
              </links> 
            </configuration> 
          </reportSet> 
        </reportSets> 
      </plugin> 
    </plugins> 
  </reporting> 

更多項目信息

這塊是一些非必要的設置信息,但是作為項目來講、版權來講,也會很重要的信息。

name:項目除了artifactId外,可以定義多個名稱。

description:項目描述。

url:項目url。

inceptionYear:創始年份。

Licenses樣例如下

<licenses>
  <license>
    <name>Apache 2</name>
    <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
    <distribution>repo</distribution>
    <comments>A business-friendly OSS license</comments>
  </license>
</licenses>

organization:組織信息。

developers:開發者信息。樣例如下:

<developers>
    <developer>
      <id>hanyahong</id>
      <name>hanyahong</name>
      <email>ceo@hanyahong.com</email>
      <url>http://www.hanyahong.com</url>
      <organization>hanyahong</organization>
      <organizationUrl>http://www.hanyahong.com</organizationUrl>
      <roles>
        <role>architect</role>
        <role>developer</role>
      </roles>
      <timezone>-6</timezone>
      <properties>
        <picUrl>http://www.hanyahong.com/test</picUrl>
      </properties>
    </developer>
  </developers>

issueManagement:環境配置信息,樣例如下:

<issueManagement> 
	<system>Bugzilla</system> 
	<url>http://hanyahong.com/</url> 
</issueManagement> 

repositories:倉庫配置信息,pom里面的倉庫與setting.xml里的倉庫功能是一樣,主要的區別在於,pom里的倉庫是個性化的。比如一家大公司里的setting文件是公用 的,所有項目都用一個setting文件,但各個子項目卻會引用不同的第三方庫,所以就需要在pom里設置自己需要的倉庫地址。
repositories:要成為maven2的repository artifact,必須具有pom文件在$BASE_REPO/groupId/artifactId/version/artifactId-version.pom
BASE_REPO可以是本地,也可以是遠程的。repository元素就是聲明那些去查找的repositories
默認的central Maven repository在http://repo1.maven.org/maven2/。樣例如下:

<repositories> 
    <repository> 
      <releases> 
        <enabled>false</enabled> 
        <updatePolicy>always</updatePolicy> 
        <checksumPolicy>warn</checksumPolicy> 
      </releases> 
      <snapshots> 
        <enabled>true</enabled> 
        <updatePolicy>never</updatePolicy> 
        <checksumPolicy>fail</checksumPolicy> 
      </snapshots> 
      <id>codehausSnapshots</id> 
      <name>Codehaus Snapshots</name> 
      <url>http://snapshots.maven.codehaus.org/maven2</url> 
      <layout>default</layout> 
    </repository> 
  </repositories> 

(本文完)
注:本文參考互聯網相關文章整理所得。

更多信息后續分專題講解

http://www.hanyahong.com

https://github.com/hanyahong


免責聲明!

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



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