pom.xml詳解

1.pom.xml是什麼

pom是Project Object Model(項目對象模型)的縮寫,是Maven中的項目文件,可用於管理與配置依賴,組織信息,項目受權,遠程倉庫等等.一個Maven項目,能夠沒有任何代碼,但不能沒有pom.xml.java

2.基本配置

(1)<project>

<project>是pom.xml的根元素,包含了一些約束信息.web

<project 
xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
</project>

(2)<modelVersion>

<modelVersion>4.0.0</modelVersion>

pom的版本,這是Maven 2&3惟一支持的pom版本,並且不能忽略.spring

(3)Maven座標

<groupId>,<artifactId>與<version>標識了倉庫中的一個特定位置,叫項目座標.三個屬性告訴了Maven項目中的一個特定版本,讓Maven知道如何處理它們以及在生命週期內的哪一階段須要它們.shell

A.<groupId>

<groupId>表示項目所屬的組,一般是一個公司或者組織的名稱,如org.springframework.apache

B.<artifactId>

<artifactId>表示項目的惟一標識.bash

C.<version>

<version>表示項目的版本號,一般來講項目的版本號分紅三段: 主版本號.次版本號.修訂版本號服務器

  • 主版本號:表明架構變更或者不兼容的實現.
  • 次版本號:兼容性修改,功能加強.
  • 修訂版本號:bug修復.

版本號的後綴意味着項目的不一樣階段:架構

  • SNAPSHOT:開發中的版本
  • RELEASE:正式發佈版
  • M1,M2:M指里程碑,表示即將發佈
  • RC:Release Candidate,發佈候選
  • GA:General Availablity,基本可用版本

(4)<packaging>

打包類型,沒有提供的話默認值爲jar,常見的有jar與war,也能夠取值:app

  • maven-plugin
  • pom
  • ejb
  • ear
  • rar

(5)POM 關係

Maven的一個強大之處是處理項目關係的方式,能夠經過一個公共的本地倉庫去解決問題.maven

A.依賴

POM的基礎就是依賴列表,Maven下載與在編譯時連接依賴與其餘所須要的目標,並且能夠處理傳遞性依賴,使列表能夠專一於項目所需的依賴.依賴放在<dependencies>裏面,包含若干個<dependency>.

<dependencies>
	<dependency>
		....
	</dependency>
	<dependency>
		....
	</dependency>
</dependencies>

一個<dependency>一般包含:

a.<groupId>與<artifactId>

對應項目座標

b.<version>

版本

c.<classifier>

可用於配置不一樣jdk的<depenency>,好比讓一個<dependency>同時支持jdk8與jdk11,能夠選擇使用哪個<classifier>,方便在不一樣jdk中使用.

d.<type>

對應的依賴類型,默認爲jar,一般對應與<packaging>.

e.<scope>

scope表示類庫與項目的關係,能夠取如下5個值:

  • compile:默認值,編譯依賴使其在全部類路徑中可用,並且這些依賴項會傳遞到其餘依賴項目,在編譯和打包時都須要此類庫.
  • provided:相似compile,可是指望JDK或一個容器會在運行時提供,僅在編譯和測試類路徑上可用,不可傳遞.
  • runtime:在運行時與測試類路徑中可用,在編譯類路徑中不可用.
  • test:測試編譯與執行階段可用,不可傳遞.
  • system:相似於provided,但必須顯式提供jar包.

f.<systemPath>

當<scope>爲system才須要這個,不然(當<scope>不爲system時)會構建失敗.路徑必須爲絕對路徑.

g.<optional>

標記依賴的可選狀態.

h.<exclusions>

排除不須要的依賴,包含子元素<exclusion>,每一個<exclusion>都包含<groupId>與<artifactId>.

B.繼承

使用<parent>指定須要繼承的pom.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
</parent>

子pom會繼承父pom的<groupId>,<version>,<build>等衆多屬性,具體包括:

  • groupId
  • version
  • description
  • url
  • inceptionYear
  • organization
  • licenses
  • developers
  • contributors
  • mailingLists
  • scm
  • issueManagement
  • ciManagement
  • properties
  • dependencyManagement
  • dependencies
  • repositories
  • pluginRepositories
  • build
  • reporting
  • profiles

