原文:http://blog.csdn.net/oDeviloo/article/details/52050277
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
2.基本配置
- modelVersion:pom模型版本,maven2和3只能为4.0.0
- groupId:组ID,maven用于定位
- artifactId:在组中的唯一ID用于定位
- version:项目版本
- packaging:项目打包方式,有以下值:pom, jar, maven-plugin, ejb, war, ear, rar, par
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
这样的好处是,父项目统一了版本,而且子项目可以在需要的时候才引用对应的依赖
4.构建配置
build
用于配置项目构建相关信息
<build> <sourceDirectory/> <scriptSourceDirectory/> <testSourceDirectory/> <outputDirectory/> <testOutputDirectory/> <extensions> <extension> <groupId/> <artifactId/> <version/> </extension> </extensions> <defaultGoal/> <resources> <resource> <targetPath/> <filtering/> <directory/> <includes/> <excludes/> </resource> </resources> <testResources> <testResource> <targetPath/><filtering/><directory/><includes/><excludes/> </testResource> </testResources> <directory/> <finalName/> <filters/> <pluginManagement> <plugins> <plugin> <groupId/> <artifactId/> <version/> <extensions/> <executions> <execution> <id/> <phase/> <goals/> <inherited/> <configuration/> </execution> </executions> <dependencies> <dependency> ...... </dependency> </dependencies> <inherited/> <configuration/> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId/><artifactId/><version/><extensions/> <executions> <execution> <id/><phase/><goals/><inherited/><configuration/> </execution> </executions> <dependencies> <dependency> ...... </dependency> </dependencies> <goals/><inherited/><configuration/> </plugin> </plugins> </build>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
reporting
该元素描述使用报表插件产生报表的规范。当用户执行“mvn site”,这些报表就会运行。 在页面导航栏能看到所有报表的链接。
<reporting> <excludeDefaults/> <outputDirectory/> <plugins> <plugin> <groupId/> <artifactId/> <version/> <inherited/> <configuration/> <reportSets> <reportSet> <id/> <configuration/> <inherited/> <reports/> </reportSet> </reportSets> </plugin> </plugins> </reporting>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
5.项目信息
- name:给用户提供更为友好的项目名
- description:项目描述,maven文档中保存
- url:主页的URL,maven文档中保存
- inceptionYear:项目创建年份,4位数字。当产生版权信息时需要使用这个值
- licenses:该元素描述了项目所有License列表。 应该只列出该项目的license列表,不要列出依赖项目的 license列表。如果列出多个license,用户可以选择它们中的一个而不是接受所有license。(如下)
<license> <name>...</name> <url>....</url> <distribution>repo</distribution> <comments>....</comments> </license>
- organization:1.name 组织名 2.url 组织主页url
- developers:项目开发人员列表(如下)
- contributors:项目其他贡献者列表,同developers
<developers> <developer> <id>....</id> <name>...</name> <email>...</email> <url>...<url/> <roles> <role>Java Dev</role> <role>Web UI</role> </roles> <organization>sun</organization> <organizationUrl>...</organizationUrl> <properties> </properties> <timezone>-5</timezone> </developer> </developers>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
6.环境设置
issueManagement
目的问题管理系统(Bugzilla, Jira, Scarab)的名称和URL
<issueManagement> <system>Bugzilla</system> <url>http://127.0.0.1/bugzilla/</url></issueManagement>
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 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: 被核实时正确的和最终的
Repository
指定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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 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>