1. 程式人生 > >Maven工程的pom檔案解析

Maven工程的pom檔案解析

什麼是pom?
    pom作為專案物件模型。通過xml表示maven專案,使用pom.xml來實現。主要描述了專案:包括配置檔案;開發者需要遵循的規則,缺陷管理系統,組織和licenses,專案的url,專案的依賴性,以及其他所有的專案相關因素。

快速察看:
<project>
  <modelVersion>4.0.0</modelVersion>
<!--maven2.0必須是這樣寫,現在是maven2唯一支援的版本-->
  <!-- 基礎設定 -->
  <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>

基本內容:

POM包括了所有的專案資訊

groupId:專案或者組織的唯一標誌,並且配置時生成路徑也是由此生成,如org.myproject.mojo生成的相對路徑為:/org/myproject/mojo

artifactId:專案的通用名稱

version:專案的版本

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

name:使用者描述專案的名稱,無關緊要的東西,可選

url:應該是隻是寫明開發團隊的網站,無關緊要,可選

classifer:分類

其中groupId,artifactId,version,packaging這四項組成了專案的唯一座標。一般情況下,前面三項就可以組成專案的唯一座標了。

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這三個組合標示依賴的具體工程,而且 這個依賴工程必需是maven中心包管理範圍內的,如果碰上非開源包,maven支援不了這個包,那麼則有有三種 方法處理:

1.本地安裝這個外掛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裡屬性介紹:

type:預設為jar型別,常用的型別有:jar,ejb-client,test-jar...,可設定plugins中的extensions值為true後在增加 新的型別,

scope:是用來指定當前包的依賴範圍,maven的依賴範圍

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或者aggregation(即mutil-module的)的,那麼必須在packing賦值為pom,child工程從parent繼承的包括:dependencies,developers,contributors,plugin lists,reports lists,plugin execution with matching ids,plugin configuration

parent的使用方法如下:

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

relativePath是可選的,maven會首先搜尋這個地址,在搜尋本地遠端repositories之前.

dependencyManagement:是用於幫助管理chidren的dependencies的。例如如果parent使用dependencyManagement定義了一個dependencyon junit:junit4.0,那麼 它的children就可以只引用 groupId和artifactId,而version就可以通過parent來設定,這樣的好處就是可以集中管理 依賴的詳情

modules:對於多模組的project,outer-module沒有必需考慮inner-module的dependencies,當列出modules的時候,modules的順序是不重要的,因為maven會自動根據依賴關係來拓撲排序,

modules例子如下 :

<module>my-project</module>

<module>other-project</module>

properties:是為pom定義一些常量,在pom中的其它地方可以直接引用。

定義方式如下:

<properties>

      <file.encoding>UTF-8</file_encoding>

      <java.source.version>1.5</java_source_version>

      <java.target.version>1.5</java_target_version>

</properties>

使用方式 如下 :

${file.encoding}

還可以使用project.xx引用pom裡定義的其它屬性:如$(project.version} 

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>

targetPath:指定build資源到哪個目錄,預設是base directory

filtering:指定是否將filter檔案(即上面說的filters裡定義的*.property檔案)的變數值在這個resource檔案有效,例如上面就指定那些變數值在configuration檔案無效。

directory:指定屬性檔案的目錄,build的過程需要找到它,並且將其放到targetPath下,預設的directory是${basedir}/src/main/resources

includes:指定包含檔案的patterns,符合樣式並且在directory目錄下的檔案將會包含進project的資原始檔。

excludes:指定不包含在內的patterns,如果inclues與excludes有衝突,那麼excludes勝利,那些符合衝突的樣式的檔案是不會包含進來的。

