1.概述
pom中節點如下分布
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <!-- 基本配置 -->
<modelVersion>4.0.0</modelVersion> <groupId>...</groupId> <artifactId>...</artifactId> <version>...</version> <packaging>...</packaging> <!-- 依賴配置 --> <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>
2.基本配置
- modelVersion:pom模型版本,maven2和3只能為4.0.0
- groupId:組ID,maven用於定位
- artifactId:在組中的唯一ID用於定位
- version:項目版本
- packaging:項目打包方式,有以下值:pom, jar, maven-plugin, ejb, war, ear, rar, par在J2EE項目中使用war
3.依賴配置
parent
用於確定父項目的坐標。
<parent> <groupId>com.learnPro</groupId> <artifactId>SIP-parent</artifactId> <relativePath></relativePath> <version>0.0.1-SNAPSHOT</version> </parent>
- groupId:父項目的構件標識符
- artifactId:父項目的唯一標識符
- relativePath:Maven首先在當前項目的找父項目的pom,然后在文件系統的這個位置(relativePath),然后在本地倉庫,再在遠程倉庫找。
- version:父項目的版本
modules
有些maven項目會做成多模塊的,這個標簽用於指定當前項目所包含的所有模塊。之后對這個項目進行的maven操作,會讓所有子模塊也進行相同操作。
<modules> <module>com-a</> <module>com-b</> <module>com-c</> </>
properties
用於定義pom常量
<properties> <java.version>1.7</java.version> </properties>
上面這個常量可以在pom文件的任意地方通過${java.version}來引用
dependencies
項目相關依賴配置,如果在父項目寫的依賴,會被子項目引用,一般父項目會將子項目公用的依賴引入(將在之后詳細講解)
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
這邊依賴和中央倉庫中的一致,就可以引入對應的jar
dependencyManagement
配置寫法同dependencies
<dependencyManagement> <dependencies> ..... </dependencies> </dependencyManagement>
在父模塊中定義后,子模塊不會直接使用對應依賴,但是在使用相同依賴的時候可以不加版本號:
父項目: <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> 子項目: <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency>
這樣的好處是,父項目統一了版本,而且子項目可以在需要的時候才引用對應的依賴
4.構建配置
build
用於配置項目構建相關信息
<build> <!--該元素設置了項目源碼目錄,當構建項目的時候,構建系統會編譯目錄里的源碼。該路徑是相對於pom.xml的相對路徑。 --> <sourceDirectory /> <!--該元素設置了項目腳本源碼目錄,該目錄和源碼目錄不同:絕大多數情況下,該目錄下的內容 會被拷貝到輸出目錄(因為腳本是被解釋的,而不是被編譯的)。 --> <scriptSourceDirectory /> <!--該元素設置了項目單元測試使用的源碼目錄,當測試項目的時候,構建系統會編譯目錄里的源碼。該路徑是相對於pom.xml的相對路徑。 --> <testSourceDirectory /> <!--被編譯過的應用程序class文件存放的目錄。 --> <outputDirectory /> <!--被編譯過的測試class文件存放的目錄。 --> <testOutputDirectory /> <!--使用來自該項目的一系列構建擴展 --> <extensions> <!--描述使用到的構建擴展。 --> <extension> <!--構建擴展的groupId --> <groupId /> <!--構建擴展的artifactId --> <artifactId /> <!--構建擴展的版本 --> <version /> </extension> </extensions> <!--當項目沒有規定目標(Maven2 叫做階段)時的默認值 --> <defaultGoal /> <!--這個元素描述了項目相關的所有資源路徑列表,例如和項目相關的屬性文件,這些資源被包含在最終的打包文件里。 --> <resources> <!--這個元素描述了項目相關或測試相關的所有資源路徑 --> <resource> <!-- 描述了資源的目標路徑。該路徑相對target/classes目錄(例如${project.build.outputDirectory})。
舉個例子,如果你想資源在特定的包里(org.apache.maven.messages),你就必須該元素設置為org/apache/maven /messages。
然而,如果你只是想把資源放到源碼目錄結構里,就不需要該配置。
--> <targetPath /> <!--是否使用參數值代替參數名。參數值取自properties元素或者文件里配置的屬性,文件在filters元素里列出。 --> <filtering /> <!--描述存放資源的目錄,該路徑相對POM路徑 --> <directory /> <!--包含的模式列表,例如**/*.xml. --> <includes /> <!--排除的模式列表,例如**/*.xml --> <excludes /> </resource> </resources> <!--這個元素描述了單元測試相關的所有資源路徑,例如和單元測試相關的屬性文件。 --> <testResources> <!--這個元素描述了測試相關的所有資源路徑,參見build/resources/resource元素的說明 --> <testResource> <targetPath /> <filtering /> <directory /> <includes /> <excludes /> </testResource> </testResources> <!--構建產生的所有文件存放的目錄 --> <directory /> <!--產生的構件的文件名,默認值是${artifactId}-${version}。 --> <finalName /> <!--當filtering開關打開時,使用到的過濾器屬性文件列表 --> <filters /> <!--子項目可以引用的默認插件信息。該插件配置項直到被引用時才會被解析或綁定到生命周期。給定插件的任何本地配置都會覆蓋這里的配置 --> <pluginManagement> <!--使用的插件列表 。 --> <plugins> <!--plugin元素包含描述插件所需要的信息。 --> <plugin> <!--插件在倉庫里的group ID --> <groupId /> <!--插件在倉庫里的artifact ID --> <artifactId /> <!--被使用的插件的版本(或版本范圍) --> <version /> <!--是否從該插件下載Maven擴展(例如打包和類型處理器),由於性能原因,只有在真需要下載時,該元素才被設置成enabled。 --> <extensions /> <!--在構建生命周期中執行一組目標的配置。每個目標可能有不同的配置。 --> <executions> <!--execution元素包含了插件執行需要的信息 --> <execution> <!--執行目標的標識符,用於標識構建過程中的目標,或者匹配繼承過程中需要合並的執行目標 --> <id /> <!--綁定了目標的構建生命周期階段,如果省略,目標會被綁定到源數據里配置的默認階段 --> <phase /> <!--配置的執行目標 --> <goals /> <!--配置是否被傳播到子POM --> <inherited /> <!--作為DOM對象的配置 --> <configuration /> </execution> </executions> <!--項目引入插件所需要的額外依賴 --> <dependencies> <!--參見dependencies/dependency元素 --> <dependency> ...... </dependency> </dependencies> <!--任何配置是否被傳播到子項目 --> <inherited /> <!--作為DOM對象的配置 --> <configuration /> </plugin> </plugins> </pluginManagement> <!--使用的插件列表 --> <plugins> <!--參見build/pluginManagement/plugins/plugin元素 --> <plugin> <groupId /> <artifactId /> <version /> <extensions /> <executions> <execution> <id /> <phase /> <goals /> <inherited /> <configuration /> </execution> </executions> <dependencies> <!--參見dependencies/dependency元素 --> <dependency> ...... </dependency> </dependencies> <goals /> <inherited /> <configuration /> </plugin> </plugins> </build>
reporting
該元素描述使用報表插件產生報表的規范。當用戶執行“mvn site”,這些報表就會運行。 在頁面導航欄能看到所有報表的鏈接。
<reporting> <!--true,則,網站不包括默認的報表。這包括“項目信息”菜單中的報表。 --> <excludeDefaults /> <!--所有產生的報表存放到哪里。默認值是${project.build.directory}/site。 --> <outputDirectory /> <!--使用的報表插件和他們的配置。 --> <plugins> <!--plugin元素包含描述報表插件需要的信息 --> <plugin> <!--報表插件在倉庫里的group ID --> <groupId /> <!--報表插件在倉庫里的artifact ID --> <artifactId /> <!--被使用的報表插件的版本(或版本范圍) --> <version /> <!--任何配置是否被傳播到子項目 --> <inherited /> <!--報表插件的配置 --> <configuration /> <!--
一組報表的多重規范,每個規范可能有不同的配置。一個規范(報表集)對應一個執行目標 。
例如,有1,2,3,4,5,6,7,8,9個報表。1,2,5構成A報表集,對應一個執行目標。2,5,8構成B報表集,對應另一個執行目標
--> <reportSets> <!--表示報表的一個集合,以及產生該集合的配置 --> <reportSet> <!--報表集合的唯一標識符,POM繼承時用到 --> <id /> <!--產生報表集合時,被使用的報表的配置 --> <configuration /> <!--配置是否被繼承到子POMs --> <inherited /> <!--這個集合里使用到哪些報表 --> <reports /> </reportSet> </reportSets> </plugin> </plugins> </reporting>
5.項目信息
- name:給用戶提供更為友好的項目名
- description:項目描述,maven文檔中保存
- url:主頁的URL,maven文檔中保存
- inceptionYear:項目創建年份,4位數字。當產生版權信息時需要使用這個值
- licenses:該元素描述了項目所有License列表。 應該只列出該項目的license列表,不要列出依賴項目的 license列表。如果列出多個license,用戶可以選擇它們中的一個而不是接受所有license。(如下)
<license> <!--license用於法律上的名稱--> <name>...</name> <!--官方的license正文頁面的URL--> <url>....</url> <!--項目分發的主要方式:repo,可以從Maven庫下載 manual, 用戶必須手動下載和安裝依賴--> <distribution>repo</distribution> <!--關於license的補充信息--> <comments>....</comments> </license>
- organization:1.name 組織名 2.url 組織主頁url
- developers:項目開發人員列表(如下)
- contributors:項目其他貢獻者列表,同developers
<developers> <!--某個開發者信息--> <developer> <!--開發者的唯一標識符--> <id>....</id> <!--開發者的全名--> <name>...</name> <!--開發者的email--> <email>...</email> <!--開發者的主頁--> <url>...<url/> <!--開發者在項目中的角色--> <roles> <role>Java Dev</role> <role>Web UI</role> </roles> <!--開發者所屬組織--> <organization>sun</organization> <!--開發者所屬組織的URL--> <organizationUrl>...</organizationUrl> <!--開發者屬性,如即時消息如何處理等--> <properties> <!-- 和主標簽中的properties一樣,可以隨意定義子標簽 --> </properties> <!--開發者所在時區, -11到12范圍內的整數。--> <timezone>-5</timezone> </developer> </developers>
6.環境設置
issueManagement
目的問題管理系統(Bugzilla, Jira, Scarab)的名稱和URL
<issueManagement> <system>Bugzilla</system> <url>http://127.0.0.1/bugzilla/</url> </issueManagement>
- system:系統類型
- url:路徑
ciManagement
項目的持續集成信息
<ciManagement> <system>continuum</system> <url>http://127.0.0.1:8080/continuum</url> <notifiers> <notifier> <type>mail</type> <sendOnError>true</sendOnError> <sendOnFailure>true</sendOnFailure> <sendOnSuccess>false</sendOnSuccess> <sendOnWarning>false</sendOnWarning> <address>continuum@127.0.0.1</address> <configuration></configuration> </notifier> </notifiers> </ciManagement>
- system:持續集成系統的名字
- url:持續集成系統的URL
- notifiers:構建完成時,需要通知的開發者/用戶的配置項。包括被通知者信息和通知條件(錯誤,失敗,成功,警告)
- type:通知方式
- sendOnError:錯誤時是否通知
- sendOnFailure:失敗時是否通知
- sendOnSuccess:成功時是否通知
- sendOnWarning:警告時是否通知
- address:通知發送到的地址
- configuration:擴展項
mailingLists
項目相關郵件列表信息
<mailingLists> <mailingList> <name>User List</name> <subscribe>user-subscribe@127.0.0.1</subscribe> <unsubscribe>user-unsubscribe@127.0.0.1</unsubscribe> <post>user@127.0.0.1</post> <archive>http://127.0.0.1/user/</archive> <otherArchives> <otherArchive>http://base.google.com/base/1/127.0.0.1</otherArchive> </otherArchives> </mailingList> ..... </mailingLists>
- subscribe, unsubscribe: 訂閱郵件(取消訂閱)的地址或鏈接,如果是郵件地址,創建文檔時,mailto: 鏈接會被自動創建
- archive:瀏覽郵件信息的URL
- post:接收郵件的地址
scm
允許你配置你的代碼庫,供Maven web站點和其它插件使用
<scm> <connection>scm:svn:http://127.0.0.1/svn/my-project</connection> <developerConnection>scm:svn:https://127.0.0.1/svn/my-project</developerConnection> <tag>HEAD</tag> <url>http://127.0.0.1/websvn/my-project</url> </scm>
- connection, developerConnection:這兩個表示我們如何連接到maven的版本庫。connection只提供讀,developerConnection將提供寫的請求
- 寫法如:scm:[provider]:[provider_specific]
- 如果連接到CVS倉庫,可以配置如下:scm:cvs:pserver:127.0.0.1:/cvs/root:my-project
- tag:項目標簽,默認HEAD
- url:共有倉庫路徑
prerequisites
項目構建的前提
<prerequisites> <maven>2.0.6</maven> </prerequisites>
repositories,pluginRepositories
依賴和擴展的遠程倉庫列表,同上篇文章,setting.xml配置中介紹的。
<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> <pluginRepositories> ... </pluginRepositories>
- releases, snapshots:這是各種構件的策略,release或者snapshot。這兩個集合,POM就可以根據獨立倉庫任意類型的依賴改變策略。如:一個人可能只激活下載snapshot用來開發。
- enable:true或者false,決定倉庫是否對於各自的類型激活(release 或者 snapshot)。
- updatePolicy: 這個元素決定更新頻率。maven將比較本地pom的時間戳(存儲在倉庫的maven數據文件中)和遠程的. 有以下選擇: always, daily (默認), interval:X (x是代表分鍾的整型) , never.
- checksumPolicy:當Maven向倉庫部署文件的時候,它也部署了相應的校驗和文件。可選的為:ignore,fail,warn,或者不正確的校驗和。
- layout:在上面描述倉庫的時候,提到他們有統一的布局。Maven 2有它倉庫默認布局。然而,Maven 1.x有不同布局。使用這個元素來表明它是default還是legacy。
distributionManagement
它管理的分布在整個構建過程生成的工件和支持文件
<distributionManagement> ... <downloadUrl>http://mojo.codehaus.org/my-project</downloadUrl> <status>deployed</status> </distributionManagement>
- downloadUrl: 其他pom可以通過此url的倉庫抓取組件
- status:給出該構件在遠程倉庫的狀態
- none: 默認
- converted: 將被早期Maven 2 POM轉換過來
- partner: 這個項目會從合作者倉庫同步過來
- deployed: 從Maven 2或3實例部署
- verified: 被核實時正確的和最終的
指定Maven pom從遠程下載控件到當前項目的位置和方式,如果snapshotRepository沒有被定義則使用repository相關的配置
<distributionManagement> <repository> <uniqueVersion>false</uniqueVersion> <id>corp1</id> <name>Corporate Repository</name> <url>scp://repo/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> ... </distributionManagement>
- id, name:倉庫的唯一標識
- uniqueVersion:true或false,指明控件部署的時候是否獲取獨立的版本號。
- url:repository元素的核心。指定位置和部署協議發布控件到倉庫。
- layout:布局,default或legacy
Site Distribution
多分布存儲庫,distributionManagement負責定義如何部署項目的網站和文檔。
<distributionManagement> ... <site> <id>mojo.website</id> <name>Mojo Website</name> <url>scp://beaver.codehaus.org/home/projects/mojo/public_html/</url> </site> ... </distributionManagement>
- id, name, url: 這些元素與distributionManagement repository中的相同
Relocation
重新部署-項目不是靜態的,是活的。他們需要被搬到更合適的地方。如:當你的下個成功的開源項目移到Apache下,重命名為org.apache:my-project:1.0 對你項目更有好處。
<distributionManagement> ... <relocation> <groupId>org.apache</groupId> <artifactId>my-project</artifactId> <version>1.0</version> <message>We have moved the Project under Apache</message> </relocation> ... </distributionManagement>
profiles
profile可以讓我們定義一系列的配置信息(插件等),然后指定其激活條件
profile的相關配置可以參考上篇文章Maven實戰(二)–setting.xml詳解:
本文轉載
oDeviloo:Maven實戰(三)--Pom.xml詳解