<?xml version="1.0" encoding="UTF-8"?> <project> <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> <license>...</license> <organization>...</organization> <developers>...</developers> <contributors>...</contributors> <issueManagement>...</issueManagement> <ciManagement>...</ciManagement> <mailingLists>...</mailingLists> <scm>...</scm> <preprequisites>...</preprequisites> <repositories>...</repositories> <pluginRepositories>...</pluginRepositories> <distributionManagement>...</distributionManagement> <profiles>...</profiles> </project>
常用元素的说明:
modelVersion:描述这个POM文件遵从哪个版本的项目描述符
groupId:项目或者组织的唯一标志,并且配置时生成的路径也是由此生成。通常用一个完全正确的包的名字来与其他项目的类似名字来进行区分(比如:org.apache.maven)
artifactId: 项目的通用名称
groupId 和 artifactId 合起来作为当前项目的唯一标识,maven2最终会根据这两个值,决定项目发布到 repo 上时所处的位置。
version:项目的版本号,它用来标记同一个项目发布的不同版本。
packaging: 这个项目生产出来的artifact 类型,如 pom, jar, maven-plugin, ejb, war, ear, rar, par ,... 。
最小配置
任何项目都最少要包含以下几个标签。
<project> <modelVersion>4.0.0</modelVersion> <groupId>...</groupId> <artifactId>...</artifactId> <version>...</version> </project>
modelVersion 这个标签必须存在,而且它的值必须是4.0.0,这标志着我们现在使用的是maven2。
1 maven的继承定义
假设定义了一个父项目:
<project> <modelVersion>4.0.0</modelVersion> <groupId>com.taobao</groupId> <artifactId>taobao-parent</artifactId> <version>2.0</version> <packaging>pom</packaging> </project>packaging 类型需要pom,用于parent和合成多个项目。那么在其下的子项目中加上如下设置用以继承
<parent> <groupId>com.taobao</groupId> <artifactId> taobao-parent </artifactId> <version>2.0</version> </parent>
maven继承案例 - springside sample : mini-web:
<project> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springside</groupId> <artifactId>springside3-parent</artifactId> <version>3.3.2</version> </parent> <artifactId>mini-web</artifactId> <packaging>war</packaging> <name>Springside3's Mini-Web Example</name> ... </project>
2 合成(或者多个模块)
一个项目有多个模块,也叫做多重模块,或者合成项目。每一个被列出来的子模块都指向包含这个模块的目录文件的相对路径。
如下定义:
<modules> <module>tc-client</module> <module>tc-server</module> </modules>
3 build 设置
pom中一大部分配置都包含在build标签中,这部分是在对项目进行构建时所需要的配置,当你对项目进行编译,测试,打包,发布的时候,这部分配置就会在对应的阶段起作用了。build 主要分为两部分,基本元素和扩展元素集合,注意:包括项目build和profile build
xml 代码 :
<project> <build>…</build> <profiles> <profile> <build>…</build> </profile> </profiles> </project>build标签中可以包含很多插件,这些插件可以配置到项目的某些构建阶段,随着项目的构建进程发挥作用。
4 报表部分
我们执行mvn site就可以为项目生成一系列可以用来描述项目信息的网页,maven2中的一大部分插件就是专门在这时候发挥效用的,它们可以根据项目的结构,源代码,测试,SCM信息等,生成各种特殊功能的报表,这要把这些插件配置在reporting标签中,它们就会在执行mvn site的同时起作用。所有的报告将会包括在浏览器的导航栏中。
xml 代码 :
<project> <build>…</build> <profiles> <profile> <build>…</build> </profile> </profiles> <reporting>…</reporting> </project>
5 插件
在build时,执行的插件,比较有用的部分,如使用jdk 5.0编译等等
xml 代码:
<project> <build> … <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> </plugins> </build> </project>
extensions: true or false,是否装载插件扩展。默认false
inherited: true or false,是否此插件配置将会应用于poms,那些继承于此的项目
configuration: 指定插件配置
dependencies: 插件需要依赖的包
executions: 用于配置execution目标,一个插件可以有多个目标。
springside sample : mini-web 中插件配置的案例:
<build> <plugins> <!-- eclipse插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <configuration> <wtpversion>2.0</wtpversion> <additionalProjectnatures> <projectnature>org.springframework.ide.eclipse.core.springnature </projectnature> </additionalProjectnatures> </configuration> </plugin> </plugins> </build>
6 资源(resources)你项目中需要指定的资源。如spring配置文件,log4j.properties
xml 代码:
<project> <build> … <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> </resources> <testResources> … </testResources> … </build> </project>
resources: resource的列表,用于包括所有的资源
targetPath: 指定目标路径,用于放置资源,用于build
filtering: 是否替换资源中的属性placehold
directory: 资源所在的位置
includes: 样式,包括那些资源
excludes: 排除的资源
testResources: 测试资源列表
7 依赖关系
我们平常接触最多的就是dependencies和dependency标签了。因为既然使用了maven2,最大的得益之处还是可以用它统一管理依赖库。dependencies和它内部的dependency标签就用来配置当前项目所依赖的第三方库的,并且每个依赖都需要groupId,artifactId和version这些数据 。
<project> <modelVersion>4.0.0</modelVersion> <groupId>...</groupId> <artifactId>...</artifactId> <version>...</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.0</version> <type>jar</type> <scope>test</scope> <optional>true</optional> </dependency> </dependencies> </project>
我们可以看到dependency标签中也包含了groupId,artifactId和version信息,这三部分就是一个maven2项目所需的最小配置,也是maven2所管理的项目的唯一标识,有了这三个数据,maven2就可以在repo中查找到对应的依赖,并将其包含到我们的项目中,从而实现对依赖库的管理。
上面的依赖中,我们使用了groupId为junit,artifactId为junit,版本为4.0的依赖,这个依赖会在test作用域中起作用,也就是说,只有在进行单元测试的时候,才会用到这个依赖,与之对应的是,当我们的程序正常编译,运行,打包发布时,是不会包含这个依赖的。maven2内置了多个作用域,可以让我们将不同阶段使用的依赖库进行隔离。
8 其它标签description 当前项目的一个细节描述,当需要描述这个项目的时候被Maven所用,比如在web 站点中。 这个元素能够被指定为CDATA 类型,允许在描述器中 HTML的tags, 并不鼓励用空白文本来表示。 如果你需要去修改生成的web 站点的索引页,你能够用你自己的索引来代替自动生成的文本。
url 当前项目的主页的URL prerequisites 描述当前项目的编译环境的先决条件
issueManagement 当前项目的发布管理信息。
ciManagement 当前项目的连续集成信息。
inceptionYear 当前项目开始的年份, 用4位数字描述。 涉及到介绍情况时用作提供版权信息mailingLists 包含的信息包括邮件列表developers 描述当前的项目的开发人员的信息contributors 描述对当前项目有贡献的人员的信息,不特指开发人员licenses 这个元素描述了当前项目的所有的许可文件。每一个许可文件用一个许可元素来描述,然后描述额外的元素。 通常只列出适用于这个项目的许可文件以及适用于 依赖的非licenses.如果多个licenses都列出来了,那么假设这个用户选择其中的所需的,而不是接受所有的许可文件。
scm 指定当前项目中的版本控制工具,比如CVS, Subversion, 等等。
organization 这个元素描述这个项目所属组织的各种属性的描述。这些属性应用于文档创建的时候 (版权通知和链接)。
repositories 发现依赖和扩展的远程资源库。
pluginRepositories 发现plugins 的远程资源库的列表,主要是为了编译和报告dependencies 这个元素描述了所有与当前项目相关的依赖。这些依赖被用作创建一个编译时的路径。 他们被自动的从资源库中下在下来依据当前项目的定义。如需更多信息,参看 the dependency mechanism reports Deprecated.禁止适用。现在的版本中被 Maven所忽略掉。
dependencyManagement 缺省的依赖信息将会从这个元素中继承。这些依赖在这一部分中被不立刻被解决的。当一个源于这个POM的元素描述了一个依赖根据匹配的 groupId 和 artifactId,这个部分的版本和其他值用作那些还没有指定的依赖。
distributionManagement 对于一个项目分配的信息允许对于远程web服务器和资源库的site和artifacts配置。
properties 属性可以作为POM的自始自终的替换物,如果可行的话可以用作resources 的过滤器,格式是<name>value</name>.案例如下:
<!-- 项目属性 --> <project> <properties> <jdbc.driver.groupId>mysql</jdbc.driver.groupId> <jdbc.driver.artifactId>mysql-connector-java</jdbc.driver.artifactId> <jdbc.driver.version>5.1.11</jdbc.driver.version> </properties> </project>
9 可以通过以下方式进行安装:
使用以下的命令安装:
mvn install:install-file –Dfile=non-maven-proj.jar –DgroupId=some.group –DartifactId=non-maven-proj –Dversion=1
创建自己的库,并配置,使用deploy:deploy-file
设置此依赖范围为system,定义一个系统路径。不提倡。
type:相应的依赖产品包形式,如jar,war
scope:用于限制相应的依赖范围,包括以下的几种变量:
compile :默认范围,用于编译
provided:类似于编译,但支持你期待jdk或者容器提供,类似于classpath
runtime:在执行时,需要使用
test:用于test任务时使用
system:需要外在提供相应得元素。通过systemPath来取得
systemPath: 仅用于范围为system。提供相应的路径
optional: 标注可选,当项目自身也是依赖时。用于连续依赖时使用