但不能繼承:

  • artifactId
  • name
  • prerequisites

另外,就像java中全部類都繼承於java.lang.Object同樣,全部POM都有一個"Super POM",pom都從它繼承而來,下面是Maven3.5.4的"Super pom":

<project>
  <modelVersion>4.0.0</modelVersion>
 
  <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
 
  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>
 
  <build>
    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>
    <pluginManagement>
      <!-- NOTE: These plugins will be removed from future versions of the super POM -->
      <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.5.3</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
 
  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
  </reporting>
 
  <profiles>
    <!-- NOTE: The release profile will be removed from future versions of the super POM -->
    <profile>
      <id>release-profile</id>
 
      <activation>
        <property>
          <name>performRelease</name>
          <value>true</value>
        </property>
      </activation>
 
      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar-no-fork</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
</project>

C.聚合(或多模塊)

模塊是pom列出的項目,並做爲一個組執行,每一個模塊經過pom文件或項目的相對路徑進行列出.

<modules>
    <module>my-project</module>
    <module>another-project</module>
    <module>third-project/pom-example.xml</module>
</modules>

不須要考慮模塊間的依賴關係,Maven會對其進行拓撲排序以便在依賴模塊以前構建依賴關係.

(6)屬性

屬性是Maven中的值佔位符,像Ant同樣,能夠以

${x}

這樣的形式在pom.xml的任何位置訪問一個值,也能夠被用做默認值使用. 有5種形式使用值:

①env.x

env會使用當前shell的環境變量的值. 例如

${env.PATH}

②project.x

<project>下的x元素的值,如

${project.version}

③settings.x

使用settings.xml中的元素的值

${settings.offline}

④java.x

java系統屬性值,經過java.lang.System.getProperties()獲取,如

${java.home}

⑤x

直接使用x,用的是<properties>下的屬性,好比

<properties>
	<aaa>AAAAA</aaa>
</properties>
${aaa}

3.構建配置

<build>,聲明項目結構,管理插件等.

(1)BaseBuild元素

A.<defaultGoal>

目標的默認值,能夠取值install,copile

B.<directory>

構建產生的文件存放目錄

C.<finalName>

構建最終產生的項目名字,但有可能會被更改.

D.<filters>

定義一組<filter>,<filter>內是.properties文件,項目中的佔位符如xxx.xxx會被.properties中的xxx=xxx的具體值替換掉.

(2)資源

<resources>,項目相關的資源文件的位置.

A.<resource>

描述每一個資源的根元素.

B.<targetPath>

構建資源的位置,對於jar包放在META-INF裏面.

C.<flitering>

取值true或false,表示是否開啓過濾

D.<directory>

資源位置.

E.<include>

指定要包含的資源,使用*做爲通配符.

F.<excludes>

與include相反,要排除的資源列表.

(3)插件

<plugins>下包含了若干個<plugin>,表示插件,每一個<plugin>有如下元素:

A.<groupId>與<artifactId>

與上面的<groupId>與<artifactId>同樣.

B.<version>

與上面的<version>同樣.

C.<extensions>

取值true或false,表示是否加載擴展,默認爲false.

D.<inherited>

取值ture或false,是否應用pom的繼承關係,默認true.

E.<configuration>

插件項的相關配置,能夠配置<finalName>,<appendAssemblyld>,<descriptor>等.

F.<dependencies>

引入插件的依賴,與前面的<dependencies>相似.

G.<executions>

插件可能有多個目標,<executions>配置每個<execution>做爲插件的目標,在<execution>中,用<id>指定執行目標的標識符,用<goals>指定目標,<goals>包含一組<goal>,<phase>用於指定階段,<inherited>用於指定是否啓用繼承關係.另外<execution>也能夠包含<configuration>,與上面相似,用於配置特定的目標,而不是插件的全部目標.

(4)插件管理

<pluginManagement>,包含一組<plugins>,繼承於此項目的子項目均可以使用,子項目能夠覆蓋修改<pluginManagement>.

(5)目錄

能夠爲pom設置各類目錄,好比

