什麼是pom?
pom做爲項目對象模型。經過xml表示maven項目,使用pom.xml來實現。主要描述了項目:包括配置文件;開發者須要遵循的規則,缺陷管理系統,組織和licenses,項目的url,項目的依賴性,以及其餘全部的項目相關因素。java
快速察看:
<project>
<modelVersion>4.0.0</modelVersion>
<!--maven2.0必須是這樣寫,如今是maven2惟一支持的版本-->
<!-- 基礎設置 -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>web
<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>apache
基本內容:api
POM包括了全部的項目信息maven
groupId:項目或者組織的惟一標誌,而且配置時生成路徑也是由今生成,如org.myproject.mojo生成的相對路徑爲:/org/myproject/mojo測試
artifactId:項目的通用名稱網站
version:項目的版本ui
packaging:打包機制,如pom,jar,maven-plugin,ejb,war,ear,rar,parurl
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>eredmond@codehaus.org</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