【MAVEN】maven系列--pom.xml標(biāo)簽詳解

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

標(biāo)簽預(yù)覽

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

基本內(nèi)容設(shè)置

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

artifactId:項目的通用名稱

version:項目的版本

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

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

url:開發(fā)團隊官方地址 ,非必要

classifer:分類

對于以上基本標(biāo)簽,groupId,artifactId,version,packaging作為項目唯一坐標(biāo)

POM依賴關(guān)系設(shè)置

對于POM文件中的關(guān)系,主要有依賴,繼承,合成等關(guān)系。

依賴關(guān)系

<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這三個組合標(biāo)示依賴的具體工程。如果在中央倉庫中沒有的依賴包,需要自行導(dǎo)入到本地或私有倉庫中。

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

  1. 通過本地maven 進行配置安裝 使用maven install plugin。如:mvn install:intall-file -Dfile=non-maven-proj.jar -DgroupId=som.group -DartifactId=non-maven-proj -Dversion=1。
  1. 創(chuàng)建自己的repositories并且部署這個包,使用類似上面的deploy:deploy-file命令。

  2. 在代碼中配置scope為system,并且指定系統(tǒng)路徑。

dependency介紹

dependency下面包含眾多字標(biāo)簽。

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

scope:用來指定當(dāng)前包的依賴范圍

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

optional:設(shè)置指依賴是否可選,默認為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,子系統(tǒng)要繼承父類,也必須使用parent標(biāo)簽。對于子系統(tǒng)使用如下所示:

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

說明:

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

dependencyManagement:用于幫助管理chidren的dependencies,優(yōu)點就是可以集中管理版本。

modules:多模塊項目的標(biāo)簽,順序不重要,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文件常量定義區(qū),在文件中可以直接引用,如版本、編碼等。如下所示:

<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構(gòu)建設(shè)置

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

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

build模塊設(shè)置

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

directory:指定build target目標(biāo)的目錄,默認為$(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>

子標(biāo)簽介紹:

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

plugins配置

對于打包插件的相關(guān)配置在該模塊配置。樣例如下:

<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>

子標(biāo)簽說明:

  1. extensions:true or false, 決定是否要load這個plugin的extensions,默認為true。
  2. inherited:是否讓子pom繼承,ture or false 默認為true。
  3. configuration:通常用于私有不開源的plugin,不能夠詳細了解plugin的內(nèi)部工作原理,但使plugin滿足的properties
  4. dependencies:與pom基礎(chǔ)的dependencies的結(jié)構(gòu)和功能都相同,只是plugin的dependencies用于plugin,而pom的denpendencies用于項目本身。
  5. dependencies:排除一些用不到的dependency或者修改dependency的版本等。
  6. executions:plugin也有很多個目標(biāo),每個目標(biāo)具有不同的配置,executions就是設(shè)定plugin的目標(biāo)。
  7.  <!--executions 內(nèi)部標(biāo)簽示意-->
    
     <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區(qū)別是,這里的plugin是列出來,然后讓子pom來決定是否引用。

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

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

reporting設(shè)置

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> 

更多項目信息

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

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

description:項目描述。

url:項目url。

inceptionYear:創(chuàng)始年份。

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:開發(fā)者信息。樣例如下:

<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:環(huán)境配置信息,樣例如下:

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

repositories:倉庫配置信息,pom里面的倉庫與setting.xml里的倉庫功能是一樣,主要的區(qū)別在于,pom里的倉庫是個性化的。比如一家大公司里的setting文件是公用 的,所有項目都用一個setting文件,但各個子項目卻會引用不同的第三方庫,所以就需要在pom里設(shè)置自己需要的倉庫地址。
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> 

(本文完)
注:本文參考互聯(lián)網(wǎng)相關(guān)文章整理所得。

更多信息后續(xù)分專題講解

http://www.hanyahong.com

https://github.com/hanyahong

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容