A.項目源碼目錄

<sourceDirectory></sourceDirectory>

構建項目時會編譯該目錄的源碼,是相對於pom.xml的相對路徑.

B.測試源碼目錄

<testSourceDirectory></testSourceDirectory>

測試時會編譯其中的源碼,也是相對於pom.xml的相對路徑.

C.class目錄

<outputDirectory></outputDirectory>

這裏存放被編譯過的class文件.

D.測試class

<testOutputDirectory></testOutputDirectory>

存放測試文件編譯後的class文件.

(6)擴展

<extensions>,將包含在運行中的構建的類路徑中,在構建過程當中能夠激活擴展.好比能夠爲,例如這是支持ftp的wagon-ftp插件:

<build>
	<extensions>
		<extension>
			<groupId>org.apache.maven.wagon</groudId>
			<artifactId>wagon-ftp</artifactId>
			<version>3.3.4</version>
		</extension>
	</extensions>
</build>

(7)報表

<reporting>,描述產生報表的規範等,執行"mvn site"時報表就會運行.

A.<excludeDefaults>

是否包含默認報表.

B.<outputDirectory>

報表存放位置.

C.<plugins>

報表包含的插件以及配置.

D.<reportSets>

包含一組<reportSet>,與<execution>相似,配置多個目標,每一個<reportSet>包含<id>,<configuration>,<inherited>,以及<reports>,<id>指定報表集合的標識符,<configuration>表示使用的報表配置,<inherited>表示是否繼承到子pom,<reports>包含一組<report>,表示使用哪些報表.

4.項目信息

(1)許可證

<licenses>,包含一組<license>,每一個<license>包含<name>,<url>,<distribution>,<comments>.

A.<name>

名稱.

B.<url>

官方license頁面的url.

C.<distribution>

項目分發的方式,能夠選擇

  • repo:從Maven倉庫下載.
  • manual:手動安裝.

D.<comments>

一些補充信息.

(2)組織

<organazation>,包含<name>,<url>,與<license>的相似.

(3)開發者

<developers>,包含一組<developer>,每一個<developer>包含:

A.<id>

開發者id.

B.<name>

姓名.

C.<email>

郵箱.

D.<url>

主頁url.

E.<organization>

所屬組織.

F.<organizationUrl>

所屬組織的主頁url.

G.<roles>

角色,包含一組<role>,一個<role>描述一個角色.

H.<timeZone>

時區,能夠以America/New_York或Europe/Berlin這樣的形式,或者設置一個整數,範圍[-11,12].

I.<properties>

開發者屬性,如如何處理即時消息等.

(4)貢獻者

<contributors>,包含一組<contributor>,相似於<developer>,包含<name>,<email>等元素.

5.環境配置

(1)問題管理

<issueManagement>,定義缺陷跟蹤系統,如Bugzilla,TestTrack,ClearQuest等,包含<system>與<url>元素,<system>指定系統名字,<url>指定問題管理系統的url.

(2)持續集成管理

<ciManagement>,使用了觸發器,包含了:

A.<system>

持續集成系統的名稱.

B.<url>

持續集成系統的url.

C.<notifiers>

包含一組<notifier>,用來配置觸發器,每一個<notifier>包含:

a.<type>

如何發送通知,好比能夠取值mail.

b.<sendOnError>

取值true/false,錯誤時發送.

c.<sendOnFailure>

取值true/false,失敗時發送.

d.<sendOnSuccess>

取值true/false,成功時發送.

e.<sendOnWarning>

取值true/false,發生警告時發送.

f.<configuration>

相關配置,例如能夠添加<address>,發送的地址.

(3)郵件列表

<mailingLists>,包含一組<mailingList>,表示郵件信息,包括:

A.<name>

郵件名稱.

B.<subscribe>

訂閱郵件地址或連接.

C.<unsubscribe>

取消訂閱郵件或連接.

D.<post>

要發送的郵件地址.

E.<archive>

查看舊的郵件的url.

(4)軟件配置管理(SCM)

<scm>,也叫Source Code/Control Management,容許配置代碼庫供web站點和其餘插件使用.包含:

A.<connection>與<developConnection>

