| title | Maven 之 pom.xml 详解(一) | ||||
|---|---|---|---|---|---|
| date | 2016/11/10 | ||||
| categories |
|
||||
| tags |
|
POM 是 Project Object Model 的缩写,即项目对象模型。
pom.xml 就是 maven 的配置文件,用以描述项目的各种信息。
<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>
<!-- The Basics -->
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<packaging>...</packaging>
<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>- project:
project是 pom.xml 中描述符的根。 - modelVersion:
modelVersion指定 pom.xml 符合哪个版本的描述符。maven 2 和 3 只能为 4.0.0。
一般 jar 包被识别为: groupId:artifactId:version 的形式。
<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.codehaus.mojo</groupId>
<artifactId>my-project</artifactId>
<version>1.0</version>
<packaging>war</packaging>
</project>在 maven 中,根据 groupId、artifactId、version 组合成 groupId:artifactId:version 来唯一识别一个 jar 包。
-
groupId:团体、组织的标识符。团体标识的约定是,它以创建这个项目的组织名称的逆向域名(reverse domain name)开头。一般对应着 java 的包结构。
-
artifactId:单独项目的唯一标识符。比如我们的 tomcat、commons 等。不要在artifactId中包含点号(.)。
-
version:一个项目的特定版本。
maven 有自己的版本规范,一般是如下定义 major version、minor version、incremental version-qualifier ,比如1.2.3-beta-01。要说明的是,maven 自己判断版本的算法是 major、minor、incremental 部分用数字比较,qualifier部分用字符串比较,所以要小心 alpha-2 和 alpha-15 的比较关系,最好用 alpha-02 的格式。
maven 在版本管理时候可以使用几个特殊的字符串 SNAPSHOT、LATEST、RELEASE。比如 “1.0-SNAPSHOT”。各个部分的含义和处理逻辑如下说明:
- SNAPSHOT:这个版本一般用于开发过程中,表示不稳定的版本。
- LATEST:指某个特定构件的最新发布,这个发布可能是一个发布版,也可能是一个 snapshot 版,具体看哪个时间最后。
- RELEASE :指最后一个发布版。
-
packaging:项目的类型,描述了项目打包后的输出,默认是 jar。常见的输出类型为:pom, jar, maven-plugin, ejb, war, ear, rar, par。
<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">
...
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>2.0</version>
<type>jar</type>
<scope>test</scope>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
</exclusion>
</exclusions>
</dependency>
...
</dependencies>
...
</project>- groupId, artifactId, version :和基本配置中的
groupId、artifactId、version意义相同。 - type:对应
packaging的类型,如果不使用type标签,maven 默认为 jar。 - scope:此元素指的是任务的类路径(编译和运行时,测试等)以及如何限制依赖关系的传递性。有 5 种可用的限定范围:
- compile - 如果没有指定
scope标签,maven 默认为这个范围。编译依赖关系在所有 classpath 中都可用。此外,这些依赖关系被传播到依赖项目。 - provided - 与 compile 类似,但是表示您希望 jdk 或容器在运行时提供它。它只适用于编译和测试 classpath,不可传递。
- runtime - 此范围表示编译不需要依赖关系,而是用于执行。它是在运行时和测试 classpath,但不是编译 classpath。
- test - 此范围表示正常使用应用程序不需要依赖关系,仅适用于测试编译和执行阶段。它不是传递的。
- system - 此范围与 provided 类似,除了您必须提供明确包含它的 jar。该 artifact 始终可用,并且不是在仓库中查找。
- compile - 如果没有指定
- systemPath:仅当依赖范围是系统时才使用。否则,如果设置此元素,构建将失败。该路径必须是绝对路径,因此建议使用
propertie来指定特定的路径,如$ {java.home} / lib。由于假定先前安装了系统范围依赖关系,maven 将不会检查项目的仓库,而是检查库文件是否存在。如果没有,maven 将会失败,并建议您手动下载安装。 - optional:
optional让其他项目知道,当您使用此项目时,您不需要这种依赖性才能正常工作。 - exclusions:包含一个或多个排除元素,每个排除元素都包含一个表示要排除的依赖关系的
groupId和artifactId。与可选项不同,可能或可能不会安装和使用,排除主动从依赖关系树中删除自己。
maven 支持继承功能。子 POM 可以使用 parent 指定父 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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<relativePath>../my-parent</relativePath>
</parent>
<artifactId>my-project</artifactId>
</project>- relativePath:注意
relativePath元素。在搜索本地和远程存储库之前,它不是必需的,但可以用作 maven 的指示符,以首先搜索给定该项目父级的路径。
dependencyManagement 是表示依赖 jar 包的声明。即你在项目中的 dependencyManagement 下声明了依赖,maven不会加载该依赖,dependencyManagement 声明可以被子 POM 继承。
dependencyManagement 的一个使用案例是当有父子项目的时候,父项目中可以利用 dependencyManagement 声明子项目中需要用到的依赖 jar 包,之后,当某个或者某几个子项目需要加载该依赖的时候,就可以在子项目中 dependencies 节点只配置 groupId 和 artifactId 就可以完成依赖的引用。
dependencyManagement 主要是为了统一管理依赖包的版本,确保所有子项目使用的版本一致,类似的还有plugins和pluginManagement。
子模块列表。
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.mojo</groupId>
<artifactId>my-parent</artifactId>
<version>2.0</version>
<packaging>pom</packaging>
<modules>
<module>my-project</module>
<module>another-project</module>
<module>third-project/pom-example.xml</module>
</modules>
</project>属性列表。定义的属性可以在 pom.xml 文件中任意处使用。使用方式为 ${propertie} 。
<project>
...
<properties>
<maven.compiler.source>1.7<maven.compiler.source>
<maven.compiler.target>1.7<maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
...
</project>