目录
POM是项目对象模型(Project Object Model)的简称,它是Maven项目中的文件,XML格式,名称为 pom.xml,作用类似 ant 的 build.xml 文件,功能则更强大。该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。事实上,在Maven的世界中,一个项目可以什么都没有,甚至没有代码,但是必须包含pom.xml文件。
关于Maven的详细介绍请参见《Maven 从入门到精通》。
pom文件约定了一个maven项目的maven配置,一般 pom文件放在项目或者模块的根目录下。
maven 遵循约定大于配置,约定了如下的目录结构:
| 目录 | 说明 |
|---|---|
| ${basedir} | 存放pom.xml和所有的子目录 |
| ${basedir}/src/main/java | 项目的java源代码 |
| ${basedir}/src/main/resources | 项目的资源,比如说property文件,springmvc.xml |
| ${basedir}/src/test/java | 项目的测试类,比如说Junit代码 |
| ${basedir}/src/test/resources | 测试用的资源 |
| ${basedir}/src/main/scripts | 项目脚本源码的目录 |
| ${basedir}/src/main/webapp/WEB-INF | web应用文件目录,web项目的信息,比如存放web.xml、本地图片、jsp视图页面 |
| ${basedir}/target | 打包输出目录 |
| ${basedir}/target/classes | 编译输出目录 |
| ${basedir}/target/site | 生成文档的目录,可以通过index.html查看项目的文档 |
| ${basedir}/target/test-classes | 测试编译输出目录 |
| Test.java | Maven只会自动运行符合该命名规则的测试类 |
| ~/.m2/repository | 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.0modelVersion>
-
-
- <groupId>com.companyname.project-groupgroupId>
-
-
- <artifactId>projectartifactId>
-
-
- <version>1.0version>
-
-
- <packaging>jarpackaging>
- project>
1. project:整个pom配置文件的根元素,所有的配置都是写在project元素里面的;
2. modelVersion:指定了当前POM模型的版本,对于Maven2及Maven 3来说,它只能是4.0.0;
3. groupId:这是项目组的标识。它在一个组织或者项目中通常是唯一的。
4. artifactId:这是项目的标识,通常是工程的名称。它在一个项目组(group)下是唯一的。
5. version:这是项目的版本号,用来区分同一个artifact的不同版本。
6. packaging:这是项目产生的构件类型,即项目通过maven打包的输出文件的后缀名,包括jar、war、ear、pom等。
- <parent>
-
- <artifactId>com.companyname.project-groupartifactId>
-
- <groupId>base-projectgroupId>
-
- <version>1.0.1-RELEASEversion>
-
-
-
- <relativePath>../pom.xmlrelativePath>
- parent>
所有的pom都继承自一个父pom(Super POM)。父pom包含了一些可以被继承的默认设置,如果项目的pom中没有设置这些元素,就会使用父pom中设置。例如,Super POM中配置了默认仓库http://repo1.maven.org/maven2,这样哪怕项目的pom中没有配置仓库,也可以去这个默认仓库中去下载依赖。实际上,maven中pom文件约定大于配置的原则,就是通过在Super POM中预定义了一些配置信息来实现的。
Maven使用effective pom(Super pom加上工程自己的配置)来执行相关的目标,它帮助开发者在pom.xml中做尽可能少的配置。当然,这些配置也可以被重写。
1. parent:用于指定父项目;
2. groupId:parent的子元素,父项目的groupId,用于定位父项目;
3. artifactId:parent的子元素,父项目的artifactId,用于定位父项目;
4. version:parent的子元素,父项目的version,用于定位父项目;
5. relativePath:parent的子元素,用于定位父项目pom文件的位置。
- <build>
-
-
- <sourceDirectory />
-
- <testSourceDirectory />
-
- <outputDirectory />
-
- <testOutputDirectory />
-
- <scriptSourceDirectory />
-
-
-
- <resources>
-
- <resource>
-
-
- <targetPath />
-
- <filtering />
-
- <directory />
-
- <includes />
-
- <excludes />
- resource>
- resources>
-
- <testResources>
-
- <testResource>
- <targetPath />
- <filtering />
- <directory />
- <includes />
- <excludes />
- testResource>
- testResources>
-
-
-
-
- <pluginManagement>
-
- <plugins>
-
- <plugin>
-
- <groupId />
-
- <artifactId />
-
- <version />
-
-
- <extensions />
-
- <executions>
-
- <execution>
-
- <id />
-
- <phase />
-
- <goals />
-
- <inherited />
-
- <configuration />
- execution>
- executions>
-
- <dependencies>
-
- <dependency>
- ......
- dependency>
- dependencies>
-
- <inherited />
-
- <configuration />
- plugin>
- plugins>
- pluginManagement>
-
- <plugins>
-
- <plugin>
- <groupId />
- <artifactId />
- <version />
- <extensions />
- <executions>
- <execution>
- <id />
- <phase />
- <goals />
- <inherited />
- <configuration />
- execution>
- executions>
- <dependencies>
-
- <dependency>
- ......
- dependency>
- dependencies>
- <goals />
- <inherited />
- <configuration />
- plugin>
- plugins>
-
-
-
- <extensions>
-
- <extension>
-
- <groupId />
- <artifactId />
- <version />
- extension>
- extensions>
-
-
-
- <defaultGoal />
-
- <directory />
-
- <finalName />
-
- <filters />
- build>
- <sourceDirectory />
- <testSourceDirectory />
- <outputDirectory />
- <testOutputDirectory />
- <scriptSourceDirectory />
路径管理定义了各种源码和编译结果的输出路径。如果遵循maven默认的路径约定,这里的几个元素是不需要配置的。这些元素包括:
1. sourceDirectory:项目源码目录,定义的是相对于pom文件的相对路径;
2. testSourceDirectory:项目单元测试源码目录,定义的也是是相对于pom文件的相对路径;
3. outputDirectory:被编译过的应用程序class文件存放的目录,也是是相对于pom文件的相对路径;
4. testOutoutDIrectory:被编译过的测试class文件存放的目录,也是是相对于pom文件的相对路径;
5. scriptSourceDirectory:项目脚本源码目录,也是是相对于pom文件的相对路径。由于脚本是解释性的语言,所以该目录下的内容,会直接被拷贝到输出目录,而不需要编译。
- <resources>
-
- <resource>
-
-
- <targetPath />
-
- <filtering />
-
- <directory />
-
- <includes />
-
- <excludes />
- resource>
- resources>
- <testResources>
-
- <testResource>
- <targetPath />
- <filtering />
- <directory />
- <includes />
- <excludes />
- testResource>
- testResources>
这里的元素主要是对应用程序resource资源和单元测试部分resource资源的管理,分别通过resource标签和testResource标签管理两种资源。两个标签元素可选的子元素都是一样的。子元素包括:
1. targetPath:描述了资源的目标输出路径,该路径是相对于target/classes的路径;
2. filtering:对文件中的参数值进行过滤,需要被过滤的文件在filter中指定;
3. directory:描述打包前的资源的存放路径,这个路径是相对于pom文件所在目录的相对路径;
4. includes:包含的模式列表,例如**/*.xml。只有符合条件的资源文件才会在打包的时候被放入到输出路径中;
5. excludes:排除的模式列表,例如**/*.xml,符合的资源文件不会在打包的时候会被过滤掉。
插件管理相关的元素有两个,包括pluginManagement和plugins。pluginManagement中有子元素plugins,它和project下的直接子元素plugins的区别是,pluginManagement主要是用来声明子项目可以引用的默认插件信息,这些插件如果只写在pluginManagement中是不会被引入的。project下的直接子元素plugins中定义的才是这个项目中真正需要被引入的插件。
- <pluginManagement>
-
- <plugins>
-
- <plugin>
-
- <groupId />
- <artifactId />
- <version />
-
-
- <extensions />
-
- <executions>
-
- <execution>
-
- <id />
-
- <phase />
-
- <goals />
-
- <inherited />
-
- <configuration />
- execution>
- executions>
-
- <dependencies>
- ......
- dependencies>
-
- <inherited />
-
- <configuration />
- plugin>
- plugins>
- pluginManagement>
- <plugins>
- ......
- plugins>
extensions 是在此构建中使用的项目的列表,它们将被包含在运行构建的classpath中。这些项目可以启用对构建过程的扩展,并使活动的插件能够对构建生命周期进行更改。简而言之,扩展是在构建期间激活的artifacts。扩展不需要实际执行任何操作,也不包含 Mojo。因此,扩展对于指定普通插件接口的多个实现中的一个是非常好的。
- <extensions>
-
- <extension>
-
- <groupId />
- <artifactId />
- <version />
- extension>
- extensions>
- <defaultGoal />
- <directory />
- <finalName />
- <filters />
pom文件中通过dependencyManagement来声明依赖,通过dependencies元素来管理依赖。dependencyManagement下的子元素只有一个直接的子元素dependencices,其配置和dependencies子元素是完全一致的;而dependencies下只有一类直接的子元素:dependency。一个dependency子元素表示一个依赖项目。
- <dependencies>
- <dependency>
-
-
- <groupId>org.apache.mavengroupId>
- <artifactId>maven-artifactartifactId>
- <version>3.8.1version>
-
-
-
-
-
- <type>jartype>
-
-
- <classifier>classifier>
-
-
-
- <exclusions>
- <exclusion>
- <artifactId>spring-coreartifactId>
- <groupId>org.springframeworkgroupId>
- exclusion>
- exclusions>
-
- <optional>trueoptional>
-
-
-
- <scope>testscope>
-
-
- <systemPath>systemPath>
- dependency>
- dependencies>
-
- <dependencyManagement>
- <dependencies>
-
- <dependency>
- ......
- dependency>
- dependencies>
- dependencyManagement>
这里也是根据元素的作用,简单的对dependency的子元素做了一下分类。下面按分类来看一下dependency的子元素:
依然是通过groupId + artifactId + version来在仓库中定位一个项目:
1. groupId:parent的子元素,父项目的groupId,用于定位父项目;
2. artifactId:parent的子元素,父项目的artifactId,用于定位父项目;
3. version:parent的子元素,父项目的version,用于定位父项目。
这个分类主要包括两个元素,分别是依赖类型和依赖的分类器。同一个项目,即使打包成同一种类型,也可以有多个文件同时存在,因为它们的分类器可能是不同的。
1. type:依赖类型,默认是jar。通常表示依赖文件的扩展名,但也有例外。一个类型可以被映射成另外一个扩展名或分类器。类型经常和使用的打包方式对应,尽管这也有例外,一些类型的例子:jar,war,ejb-client和test-jar。如果设置extensions为true,就可以在plugin里定义新的类型。
2. classifier:依赖的分类器。分类器可以区分属于同一个POM,但不同构建方式的构件。分类器名被附加到文件名的版本号后面,如果想将项目构建成两个单独的JAR,分别使用Java 4和6编译器,就可以使用分类器来生成两个单独的JAR构件。
依赖传递相关的子元素主要有两个,用于依赖排除的exclusions和设置依赖是否可选的optional。
exclusions:排除该项目中的一些依赖,即本项目A依赖该dependency指示的项目B,但是不依赖项目B中依赖的这些依赖;
optional:可选依赖,用于阻断依赖的传递性,即本项目不会依赖父项目中optional设置为true的依赖。
scope:依赖范围。在项目发布过程中,帮助决定哪些构件被包括进来
- compile:默认范围,用于编译。
- provided:类似于编译,但支持jdk或者容器提供,类似于classpath。
- runtime:在执行时需要使用。
- systemPath:仅用于范围为system,提供相应的路径。
- test:用于test任务时使用。
- system:需要外在提供相应的元素,通过systemPath来取得。
- optional:当项目自身被依赖时,标注依赖是否传递。用于连续依赖时使用。
systemPath:该元素为依赖规定了文件系统上的绝对路径。仅在scope设置成system时才会使用。不推荐使用这个元素。不推荐使用绝对路径,如果必须要用,推荐使用属性匹配绝对路径,例如${java.home}。
- <name>project-mavenname>
-
- <url>http://123.a.b/nsnxsurl>
-
- <description>Description of this maven projectdescription>
备注:maven可以通过mvn site命令生成项目的相关文档。
生成文档相关的元素,包括name,url,和description。
name:项目名称,maven生成文档会使用项目名。
url:项目主页的地址,maven生成文档的时候使用。
description:项目描述。如果可以使用HTML格式进行描述的时候,不推荐使用纯文本的描述。
远程仓库列表的配置,包括依赖和扩展的远程仓库配置,以及插件的远程仓库配置。在本地仓库找不到的情况下,maven下载依赖、扩展和插件就是从这里配置的远程仓库中进行下载。
需要注意的是release和snapshot两者的区别。release是稳定版本,一经发布不再修改,想发布修改后的项目,只能升级项目版本再进行发布;snapshot是不稳定的,一个snapshot的版本可以不断改变。项目在开发期间一般会使用snapshot,更方便进行频繁的代码更新;一旦发布到外部,或者开发基本完成,代码迭代不再频繁,则推荐使用release。
- <repositories>
-
- <repository>
-
- <releases>
-
- <enabled />
-
-
- <updatePolicy />
-
- <checksumPolicy />
- releases>
-
-
- <snapshots>
- <enabled />
- <updatePolicy />
- <checksumPolicy />
- snapshots>
-
-
- <id>nanxs-repository-proxyid>
-
- <name>nanxs-repository-proxyname>
-
- <url>http://192.168.1.169:9999/repository/url>
-
- <layout>defaultlayout>
- repository>
- repositories>
-
- <pluginRepositories>
-
- <pluginRepository>
- ......
- pluginRepository>
- pluginRepositories>
- <distributionManagement>
-
- <repository>
-
- <uniqueVersion />
-
- <id>nanxs-maven2id>
- <name>nanxsmaven2name>
- <url>file://${basedir}/target/deployurl>
- <layout />
- repository>
-
- <snapshotRepository>
- <uniqueVersion />
- <id>nanxs-maven2id>
- <name>Nanxs-maven2 Snapshot Repositoryname>
- <url>scp://svn.baidu.com/nanxs:/usr/local/maven-snapshoturl>
- <layout />
- snapshotRepository>
-
- <site>
-
- <id>nanxs-siteid>
-
- <name>business api websitename>
-
- <url>scp://svn.baidu.com/nanxs:/var/www/localhost/nanxs-weburl>
- site>
-
-
- <downloadUrl />
-
- <relocation>
-
- <groupId />
-
- <artifactId />
-
- <version />
-
- <message />
- relocation>
-
-
- <status />
- distributionManagement>
项目分发信息的相关配置,在distributionManagement中设置。设置的内容包括:
1. repository和snapshotRepository:项目产生的构建/快照构建部署的远程仓库。如果不配置snapshotRepository,快照也会部署到repository中;
2. site:部署项目的网站需要的信息;
3. downloadUrl:项目下载页面的URL,这是为不在仓库中的构建提供的;
4. relocation:如果构件有了新的group ID和artifact ID(移到了新的位置),这里列出构件的新的信息;
5. status:给出该构件在远程仓库的状态,本地项目中不能设置该元素,这是工具自动更新的。
报表规范描述的是使用mvn site命令时使用的一些配置。
- <reporting>
-
- <excludeDefaults />
-
- <outputDirectory />
-
- <plugins>
-
- <plugin>
-
- <groupId />
- <artifactId />
- <version />
-
-
- <inherited />
-
- <configuration />
-
-
- <reportSets>
-
- <reportSet>
-
- <id />
-
- <configuration />
-
- <inherited />
-
- <reports />
- reportSet>
- reportSets>
- plugin>
- plugins>
- reporting>
- <profiles>
-
- <profile>
-
- <id />
-
-
- <activation>
-
- <activeByDefault />
-
- <jdk />
-
- <os>
-
- <name>Windows XPname>
-
- <family>Windowsfamily>
-
- <arch>x86arch>
-
- <version>5.1.2600version>
- os>
-
-
- <property>
-
- <name>mavenVersionname>
-
- <value>2.0.3value>
- property>
-
-
- <file>
-
- <exists>/usr/local/abcd/abcd-home/jobs/maven-guide-zh-to-production/workspace/
- exists>
-
- <missing>/usr/local/abcd/abcd-home/jobs/maven-guide-zh-to-production/workspace/
- missing>
- file>
- activation>
-
-
- <build />
-
- <repositories />
-
- <pluginRepositories />
-
- <dependencies />
-
- <reporting />
-
- <dependencyManagement />
-
- <distributionManagement />
-
-
- <reports />
-
- <modules />
-
- <properties />
- profile>
- profiles>
- <ciManagement>
-
- <system />
-
- <url />
-
- <notifiers>
-
- <notifier>
-
- <type />
-
- <sendOnError />
-
- <sendOnFailure />
-
- <sendOnSuccess />
-
- <sendOnWarning />
-
- <address />
-
- <configuration />
- notifier>
- notifiers>
- ciManagement>
-
- <mailingLists>
-
- <mailingList>
-
- <name>Demoname>
-
- <post>nanxs@123.compost>
-
- <subscribe>nanxs@123.comsubscribe>
-
- <unsubscribe>nanxs@123.comunsubscribe>
-
- <archive>http:/a.b.c/nanxs/demo/dev/archive>
- mailingList>
- mailingLists>
- <issueManagement>
-
- <system>jirasystem>
-
- <url>http://jira.baidu.com/nanxsurl>
- issueManagement>
-
- <inceptionYear />
-
- <developers>
-
- <developer>
-
- <id>HELLO WORLDid>
-
- <name>nanxsname>
-
- <email>123@abc.comemail>
-
- <url />
-
- <roles>
- <role>Project Managerrole>
- <role>Architectrole>
- roles>
-
- <organization>demoorganization>
-
- <organizationUrl>http://a.b.com/nanxsorganizationUrl>
-
- <properties>
- <dept>Nodept>
- properties>
-
- <timezone>-5timezone>
- developer>
- developers>
-
- <contributors>
-
- <contributor>
- <name />
- <email />
- <url />
- <organization />
- <organizationUrl />
- <roles />
- <timezone />
- <properties />
- contributor>
- contributors>
-
- <licenses>
-
- <license>
-
- <name>Apache 2name>
-
- <url>http://a.b.com/nanxs/LICENSE-1.0.txturl>
-
- <distribution>repodistribution>
-
- <comments>A business-friendly OSS licensecomments>
- license>
- licenses>
-
- <scm>
-
- <connection>scm:svn:http://a.b.com/nanxsconnection>
-
- <developerConnection>scm:svn:http://a.b.com/nanxsdeveloperConnection>
-
- <tag />
-
- <url>http://a.b.com/nanxsurl>
- scm>
-
- <organization>
-
- <name>demoname>
-
- <url>http://a.b.com/nanxsurl>
- organization>
- <prerequisites>
-
- <maven />
- prerequisites>
-
- <modules />
-
- <properties />
-
- <reports>reports>