pom文件节点
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.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>
接下来我们就对这几个部分进行一一研究。
基础配置信息
<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">
<!-- pom模型版本,maven2和3只能为4.0.0-->
<modelVersion>4.0.0</modelVersion>
<!-- 项目的组ID,用于maven定位-->
<groupId>com.company.bank</groupId>
<!-- 项目ID,通常是项目的名称,唯一标识符-->
<artifactId>parent</artifactId>
<!-- 项目的版本-->
<version>0.0.1-SNAPSHOT</version>
<!-- 项目的打包方式-->
<packaging>war</packaging>
<project>
在pom.xml文件中,最外面的标签是project标签,它定义了以下属性:
- xmlns 属性:表示该标签下元素的默认命名空间为:http://maven.apache.org/POM/4.0.0。在xml文件中,定义命名空间的格式为:xmlns:namespace-prefix=“namespaceURI”,定义默认命名空间的格式为:xmlns=“namespaceURI”;
- xmlns:xsi 属性:定义一个命名空间前缀,它代表的命名空间 URI 为:http://www.w3.org/2001/XMLSchema-instance。定义这个命名空间主要是为了更方便地使用它所代表的内容。命名空间中的 schemaLocation 属性;其中,使用xsi作为namespace-prefix并不是硬性规定,只是一般性的选择,当然也可以改为其他名称;
- xsi:schemaLocation 属性:该属性的使用格式为:xsi:schemaLocation="namespaceURI1 schemaURI1 namespaceURI2 schemaURI2...",这里表示使用 schemaURI1 对应的 schema 文件来验证命名空间 namespaceURI1 下的元素是否符合XML 语法规范,如下: 类比是这样的。
我们看一下一些节点的基本信息:
节点 | 相应解释 |
---|---|
型号版本 | pom模型版本,根据官方文档,Maven2和3只能是4.0.0 |
组ID | 项目组id表示项目所属的组织或公司。命名规则通常是组织或公司域名倒置,然后是项目名称。 |
工件ID | 项目ID有时与项目名称一致,有时是项目名称+模块名称。 ID 是唯一的。一个goupId下可能有多个artifactId,通过artifactId来区分。例如:消费者银行业务。 |
版本 | 当前项目的版本号一般为:major version。小版本。增量版本 - 有限的版本号。 SHAPSHOT 是快照的意思,表明该项目还在开发中。 |
包装 | 项目打包方式,常用可选值:pom、jar、ejb、maven-plugin、war、ear、rar、par等,默认方式为jar |
项目信息
节点 | 相应解释 |
---|---|
姓名 | 声明一个更加用户友好的项目名称,可选,一般用于Maven生成的文档 |
描述 | 项目的详细描述信息。当可以用HTML格式描述时,不建议使用纯文本来描述。一般用于Maven生成的文档。 |
网址 | 项目主页的URL,一般用于Maven生成的文档 |
成立年份 | 项目启动年份,通常是4位数字,用于在介绍情况时提供版权信息。 |
许可证 | 当前项目的所有许可证文件。每个许可证文件由许可证元素描述,然后描述附加元素。通常只适用 |
组织 | 组织相关信息 |
开发商 | 项目开发商名单 |
贡献者 | 该项目的其他贡献者列表,与开发人员相同 |
许可证列表:
<license>
<!--license用于法律上的名称-->
<name>...</name>
<!--官方的license正文页面的URL-->
<url>....</url>
<!--项目分发的主要方式:repo,可以从Maven库下载 manual, 用户必须手动下载和安装依赖-->
<distribution>repo</distribution>
<!--关于license的补充信息-->
<comments>....</comments>
</license>
组织属性:
<organization>
<!-- 组织的主页 -->
<url>...</url>
<!-- 组织名称 -->
<name>...</name>
</organization>
开发者属性:
<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>
贡献者属性:
<contributors>
<contributor>
...
</contributor>
</contributors>
依赖配置
依赖项:项目相关的依赖项。如果父项目中的依赖被子项目引用,则子项目中的公共依赖一般都是在父项目中定义的。并且如果有需要,子项目可以修改依赖包的版本:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
parent:用于确定父项目的坐标位置
<parent>
<groupId>com.learnPro</groupId>
<artifactId>SIP-parent</artifactId>
<relativePath></relativePath>
<version>0.0.1-SNAPSHOT</version>
</parent>
groupId: 父项目的组Id标识符
artifactId:父项目的唯一标识符
relativePath:Maven首先在当前项目中找父项目的pom,然后在文件系统的这个位置(relativePath)查找,然后在本地仓库查找,再在远程仓库找。
version:父项目的版本
dependencyManagement:用于帮助管理孩子的依赖关系。例如,如果父级使用dependencyManagement定义了junit:junit4.0,那么它的子级只能引用groupId和artifactId,版本可以直接使用父级模块。如果需要的话,我们还可以设置版本。这样做的好处是可以集中管理依赖细节,更加灵活。但dependencyManagement只声明依赖关系,并没有实现引入,所以子项目需要显示所需的依赖关系。配置与依赖类似;
<!-- 父项目中定义:-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 子项目先通过parent继承之后,就可以引入使用了 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
依赖的几个重要属性介绍:
- type属性,依赖的类型,默认为jar,通过指定pom,可以定义整个pom文件;
- scope属性,当前包的依赖范围,用于限制依赖的传递性。通过控制依赖关系的范围,您可以指定依赖关系在哪个阶段有效。该属性共有 5 个值。默认是compile,这意味着依赖关系在所有类路径中都可用,包括编译、测试和运行。例如,provided 表示依赖项在编译和测试期间在类路径中可用。全部查看请参考:依赖机制介绍-依赖范围
- 可选属性,因为依赖关系具有传递性,例如项目A依赖于项目B,B依赖C,那么B对C的依赖也会传递给A。如果我们不需要这种传递依赖,我们可以通过设置该财产的价值。默认为false,即子项目默认继承。如果为true,则必须显式引入子项目,类似于dependencyManagement中定义的依赖关系;
- 排除和排除属性用于删除项目依赖性。如果项目X需要A,并且A包含B依赖,那么X可以声明它不依赖B。只需在exclusions中声明exclusion,并从依赖树中删除B即可。这个一般用来解决jar包冲突的问题;
<dependencies>
<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>
</dependency>
</dependencies>
modules
:Maven项目的一大特点就是以多模块著称,该标签就是用于指定当前项目所包含的模块,对该模块进行的Maven操作,会让所有子模块也进行相应的操作;
<modules>
<module>com-a</>
<module>com-b</>
<module>com-c</>
<modules/>
properties
:该标签用于定义pom文件中的常量,这样,在pom文件的任何地方,都可以通过${java.version}来引用该值;
<properties>
<!-- 定义常量 -->
<rocketmq.version>3.2.6</rocketmq.version>
</properties>
<!-- 使用 -->
<dependency>
<groupId>com.alibaba.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>${rocketmq.version}</version>
</dependency>
构建配置
所谓的build,也就是项目构建。在构建配置的过程中,一般包含两个部分,一个是<build>
,另一个是<reporting>
;而在Maven的pom.xml中build又分为两种,一种被称为Project Build,也就是<project>
下的直接子元素,另一种被称为Profile Build,即是<profile>
的直接子元素。Profile Build包含了基本的build元素,而Project Build还包含两个特殊的元素,即各种<...Directory>
和<extensions>
。
<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>
环境配置
issueManagement
:用于缺陷系统的跟踪与管理,比如Bugzilla, Jira等管理工具:
<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:构建完成时,需要通知的开发者/用户的配置项。包括被通知者信息和通知条件(错误,失败,成功,警告)
- 类型:通知方式
- sendOnError:发生错误时是否通知
- sendOnFailure:失败时是否通知
- sendOnSuccess:成功时是否通知
- sendOnWarning:警告时是否通知
- 地址:通知发送到的地址
- 配置:扩展项
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: 订阅邮件(取消订阅)的地址或链接,如果是邮件地址,创建文档时,链接会被自动创建;
archive:浏览邮件信息的URL;
post:接收邮件的地址;
SCM
:(Source Control Management),该标签允许你配置你的代码库,供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、依赖项和扩展远程仓库列表; pom中的仓库与setting.xml中的仓库功能相同。主要区别在于pom中的仓库是个性化的。例如,一家大公司的setting文件是公共的,所有项目都使用一个setting文件,但是每个子项目都会引用不同的第三方库,所以需要在pom.xml中设置自己需要的仓库地址。 PluginRepositories 与 Repositories 具有类似的结构,只不过 Repositories 是依赖项的所在地,而这是插件的所在地。
<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>
eleases,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。
profiles
:POM 4.0的一个新特性是一个项目能够根据所构建的环境改变设置。通过profiles,我们可以指定构建的环境是开发,测试,UAT,生产等环境;
<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">
...
<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>
</project>
我们可以通过激活标签来激活相应的配置文件,即指定要构建的默认环境,并且可以配置各种需要的环境。激活配置文件的方法也有很多。具体参数请查看官网文档。
<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">
...
<profiles>
<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>sparrow-type</name>
<value>African</value>
</property>
<file>
<exists>${basedir}/file2.properties</exists>
<missing>${basedir}/file1.properties</missing>
</file>
</activation>
...
</profile>
</profiles>
</project>