描述如何經過Maven鏈接到版本控制系統,其中connection須要讀權限,developConnection須要寫權限.

B.<tag>

代碼標籤,默認爲HEAD.

C.<url>

公開的可瀏覽的倉庫,例如ViewVC或Fisheye.

(5)前提條件

<prerequisites>,這是Maven2中的元素,只有一個子元素<maven>,指定maven的版本,且規定是2.x版本.Maven3中不須要<prerequisites>了,能夠用:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>3.0.0-M3</version>
            <executions>
                <execution>
                <id>enforce-maven</id>
                <goals>
                    <goal>enforce</goal>
                </goals>
                <configuration>
                <rules>
                    <requireMavenVersion>
                        <version>3.0</version>
                    </requireMavenVersion>
                </rules>    
            </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

代替.

(6)倉庫

<repositories>,包含一組<repository>,表示倉庫的位置,每一個<repository>包含:

A.<releases>

如何處理遠征倉庫的發佈版本,包含:

  • <enabled> true/false,是否啓用.
  • <updatePolicy> 更新頻率,Maven將本地pom的時間戳與遠程進行比較,能夠選擇:always,daily(默認),interval:X(X是一個整數,表示X分鐘),never.
  • <checksumPolicy> 校驗錯誤或校驗失敗時的策略,能夠選擇ignore,fail或warn.

B.<snapshots>

如何處理遠程倉庫的快照版本,包含的元素與<releases>同樣.

C.<id>

遠程倉庫的標識符.

D.<name>

遠程倉庫的名稱.

E.<url>

遠程倉庫的url.

F.<layout>

倉庫佈局類型,能夠是default或legacy,Maven2.x爲倉庫提供了默認佈局.

(7)插件倉庫

<pluginRepositories>,插件的遠程倉庫列表,包含一組<pluginRepository>,與<repositories>中的<repository>相似.

(8)分發管理

<distributeManagement>,管理整個構建過程當中的分發,能夠把網站部署到遠程服務器或者把構件部署到遠程倉庫.包含:

A.倉庫

<repository>,倉庫信息,包含:

  • <uniqueVersion>:惟一版本,取值true/false,表示是否生成一個惟一版本號或是使用部分地址做爲版本號.
  • <name>:倉庫名字.
  • <id>:倉庫id.
  • <url>:指定倉庫位置.
  • <layout>:佈局,取值default或legacy.

還有一個叫<snapshotRepository>的元素,與<repository>相似,表示快照倉庫.

B.站點部署

<site>,定義瞭如何部署項目的站點與文檔.包含:

  • <name>:站點名稱.
  • <id>:站點id.
  • <url>:站點url.

C.重定位

<relocation>,表示項目的新位置.包含:

  • <groupId>:新的<groupId>.
  • <artifactId>:新的<artifactId>.
  • <version>:新版本.
  • <message>:提示信息.

(9)配置文件

<profiles>,包含一組<profile>,每一個<profile>能夠定義不一樣的配置,包含的元素有:

  • <id>:配置文件的id,好比測試的能夠叫test.
  • <build>:相關構建信息.
  • <modules>:模塊信息.
  • <repositories>:遠程倉庫信息.
  • <pluginRepositories>:插件倉庫信息.
  • <dependencies>:依賴信息.
  • <reporting>:報表信息.
  • <dependencyManagement>:依賴管理信息.
  • <distributeManagement>:分發管理
  • <activation>:activation是profile的關鍵,profile的強大之處是某些狀況下才能夠修改基本pom,這些狀況經過activation指定.

<activation>包含如下元素:

A.<activeByDefault>

是否默認激活,true或false.

B.<jdk>

指定jdk版本.

C.<os>

<os>能夠定義一些特定的操做系統屬性,例如<name>,<family>,<arch>,<version>.

D.<property>

若Maven檢測到該屬性就會激活該屬性所在的配置文件,能夠指定<name>與<value>.

E.<file>

有<exists>與<missing>兩個子元素,<exists>表示若存在<exists>元素中對應的文件,則激活此配置文件.<miissing>表示若不存在<missing>元素中對應的文件,則激活此配置文件.

相關文章
相關標籤/搜索