pom是Project Object Model(项目对象模型)的缩写,是Maven中的项目文件,可用于管理与配置依赖,组织信息,项目受权,远程仓库等等.一个Maven项目,能够没有任何代码,但不能没有pom.xml.java
<project>是pom.xml的根元素,包含了一些约束信息.web
<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>
<modelVersion>4.0.0</modelVersion>
pom的版本,这是Maven 2&3惟一支持的pom版本,并且不能忽略.spring
<groupId>,<artifactId>与<version>标识了仓库中的一个特定位置,叫项目坐标.三个属性告诉了Maven项目中的一个特定版本,让Maven知道如何处理它们以及在生命周期内的哪一阶段须要它们.shell
<groupId>表示项目所属的组,一般是一个公司或者组织的名称,如org.springframework.apache
<artifactId>表示项目的惟一标识.bash
<version>表示项目的版本号,一般来讲项目的版本号分红三段:
主版本号.次版本号.修订版本号服务器
版本号的后缀意味着项目的不一样阶段:架构
打包类型,没有提供的话默认值为jar,常见的有jar与war,也能够取值:app
Maven的一个强大之处是处理项目关系的方式,能够经过一个公共的本地仓库去解决问题.maven
POM的基础就是依赖列表,Maven下载与在编译时连接依赖与其余所须要的目标,并且能够处理传递性依赖,使列表能够专一于项目所需的依赖.依赖放在<dependencies>里面,包含若干个<dependency>.
<dependencies> <dependency> .... </dependency> <dependency> .... </dependency> </dependencies>
一个<dependency>一般包含:
对应项目坐标
版本
可用于配置不一样jdk的<depenency>,好比让一个<dependency>同时支持jdk8与jdk11,能够选择使用哪个<classifier>,方便在不一样jdk中使用.
对应的依赖类型,默认为jar,一般对应与<packaging>.
scope表示类库与项目的关系,能够取如下5个值:
当<scope>为system才须要这个,不然(当<scope>不为system时)会构建失败.路径必须为绝对路径.
标记依赖的可选状态.
排除不须要的依赖,包含子元素<exclusion>,每一个<exclusion>都包含<groupId>与<artifactId>.
使用<parent>指定须要继承的pom.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> </parent>
子pom会继承父pom的<groupId>,<version>,<build>等众多属性,具体包括:
但不能继承:
另外,就像java中全部类都继承于java.lang.Object同样,全部POM都有一个"Super POM",pom都从它继承而来,下面是Maven3.5.4的"Super pom":
<project> <modelVersion>4.0.0</modelVersion> <repositories> <repository> <id>central</id> <name>Central Repository</name> <url>https://repo.maven.apache.org/maven2</url> <layout>default</layout> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <name>Central Repository</name> <url>https://repo.maven.apache.org/maven2</url> <layout>default</layout> <snapshots> <enabled>false</enabled> </snapshots> <releases> <updatePolicy>never</updatePolicy> </releases> </pluginRepository> </pluginRepositories> <build> <directory>${project.basedir}/target</directory> <outputDirectory>${project.build.directory}/classes</outputDirectory> <finalName>${project.artifactId}-${project.version}</finalName> <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory> <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory> <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory> <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory> <resources> <resource> <directory>${project.basedir}/src/main/resources</directory> </resource> </resources> <testResources> <testResource> <directory>${project.basedir}/src/test/resources</directory> </testResource> </testResources> <pluginManagement> <!-- NOTE: These plugins will be removed from future versions of the super POM --> <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) --> <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <version>1.3</version> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> </plugin> <plugin> <artifactId>maven-dependency-plugin</artifactId> <version>2.8</version> </plugin> <plugin> <artifactId>maven-release-plugin</artifactId> <version>2.5.3</version> </plugin> </plugins> </pluginManagement> </build> <reporting> <outputDirectory>${project.build.directory}/site</outputDirectory> </reporting> <profiles> <!-- NOTE: The release profile will be removed from future versions of the super POM --> <profile> <id>release-profile</id> <activation> <property> <name>performRelease</name> <value>true</value> </property> </activation> <build> <plugins> <plugin> <inherited>true</inherited> <artifactId>maven-source-plugin</artifactId> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar-no-fork</goal> </goals> </execution> </executions> </plugin> <plugin> <inherited>true</inherited> <artifactId>maven-javadoc-plugin</artifactId> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <inherited>true</inherited> <artifactId>maven-deploy-plugin</artifactId> <configuration> <updateReleaseInfo>true</updateReleaseInfo> </configuration> </plugin> </plugins> </build> </profile> </profiles> </project>
模块是pom列出的项目,并做为一个组执行,每一个模块经过pom文件或项目的相对路径进行列出.
<modules> <module>my-project</module> <module>another-project</module> <module>third-project/pom-example.xml</module> </modules>
不须要考虑模块间的依赖关系,Maven会对其进行拓扑排序以便在依赖模块以前构建依赖关系.
属性是Maven中的值占位符,像Ant同样,能够以
${x}
这样的形式在pom.xml的任何位置访问一个值,也能够被用做默认值使用.
有5种形式使用值:
env会使用当前shell的环境变量的值.
例如
${env.PATH}
<project>下的x元素的值,如
${project.version}
使用settings.xml中的元素的值
${settings.offline}
java系统属性值,经过java.lang.System.getProperties()获取,如
${java.home}
直接使用x,用的是<properties>下的属性,好比
<properties> <aaa>AAAAA</aaa> </properties>
${aaa}
<build>,声明项目结构,管理插件等.
目标的默认值,能够取值install,copile
构建产生的文件存放目录
构建最终产生的项目名字,但有可能会被更改.
定义一组<filter>,<filter>内是.properties文件,项目中的占位符如xxx.xxx会被.properties中的xxx=xxx的具体值替换掉.
<resources>,项目相关的资源文件的位置.
描述每一个资源的根元素.
构建资源的位置,对于jar包放在META-INF里面.
取值true或false,表示是否开启过滤
资源位置.
指定要包含的资源,使用*做为通配符.
与include相反,要排除的资源列表.
<plugins>下包含了若干个<plugin>,表示插件,每一个<plugin>有如下元素:
与上面的<groupId>与<artifactId>同样.
与上面的<version>同样.
取值true或false,表示是否加载扩展,默认为false.
取值ture或false,是否应用pom的继承关系,默认true.
插件项的相关配置,能够配置<finalName>,<appendAssemblyld>,<descriptor>等.
引入插件的依赖,与前面的<dependencies>相似.
插件可能有多个目标,<executions>配置每个<execution>做为插件的目标,在<execution>中,用<id>指定执行目标的标识符,用<goals>指定目标,<goals>包含一组<goal>,<phase>用于指定阶段,<inherited>用于指定是否启用继承关系.另外<execution>也能够包含<configuration>,与上面相似,用于配置特定的目标,而不是插件的全部目标.
<pluginManagement>,包含一组<plugins>,继承于此项目的子项目均可以使用,子项目能够覆盖修改<pluginManagement>.
能够为pom设置各类目录,好比
<sourceDirectory></sourceDirectory>
构建项目时会编译该目录的源码,是相对于pom.xml的相对路径.
<testSourceDirectory></testSourceDirectory>
测试时会编译其中的源码,也是相对于pom.xml的相对路径.
<outputDirectory></outputDirectory>
这里存放被编译过的class文件.
<testOutputDirectory></testOutputDirectory>
存放测试文件编译后的class文件.
<extensions>,将包含在运行中的构建的类路径中,在构建过程当中能够激活扩展.好比能够为,例如这是支持ftp的wagon-ftp插件:
<build> <extensions> <extension> <groupId>org.apache.maven.wagon</groudId> <artifactId>wagon-ftp</artifactId> <version>3.3.4</version> </extension> </extensions> </build>
<reporting>,描述产生报表的规范等,执行"mvn site"时报表就会运行.
是否包含默认报表.
报表存放位置.
报表包含的插件以及配置.
包含一组<reportSet>,与<execution>相似,配置多个目标,每一个<reportSet>包含<id>,<configuration>,<inherited>,以及<reports>,<id>指定报表集合的标识符,<configuration>表示使用的报表配置,<inherited>表示是否继承到子pom,<reports>包含一组<report>,表示使用哪些报表.
<licenses>,包含一组<license>,每一个<license>包含<name>,<url>,<distribution>,<comments>.
名称.
官方license页面的url.
项目分发的方式,能够选择
一些补充信息.
<organazation>,包含<name>,<url>,与<license>的相似.
<developers>,包含一组<developer>,每一个<developer>包含:
开发者id.
姓名.
邮箱.
主页url.
所属组织.
所属组织的主页url.
角色,包含一组<role>,一个<role>描述一个角色.
时区,能够以America/New_York或Europe/Berlin这样的形式,或者设置一个整数,范围[-11,12].
开发者属性,如如何处理即时消息等.
<contributors>,包含一组<contributor>,相似于<developer>,包含<name>,<email>等元素.
<issueManagement>,定义缺陷跟踪系统,如Bugzilla,TestTrack,ClearQuest等,包含<system>与<url>元素,<system>指定系统名字,<url>指定问题管理系统的url.
<ciManagement>,使用了触发器,包含了:
持续集成系统的名称.
持续集成系统的url.
包含一组<notifier>,用来配置触发器,每一个<notifier>包含:
如何发送通知,好比能够取值mail.
取值true/false,错误时发送.
取值true/false,失败时发送.
取值true/false,成功时发送.
取值true/false,发生警告时发送.
相关配置,例如能够添加<address>,发送的地址.
<mailingLists>,包含一组<mailingList>,表示邮件信息,包括:
邮件名称.
订阅邮件地址或连接.
取消订阅邮件或连接.
要发送的邮件地址.
查看旧的邮件的url.
<scm>,也叫Source Code/Control Management,容许配置代码库供web站点和其余插件使用.包含:
描述如何经过Maven链接到版本控制系统,其中connection须要读权限,developConnection须要写权限.
代码标签,默认为HEAD.
公开的可浏览的仓库,例如ViewVC或Fisheye.
<prerequisites>,这是Maven2中的元素,只有一个子元素<maven>,指定maven的版本,且规定是2.x版本.Maven3中不须要<prerequisites>了,能够用:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>3.0.0-M3</version> <executions> <execution> <id>enforce-maven</id> <goals> <goal>enforce</goal> </goals> <configuration> <rules> <requireMavenVersion> <version>3.0</version> </requireMavenVersion> </rules> </configuration> </execution> </executions> </plugin> </plugins> </build>
代替.
<repositories>,包含一组<repository>,表示仓库的位置,每一个<repository>包含:
如何处理远征仓库的发布版本,包含:
如何处理远程仓库的快照版本,包含的元素与<releases>同样.
远程仓库的标识符.
远程仓库的名称.
远程仓库的url.
仓库布局类型,能够是default或legacy,Maven2.x为仓库提供了默认布局.
<pluginRepositories>,插件的远程仓库列表,包含一组<pluginRepository>,与<repositories>中的<repository>相似.
<distributeManagement>,管理整个构建过程当中的分发,能够把网站部署到远程服务器或者把构件部署到远程仓库.包含:
<repository>,仓库信息,包含:
还有一个叫<snapshotRepository>的元素,与<repository>相似,表示快照仓库.
<site>,定义了如何部署项目的站点与文档.包含:
<relocation>,表示项目的新位置.包含:
<profiles>,包含一组<profile>,每一个<profile>能够定义不一样的配置,包含的元素有:
<activation>包含如下元素:
是否默认激活,true或false.
指定jdk版本.
<os>能够定义一些特定的操做系统属性,例如<name>,<family>,<arch>,<version>.
若Maven检测到该属性就会激活该属性所在的配置文件,能够指定<name>与<value>.
有<exists>与<missing>两个子元素,<exists>表示若存在<exists>元素中对应的文件,则激活此配置文件.<miissing>表示若不存在<missing>元素中对应的文件,则激活此配置文件.