POM
POM 全称是 Project Object Model ,即项目对象模型。 pom.xml 是 maven 的项目描述文件。 pom.xml 文件以 xml 的形式描述项目的信息,包括项目名称、版本、项目 id、项目的依赖关系、编译环境、持续集成、项目团队、贡献管理、生成报表等等。
基本配置
<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>org.seandeng</groupId>
<artifactId>myapp</artifactId>
<version>1.0</version>
</project>
modelVersion 描述这个 POM 文件是遵从哪个版本的项目描述符,目前只能是 4.0.0。
groupId 针对一个项目的普遍唯一识别符。通常用一个完全正确的包的名字来与其他项目的类似名字来进行区分(比如: org.apache.maven) 。
artifactId 在给定 groupID 的 group 里面为 artifact 指定的标识符是唯一的 , artifact代表的是被制作或者被一个 project 应用的组件 ( 产出物 ) 。
version 当前项目产生的 artifact 的版本
以上 4 个元素缺一不可,其中 groupId, artifactId, version 描述依赖的项目唯一标志。
文件结构
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging> // 打包机制,如 pom,jar,maven-plugin,ejb,war,ear,rar,par
<dependencies>...</dependencies>
<parent>...</parent>
<dependencyManagement>...</dependencyManagement>
<modules>...</modules>
<properties>...</properties>
<!- Build Settings 项目的编译设置 ->
<build>...</build>
<reporting>...</reporting>
<!- More Project Information 其它项目信息 ->
<name>...</name> // 描述项目的名称
<description>...</description>
<url>...</url> // 写明开发团队的网站
<inceptionYear>...</inceptionYear>
<licenses>...</licenses>
<organization>...</organization>
<developers>...</developers>
<contributors>...</contributors>
<!-- Environment Settings ->
<issueManagement>...</issueManagement>
<ciManagement>...</ciManagement>
<mailingLists>...</mailingLists>
<scm>...</scm>
<prerequisites>...</prerequisites>
<repositories>...</repositories>
<pluginRepositories>...</pluginRepositories>
<distributionManagement>...</distributionManagement>
<profiles>...</profiles>
</project>
POM 很重要的 3 个关系
POM 有 3 个很重要的关系:依赖、继承、合成。
依赖关系
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<type>jar</type>
<scope>test</scope>
<optional>true</optional>
</dependency>
...
</dependencies>
如果想依赖一个 maven 库中没有的一个 jar 包,方法很简单,就是先将此 jar 包使用以下的命令安装到本地 maven 库中:
mvn install:install-file -Dfile=my.jar -DgroupId=mygroup -DartifactId=myartifactId -Dversion=1
再把依赖关系加进去即可。
继承关系
父项目配置
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.seandeng</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<packaging>pom</packaging>
</project>
packaging 类型,定义值为 pom 用于定义为 parent 和合成多个项目。 当然我们创建的 maven 项目的 pom 都继承 maven 的 super pom , 如果想看项目 ( 父或子 ) 的完全的 pom 结构,可以运行:
mvn help:effective-pom
就可以了。
子项目配置
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.seandeng</groupId>
<artifactId>my-child-project</artifactId>
<parent>
<groupId>com.seandeng</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<relativePath>../my-parent</relativePath>
</parent>
</project>
relativePath 可以不需要,但是用于指明 parent 的目录,用于快速查询。
合成关系
一个项目有多个模块。 如下的定义:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.seandeng</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<modules>
<module>my-child-project1<module>
<module>my-child-project2<module>
</modules>
</project>
其中 module 描述的是子项目的相对路径 。
dependencies
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<type>jar</type>
<scope>test</scope>
<optional>true</optional>
</dependency>
scope: compile (编译范围) , provided (已提供范围) , runtime (运行时范围) , test (测试范围) , system (系统范围)
exclusions : 如果 X 需要 A,A 包含 B 依赖,那么 X 可以声明不要 B 依赖,只要在exclusions 中声明 exclusion.
Properties
POM 片段:
<properties>
<hibernate.annotations.version>3.3.0.ga</hibernate.annotations.version>
</properties>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>${hibernate.annotations.version}</version>
</dependency>
特殊的属性引用片段:
<parent>
… …
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
… …
<version>${project.version}</version>// 这个值是一个属性引用,指向了 POM 的project/version 的值 。
</dependency>
</dependencies>
dependencyManagement
Maven 还我们提供了一个 dependencyManagement 元素,用来提供了一种方式来统一依赖版本号。 dependencyManagement 元素一 般用在顶层的父 POM 。使用pom.xml 中的 dependencyManagement 元素能让你在子项目中引用一个依赖而不用显式的列出版本号。 Maven 会沿着父子层次向上走,直到找到一个拥有dependencyManagement 元素的项目,然后它就会使用在这个dependencyManagement 元素中指定的版本号,这样就解决了修改依赖版本号不完全的问题。示例如下:
顶层的父 POM 片段:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.0.7</version>
</dependency>
</dependencies>
</dependencyManagement>
子项目 POM 片段:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</dependency>
</dependencies>
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>
release 和 snapshots :是 artifact 的两种 policies , pom 可以选择那种政策有效。
updatePolicy:
说明更新发生的频率 always 或者 never 或者 daily (默认的)或者 interval:X ( X 是分钟数)
checksumPolicy :当 Maven 的部署文件到仓库中,它也部署了相应的校验和文件。您可以选择忽略,失败,或缺少或不正确的校验和警告。
layout : maven1.x 与 maven2 有不同的 layout ,所以可以声明为 default 或者是legacy (遗留方式 maven1.x )。
Profile
Maven 的 Profile 元素可以为一个特殊的环境自定义一个特殊的构建,使得不同环境间构建的可移植性成为可能。比如要使用 production profile 来运行 mvn install ,你需要在命令行传入 -Pproduction 参数,这里 production 是 profile 的 id 。要验证production profile 覆盖了默认的 Compiler 插件配置,可以像这样以开启调试输入 (-X)的方式运行 Maven 。
它包含可选的 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>
<file>
<exists>${basedir}/file2.properties</exists>
<missing>${basedir}/file1.properties</missing>
</file>
</activation>