:notebook: 本文已归档到:「blog」html
POM 是 Project Object Model 的缩写,即项目对象模型。java
pom.xml 就是 maven 的配置文件,用以描述项目的各类信息。git
<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
是 pom.xml 中描述符的根。modelVersion
指定 pom.xml 符合哪一个版本的描述符。maven 2 和 3 只能为 4.0.0。通常 jar 包被识别为: groupId:artifactId:version
的形式。github
<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 包。web
1.0-SNAPSHOT
。各个部分的含义和处理逻辑以下说明:
<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
意义相同。packaging
的类型,若是不使用 type
标签,maven 默认为 jar。scope
标签,maven 默认为这个范围。编译依赖关系在全部 classpath 中均可用。此外,这些依赖关系被传播到依赖项目。propertie
来指定特定的路径,如$ {java.home} / lib。因为假定先前安装了系统范围依赖关系,maven 将不会检查项目的仓库,而是检查库文件是否存在。若是没有,maven 将会失败,并建议您手动下载安装。optional
让其余项目知道,当您使用此项目时,您不须要这种依赖性才能正常工做。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
元素。在搜索本地和远程存储库以前,它不是必需的,但能够用做 maven 的指示符,以首先搜索给定该项目父级的路径。dependencyManagement
是表示依赖 jar 包的声明。即你在项目中的 dependencyManagement
下声明了依赖,maven 不会加载该依赖,dependencyManagement
声明能够被子 POM 继承。apache
dependencyManagement
的一个使用案例是当有父子项目的时候,父项目中能够利用 dependencyManagement
声明子项目中须要用到的依赖 jar 包,以后,当某个或者某几个子项目须要加载该依赖的时候,就能够在子项目中 dependencies
节点只配置 groupId
和 artifactId
就能够完成依赖的引用。api
dependencyManagement
主要是为了统一管理依赖包的版本,确保全部子项目使用的版本一致,相似的还有plugins
和pluginManagement
。tomcat
子模块列表。dom
<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>
复制代码
build 能够分为 "project build" 和 "profile build"。
<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">
...
<!-- "Project Build" contains more elements than just the BaseBuild set -->
<build>...</build>
<profiles>
<profile>
<!-- "Profile Build" contains a subset of "Project Build"s elements -->
<build>...</build>
</profile>
</profiles>
</project>
复制代码
基本构建配置:
<build>
<defaultGoal>install</defaultGoal>
<directory>${basedir}/target</directory>
<finalName>${artifactId}-${version}</finalName>
<filters>
<filter>filters/filter1.properties</filter>
</filters>
...
</build>
复制代码
defaultGoal : 默认执行目标或阶段。若是给出了一个目标,它应该被定义为它在命令行中(如 jar:jar)。若是定义了一个阶段(如安装),也是如此。
directory :构建时的输出路径。默认为:${basedir}/target
。
finalName :这是项目的最终构建名称(不包括文件扩展名,例如:my-project-1.0.jar)
filter :定义 * .properties
文件,其中包含适用于接受其设置的资源的属性列表(以下所述)。换句话说,过滤器文件中定义的“name = value”对在代码中替换$ {name}字符串。
资源的配置。资源文件一般不是代码,不须要编译,而是在项目须要捆绑使用的内容。
<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">
<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>
复制代码
* .properties
文件没必要定义为进行过滤 - 资源还可使用默认状况下在 POM 中定义的属性(例如$ {project.version}),并将其传递到命令行中“-D”标志(例如,“-Dname = value”)或由 properties 元素显式定义。过滤文件覆盖上面。${basedir}/src/main/resources
。includes
相似,指定目录中要排除的文件,使用*做为通配符。注意:若是 include
和 exclude
发生冲突,maven 会以 exclude
做为有效项。testResources
与 resources
功能相似,区别仅在于:testResources
指定的资源仅用于 test 阶段,而且其默认资源目录为:${basedir}/src/test/resources
。<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">
<build>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<extensions>false</extensions>
<inherited>true</inherited>
<configuration>
<classifier>test</classifier>
</configuration>
<dependencies>...</dependencies>
<executions>...</executions>
</plugin>
</plugins>
</build>
</project>
复制代码
groupId, artifactId, version :和基本配置中的 groupId
、artifactId
、version
意义相同。
extensions :值为 true 或 false。是否加载此插件的扩展名。默认为 false。
inherited :值为 true 或 false。这个插件配置是否应该适用于继承自这个插件的 POM。默认值为 true。
configuration - 这是针对我的插件的配置,这里不扩散讲解。
dependencies :这里的 dependencies
是插件自己所须要的依赖。
executions :须要记住的是,插件可能有多个目标。每一个目标可能有一个单独的配置,甚至可能将插件的目标彻底绑定到不一样的阶段。执行配置插件的目标的执行。
<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">
...
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>echodir</id>
<goals>
<goal>run</goal>
</goals>
<phase>verify</phase>
<inherited>false</inherited>
<configuration>
<tasks>
<echo>Build Dir: ${project.build.directory}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
复制代码
与 dependencyManagement
很类似,在当前 POM 中仅声明插件,而不是实际引入插件。子 POM 中只配置 groupId
和 artifactId
就能够完成插件的引用,且子 POM 有权重写 pluginManagement 定义。
它的目的在于统一全部子 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">
...
<build>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>${basedir}/src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${basedir}/src/test/java</testSourceDirectory>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
...
</build>
</project>
复制代码
目录元素集合存在于 build
元素中,它为整个 POM 设置了各类目录结构。因为它们在配置文件构建中不存在,因此这些不能由配置文件更改。
若是上述目录元素的值设置为绝对路径(扩展属性时),则使用该目录。不然,它是相对于基础构建目录:${basedir}
。
扩展是在此构建中使用的 artifacts 的列表。它们将被包含在运行构建的 classpath 中。它们能够启用对构建过程的扩展(例如为 Wagon 传输机制添加一个 ftp 提供程序),并使活动的插件可以对构建生命周期进行更改。简而言之,扩展是在构建期间激活的 artifacts。扩展不须要实际执行任何操做,也不包含 Mojo。所以,扩展对于指定普通插件接口的多个实现中的一个是很是好的。
<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">
...
<build>
...
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ftp</artifactId>
<version>1.0-alpha-3</version>
</extension>
</extensions>
...
</build>
</project>
复制代码
报告包含特定针对 site
生成阶段的元素。某些 maven 插件能够生成 reporting
元素下配置的报告,例如:生成 javadoc 报告。reporting
与 build
元素配置插件的能力类似。明显的区别在于:在执行块中插件目标的控制不是细粒度的,报表经过配置 reportSet
元素来精细控制。而微妙的区别在于 reporting
元素下的 configuration
元素能够用做 build
下的 configuration
,尽管相反的状况并不是如此( build
下的 configuration
不影响 reporting
元素下的 configuration
)。
另外一个区别就是 plugin
下的 outputDirectory
元素。在报告的状况下,默认输出目录为 ${basedir}/target/site
。
<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">
...
<reporting>
<plugins>
<plugin>
...
<reportSets>
<reportSet>
<id>sunlink</id>
<reports>
<report>javadoc</report>
</reports>
<inherited>true</inherited>
<configuration>
<links>
<link>http://java.sun.com/j2se/1.5.0/docs/api/</link>
</links>
</configuration>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
...
</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">
...
<!-- 项目信息 begin -->
<!--项目名-->
<name>maven-notes</name>
<!--项目描述-->
<description>maven 学习笔记</description>
<!--项目url-->
<url>https://github.com/dunwu/maven-notes</url>
<!--项目开发年份-->
<inceptionYear>2017</inceptionYear>
<!--开源协议-->
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
<comments>A business-friendly OSS license</comments>
</license>
</licenses>
<!--组织信息(如公司、开源组织等)-->
<organization>
<name>...</name>
<url>...</url>
</organization>
<!--开发者列表-->
<developers>
<developer>
<id>victor</id>
<name>Zhang Peng</name>
<email>forbreak at 163.com</email>
<url>https://github.com/dunwu</url>
<organization>...</organization>
<organizationUrl>...</organizationUrl>
<roles>
<role>architect</role>
<role>developer</role>
</roles>
<timezone>+8</timezone>
<properties>...</properties>
</developer>
</developers>
<!--代码贡献者列表-->
<contributors>
<contributor>
<!--标签内容和<developer>相同-->
</contributor>
</contributors>
<!-- 项目信息 end -->
...
</project>
复制代码
这部分标签都很是简单,基本都能作到顾名思义,且都属于无关紧要的标签,因此这里仅简单介绍一下:
name - 项目完整名称
description - 项目描述
url - 通常为项目仓库的 host
inceptionYear - 开发年份
licenses - 开源协议
organization - 项目所属组织信息
developers - 项目开发者列表
contributors - 项目贡献者列表,<contributor>
的子标签和 <developer>
的彻底相同。
这定义了所使用的缺陷跟踪系统(Bugzilla,TestTrack,ClearQuest 等)。虽然没有什么能够阻止插件使用这些信息的东西,但它主要用于生成项目文档。
<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">
...
<issueManagement>
<system>Bugzilla</system>
<url>http://127.0.0.1/bugzilla/</url>
</issueManagement>
...
</project>
复制代码
CI 构建系统配置,主要是指定通知机制以及被通知的邮箱。
<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">
...
<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>
<configuration><address>continuum@127.0.0.1</address></configuration>
</notifier>
</notifiers>
</ciManagement>
...
</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">
...
<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>
...
</project>
复制代码
SCM(软件配置管理,也称为源代码/控制管理或简洁的版本控制)。常见的 scm 有 svn 和 git 。
<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">
...
<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>
...
</project>
复制代码
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">
...
<prerequisites>
<maven>2.0.6</maven>
</prerequisites>
...
</project>
复制代码
repositories
是遵循 Maven 存储库目录布局的 artifacts 集合。默认的 Maven 中央存储库位于https://repo.maven.apache.org/maven2/上。
<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">
...
<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>
...
</project>
复制代码
与 repositories
差很少。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<distributionManagement>
...
<downloadUrl>http://mojo.codehaus.org/my-project</downloadUrl>
<status>deployed</status>
</distributionManagement>
...
</project>
复制代码
它管理在整个构建过程当中生成的 artifact 和支持文件的分布。从最后的元素开始:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<distributionManagement>
...
<downloadUrl>http://mojo.codehaus.org/my-project</downloadUrl>
<status>deployed</status>
</distributionManagement>
...
</project>
复制代码
repository - 与 repositories
类似
site - 站点信息
relocation - 项目迁移位置
activation
是一个 profile
的关键。配置文件的功能来自于在某些状况下仅修改基本 POM 的功能。这些状况经过 activation
元素指定。
<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>
复制代码