testResources:這個模組包含測試資源元素,其內容定義與resources類似,不同的一點是預設的測試資源路徑是${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>

extensions:true or false, 決定是否要load這個plugin的extensions,預設為true.

inherited:是否讓子pom繼承,ture or false 預設為true.

configuration:通常用於私有不開源的plugin,不能夠詳細瞭解plugin的內部工作原理,但使plugin滿足的properties

dependencies:與pom基礎的dependencies的結構和功能都相同,只是plugin的dependencies用於plugin,而pom的denpendencies用於專案本身。在plugin的dependencies主要用於改變plugin原來的dependencies,例如排除一些用不到的dependency或者修改dependency的版本等,詳細請看pom的denpendencies.

executions:plugin也有很多個目標,每個目標具有不同的配置,executions就是設定plugin的目標,

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

id:識別符號

goals:裡面列出一系列的goals元素,例如上面的run goal

phase:宣告goals執行的時期,例如:verify

inherited:是否傳遞execution到子pom裡。

configuration:設定execution下列表的goals的設定,而不是plugin所有的goals的設定

pluginManagement配置:

pluginManagement的作用類似於denpendencyManagement,只是denpendencyManagement是用於管理專案jar包依賴,pluginManagement是用於管理plugin。與pom build裡的plugins區別是,這裡的plugin是列出來,然後讓子pom來決定是否引用。

例如:

<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引用方法:
在pom的build裡的plugins引用:
    <plugins>
      <plugin>
        < groupId>org.apache.maven.plugins</groupId>
        < artifactId>maven-jar-plugin</artifactId>
      </plugin>
    </plugins>

build裡的directories:

<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
    < scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
    < testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
    <outputDirectory>${basedir}/target/classes</outputDirectory>
    < testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>

這幾個元素只在parent build element裡面定義,他們設定多種路徑結構,他們並不在profile裡,所以不能通過profile來修改

build 裡面的Extensions:
它們是一系列build過程中要使用的產品,他們會包含在running bulid‘s classpath裡面。他們可以開啟extensions,也可以通過提供條件來啟用plugins。簡單來講,extensions是在build過程被啟用的產品
    <extensions>
      <extension>
        < groupId>org.apache.maven.wagon</groupId>
        < artifactId>wagon-ftp</artifactId>
        < version>1.0-alpha-3</version>
      </extension>
    < /extensions> 

reporting設定:

reporting包含site生成階段的一些元素,某些maven plugin可以生成reports並且在reporting下配置。例如javadoc,maven site等,在reporting下配置的report plugin的方法與build幾乎一樣,最不同的是build的plugin goals在executions下設定,而reporting的configures goals在reporttest。

excludeDefaults:是否排除site generator預設產生的reports

outputDirectory,預設的dir變成:${basedir}/target/site

report sets:設定execution goals,相當於build裡面的executions,不同的是不能夠bind a report to another phase,只能夠是site

<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>
reporting裡面的厄reportSets和build裡面的executions的作用都是控制pom的不同粒度去控制build的過程,我們不單要配置plugins,還要配置那些plugins單獨的goals。

更多專案資訊:

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>

列出本工程直接的licenses,而不要列出dependencies的licenses

配置組織資訊:
  <organization>
    <name>Codehaus Mojo</name>
    <url>http://mojo.codehaus.org</url>
  </organization>

很多工程都受到某些組織執行,這裡設定基本資訊

配置開發者資訊:

例如:一個開發者可以有多個roles,properties是 
<developers>
    <developer>
      <id>eric</id>
      <name>Eric</name>
      <email>[email protected]</email>
      <url>http://eric.propellors.net</url>
      <organization>Codehaus</organization>
      <organizationUrl>http://mojo.codehaus.org</organizationUrl>
      <roles>
        <role>architect</role>
        <role>developer</role>
      </roles>
      <timezone>-6</timezone>
      <properties>
        <picUrl>http://tinyurl.com/prv4t</picUrl>
      </properties>
    </developer>
  </developers>

環境設定:

issueManagement:bug跟蹤管理系統,定義defect tracking system缺陷跟蹤系統,比如有(bugzilla,testtrack,clearquest等).

例如:

  <issueManagement> 
    <system>Bugzilla</system> 
    <url>http://127.0.0.1/bugzilla/</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> 

release和snapshots:是artifact的兩種policies,pom可以選擇那種政策有效。 
enable:本別指定兩種型別是否可用,true or false 
updatePolicy:說明更新發生的頻率always 或者 never 或者 daily(預設的)或者 interval:X(X是分鐘數) 

checksumPolicy:當Maven的部署檔案到倉庫中,它也部署了相應的校驗和檔案。您可以選擇忽略,失敗,或缺少或不正確的校驗和警告。

layout:maven1.x與maven2有不同的layout,所以可以宣告為default或者是legacy(遺留方式maven1.x)。

外掛倉庫:

pluginRepositories:與Repositories具有類似的結構,只是Repositories是dependencies的home,而這個是plugins 的home。

分發管理:

distributionManagement :管理distribution和supporting files。 

downloadUrl:是其他專案為了抓取本專案的pom’s artifact而指定的url,就是說告訴pom upload的地址也就是別人可以下載的地址。 
status:這裡的狀態不要受到我們的設定,maven會自動設定project的狀態,有效的值:none:沒有宣告狀態,pom預設的;converted:本project是管理員從原先的maven版本convert到maven2的;partner:以前叫做synched,意思是與partner repository已經進行了同步;deployed:至今為止最經常的狀態,意思是製品是從maven2 instance部署的,人工在命令列deploy的就會得到這個;verified:本製品已經經過驗證,也就是已經定下來了最終版。 
repository:宣告deploy過程中current project會如何變成repository,說明部署到repository的資訊。 
    <repository> 
      <uniqueVersion>false</uniqueVersion> 
      <id>corp1</id> 
      <name>Corporate Repository</name> 
      <url>scp://repo1/maven2</url> 
      <layout>default</layout> 
    </repository> 
    <snapshotRepository> 
      <uniqueVersion>true</uniqueVersion> 
      <id>propSnap</id> 
      <name>Propellors Snapshots</name> 
      <url>sftp://propellers.net/maven</url> 
      <layout>legacy</layout> 
    </snapshotRepository> 
id, name::唯一性的id,和可讀性的name 
uniqueVersion:指定是否產生一個唯一性的version number還是使用address裡的其中version部分。true or false 
url:說明location和transport protocol 
layout:default或者legacy

profiles:pom4.0的一個新特性就是具有根據environment來修改設定的能力

它包含可選的activation(profile的觸發器)和一系列的changes。例如test過程可能會指向不同的資料庫(相對最終的deployment)或者不同的dependencies或者不同的repositories,並且是根據不同的JDK來改變的。那麼結構如下: 

  <profiles> 
    <profile> 
      <id>test</id> 
      <activation>...</activation> 
      <build>...</build> 
      <modules>...</modules> 
      <repositories>...</repositories> 
      <pluginRepositories>...</pluginRepositories> 
      <dependencies>...</dependencies> 
      <reporting>...</reporting> 
      <dependencyManagement>...</dependencyManagement> 
      <distributionManagement>...</distributionManagement> 
    </profile> 
  </profiles> 
Activation: 
觸發這個profile的條件配置如下例:(只需要其中一個成立就可以啟用profile,如果第一個條件滿足了,那麼後面就不會在進行匹配。 
    <profile> 
      <id>test</id> 
      <activation> 
        <activeByDefault>false</activeByDefault> 
        <jdk>1.5</jdk> 
        <os> 
          <name>Windows XP</name> 
          <family>Windows</family> 
          <arch>x86</arch> 
          <version>5.1.2600</version> 
        </os> 
        <property> 
          <name>mavenVersion</name> 
          <value>2.0.3</value> 
        </property> 
        <file> 
          <exists>${basedir}/file2.properties</exists> 
          <missing>${basedir}/file1.properties</missing> 
        </file> 
      </activation> 

啟用profile的方法有多個:setting檔案的activeProfile元素明確指定啟用的profile的ID,在命令列上明確啟用Profile用-P flag 引數 
檢視某個build會啟用的profile列表可以用:mvn help:active-profiles 

相關推薦

maven工程pom檔案依賴架包

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLS

maven工程pom檔案標籤詳解

maven是一個軟體工程(Software Project)管理工具。 對於maven來說,一個軟體工程的唯一標識(也叫maven座標)是由groupId(開發者)、artifactId(開發的產品)、version(產品版本) 共同決定的。每個maven管理的工程都有一個

maven實戰——pom檔案解析

前言 pom.xml檔案是maven專案的核心檔案,通過對怕pom檔案的解析,何以獲取專案的一些基本資訊以及所依賴的jar包等資訊。 pom檔案專案資訊的設定 一個簡單專案的pom檔案內容 <?xml version="1.0" encoding="UTF-8"?&

Maven工程pom檔案解析

什麼是pom?     pom作為專案物件模型。通過xml表示maven專案,使用pom.xml來實現。主要描述了專案:包括配置檔案;開發者需要遵循的規則,缺陷管理系統,組織和licenses,專案的url,專案的依賴性,以及其他所有的專案相關因素。 快速察看: &

Maven 工程 POM.XML檔案最全詳解

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

eclipse匯入maven工程pom.xml檔案不起作用

匯入硬碟中的maven工程時要確保import的是maven選項下的Existing Maven Pojects。 接著要替換maven倉庫的地址為自己定義的地址 window->preference->maven->user settings

maven匯出工程pom檔案中依賴的jar包

maven匯出工程pom檔案中依賴的jar包 文章目錄 在工程的pom檔案里加上下面plugin, 然後執行 mvn clean package -Dmaven.test.skip=true命令,就可以lib包收集起來了 <plug

Mavenpom檔案詳細解析

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

maven工程pom.xml檔案報錯:系統找不到指定的路徑

昨天專案正常執行且沒做任何操作,今天早上開啟遇到這個問題的小夥伴不要驚慌,可能是eclipse沒識別出pom.xml的標頭檔案。 錯誤樣式:H:\workspace\myproject\target\classes\META-INF\MANIFEST.MF (系統找不到指定

mavenpom檔案超詳細解析

<!--可以免費轉載,轉載時請註明出處 http://pengqb.javaeye.com 。--><project xmlns="http://maven.apache.org/POM/4.0.0 " xmlns:xsi="http://www.

maven工程pom.xml報jdk.tools.jar找不到問題

pom 文件的 targe copy 自己 .exe target 別人 數據平臺 轉自:http://www.cnblogs.com/gaoxu007/p/7204827.html 最近項目中使用到大數據平臺,代碼中應用了hbase-client.0.98.6-hadoo

解決idea中mavenpom檔案不會自動下載jar包問題

表現:無法下載pom配置檔案中的依賴包,或只能下載少數包,各項配置都正確的情況 理由未知; 百度了很長一段時間,網上給出比較精準的解決之一是 setting》》maven》》去掉work offline 的勾。(這裡肯定是去掉勾的) 在剛開始百度時,work offine 是出於未被勾選的狀態, 然後在網

Mavenpom.xml解析

pom.xml常用元素 modelVersion 指定了當前pom版本 groupId 反寫的公司網址+專案名 artifactId 專案名+模組名 version 版本號 第一個0表示大版本號;第二個0表示分支版本號;第三個0表示小版本號;如:0.0.1SNAPSHOT快照

匯入Maven 工程pom.xml首行報錯解決方法

1.利用IDE匯入一個Maven工程,但是pom.xml檔案首行報錯,發現是maven版本需要升級 2.在pom.xml檔案 增加配置 <properties>   <maven-jar-plugin.version>2.6</maven-jar-plugin.versio

maven工程xml檔案路徑問題

想了半天也不知道標題應該怎麼起, 1、maven工程中的xml檔案應該放在哪才能打包的時候打進去? 2、用sax解析檔案時,如何確定檔案的路徑? 3、如何才能確保本地方法可以讀取到本工程中的路徑,但是打成jar包供別人呼叫方法的時候卻找不到配置檔案路徑? 直接上程式碼和截圖

連線私服倉庫mavenpom檔案報錯:Project build error: Non-resolvable parent POM

配置了私服maven倉庫,在內網160伺服器上,修改了maven配置檔案。下載了一個專案,pom檔案報錯 問題如圖:         將<version>1.5.13.RELEASE</version>改成<version

Maven配置pom檔案詳解

maven核心,pom.xml詳解(轉) 什麼是pom? pom作為專案物件模型。通過xml表示maven專案,使用pom.xml來實現。主要描述了專案:包括配置檔案;開發者需要遵循的規則,缺陷管理系統,組織和licenses,專案的url,專案的依賴性,以及其他所有的專案相關因素。

Maven專案pom檔案詳解

1.Maven私服 依賴和外掛都屬於構件。 二、依賴、依賴的構件庫、外掛庫、構件的釋出包 1.依賴 2.對於依賴的構件庫,通過<repositories>設定 id,庫的ID name,庫的名稱 url,庫的URL layout

maven】【異常】maven專案POM檔案報錯

搭建springboot專案時,POM老是提示錯誤,雖然不影響,但是看到錯誤一直掛到那,感覺不舒服。 org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProj

Mavenpom檔案詳解

什麼是pom?     pom作為專案物件模型。通過xml表示maven專案,使用pom.xml來實現。主要描述了專案:包括配置檔案;開發者需要遵循的規則,缺陷管理系統,組織和licenses,專案的url,專案的依賴性,以及其他所有的專案相關因素。 快速察看: