为了账号安全,请及时绑定邮箱和手机立即绑定

pom.xml详解

标签:
Java

1.pom.xml是什么

pom是Project Object Model(项目对象模型)的缩写,是Maven中的项目文件,可用于管理与配置依赖,组织信息,项目授权,远程仓库等等.一个Maven项目,可以没有任何代码,但不能没有pom.xml.

2.基本配置

(1)<project>

<project>是pom.xml的根元素,包含了一些约束信息.

<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>

(2)<modelVersion>

<modelVersion>4.0.0</modelVersion>

pom的版本,这是Maven 2&3唯一支持的pom版本,而且不能忽略.

(3)Maven坐标

<groupId>,<artifactId>与<version>标识了仓库中的一个特定位置,叫项目坐标.三个属性告诉了Maven项目中的一个特定版本,让Maven知道如何处理它们以及在生命周期内的哪一阶段需要它们.

A.<groupId>

<groupId>表示项目所属的组,通常是一个公司或者组织的名称,如org.springframework.

B.<artifactId>

<artifactId>表示项目的唯一标识.

C.<version>

<version>表示项目的版本号,通常来说项目的版本号分成三段:
主版本号.次版本号.修订版本号

  • 主版本号:代表架构变动或者不兼容的实现.
  • 次版本号:兼容性修改,功能增强.
  • 修订版本号:bug修复.

版本号的后缀意味着项目的不同阶段:

  • SNAPSHOT:开发中的版本
  • RELEASE:正式发布版
  • M1,M2:M指里程碑,表示即将发布
  • RC:Release Candidate,发布候选
  • GA:General Availablity,基本可用版本

(4)<packaging>

打包类型,没有提供的话默认值为jar,常见的有jar与war,也可以取值:

  • maven-plugin
  • pom
  • ejb
  • ear
  • rar

(5)POM 关系

Maven的一个强大之处是处理项目关系的方式,可以通过一个公共的本地仓库去解决问题.

A.依赖

POM的基础就是依赖列表,Maven下载与在编译时链接依赖与其他所需要的目标,而且可以处理传递性依赖,使列表可以专注于项目所需的依赖.依赖放在<dependencies>里面,包含若干个<dependency>.

<dependencies>
	<dependency>
		....
	</dependency>
	<dependency>
		....
	</dependency>
</dependencies>

一个<dependency>通常包含:

a.<groupId>与<artifactId>

对应项目坐标

b.<version>

版本

c.<classifier>

可用于配置不同jdk的<depenency>,比如让一个<dependency>同时支持jdk8与jdk11,可以选择使用哪一个<classifier>,方便在不同jdk中使用.

d.<type>

对应的依赖类型,默认为jar,通常对应与<packaging>.

e.<scope>

scope表示类库与项目的关系,可以取以下5个值:

  • compile:默认值,编译依赖使其在所有类路径中可用,而且这些依赖项会传递到其他依赖项目,在编译和打包时都需要此类库.
  • provided:类似compile,但是期望JDK或一个容器会在运行时提供,仅在编译和测试类路径上可用,不可传递.
  • runtime:在运行时与测试类路径中可用,在编译类路径中不可用.
  • test:测试编译与执行阶段可用,不可传递.
  • system:类似于provided,但必须显式提供jar包.

f.<systemPath>

当<scope>为system才需要这个,否则(当<scope>不为system时)会构建失败.路径必须为绝对路径.

g.<optional>

标记依赖的可选状态.

h.<exclusions>

排除不需要的依赖,包含子元素<exclusion>,每个<exclusion>都包含<groupId>与<artifactId>.

B.继承

使用<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>等众多属性,具体包括:

  • groupId
  • version
  • description
  • url
  • inceptionYear
  • organization
  • licenses
  • developers
  • contributors
  • mailingLists
  • scm
  • issueManagement
  • ciManagement
  • properties
  • dependencyManagement
  • dependencies
  • repositories
  • pluginRepositories
  • build
  • reporting
  • profiles

但不能继承:

  • artifactId
  • name
  • prerequisites

另外,就像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>

C.聚合(或多模块)

模块是pom列出的项目,并作为一个组执行,每个模块通过pom文件或项目的相对路径进行列出.

<modules>
    <module>my-project</module>
    <module>another-project</module>
    <module>third-project/pom-example.xml</module>
</modules>

不需要考虑模块间的依赖关系,Maven会对其进行拓扑排序以便在依赖模块之前构建依赖关系.

(6)属性

属性是Maven中的值占位符,像Ant一样,可以以

${x}

这样的形式在pom.xml的任何位置访问一个值,也可以被用作默认值使用.
有5种形式使用值:

①env.x

env会使用当前shell的环境变量的值.
例如

${env.PATH}

②project.x

<project>下的x元素的值,如

${project.version}

③settings.x

使用settings.xml中的元素的值

${settings.offline}

④java.x

java系统属性值,通过java.lang.System.getProperties()获取,如

${java.home}

⑤x

直接使用x,用的是<properties>下的属性,比如

<properties>
	<aaa>AAAAA</aaa>
</properties>
${aaa}

3.构建配置

<build>,声明项目结构,管理插件等.

(1)BaseBuild元素

A.<defaultGoal>

目标的默认值,可以取值install,copile

B.<directory>

构建产生的文件存放目录

C.<finalName>

构建最终产生的项目名字,但有可能会被更改.

D.<filters>

定义一组<filter>,<filter>内是.properties文件,项目中的占位符如xxx.xxx会被.properties中的xxx=xxx的具体值替换掉.

(2)资源

<resources>,项目相关的资源文件的位置.

A.<resource>

描述每个资源的根元素.

B.<targetPath>

构建资源的位置,对于jar包放在META-INF里面.

C.<flitering>

取值true或false,表示是否开启过滤

D.<directory>

资源位置.

E.<include>

指定要包含的资源,使用*作为通配符.

F.<excludes>

与include相反,要排除的资源列表.

(3)插件

<plugins>下包含了若干个<plugin>,表示插件,每个<plugin>有以下元素:

A.<groupId>与<artifactId>

与上面的<groupId>与<artifactId>一样.

B.<version>

与上面的<version>一样.

C.<extensions>

取值true或false,表示是否加载扩展,默认为false.

D.<inherited>

取值ture或false,是否应用pom的继承关系,默认true.

E.<configuration>

插件项的相关配置,可以配置<finalName>,<appendAssemblyld>,<descriptor>等.

F.<dependencies>

引入插件的依赖,与前面的<dependencies>类似.

G.<executions>

插件可能有多个目标,<executions>配置每一个<execution>作为插件的目标,在<execution>中,用<id>指定执行目标的标识符,用<goals>指定目标,<goals>包含一组<goal>,<phase>用于指定阶段,<inherited>用于指定是否启用继承关系.另外<execution>也可以包含<configuration>,与上面类似,用于配置特定的目标,而不是插件的所有目标.

(4)插件管理

<pluginManagement>,包含一组<plugins>,继承于此项目的子项目都可以使用,子项目可以覆盖修改<pluginManagement>.

(5)目录

可以为pom设置各种目录,比如

A.项目源码目录

<sourceDirectory></sourceDirectory>

构建项目时会编译该目录的源码,是相对于pom.xml的相对路径.

B.测试源码目录

<testSourceDirectory></testSourceDirectory>

测试时会编译其中的源码,也是相对于pom.xml的相对路径.

C.class目录

<outputDirectory></outputDirectory>

这里存放被编译过的class文件.

D.测试class

<testOutputDirectory></testOutputDirectory>

存放测试文件编译后的class文件.

(6)扩展

<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>

(7)报表

<reporting>,描述产生报表的规范等,执行"mvn site"时报表就会运行.

A.<excludeDefaults>

是否包含默认报表.

B.<outputDirectory>

报表存放位置.

C.<plugins>

报表包含的插件以及配置.

D.<reportSets>

包含一组<reportSet>,与<execution>类似,配置多个目标,每个<reportSet>包含<id>,<configuration>,<inherited>,以及<reports>,<id>指定报表集合的标识符,<configuration>表示使用的报表配置,<inherited>表示是否继承到子pom,<reports>包含一组<report>,表示使用哪些报表.

4.项目信息

(1)许可证

<licenses>,包含一组<license>,每个<license>包含<name>,<url>,<distribution>,<comments>.

A.<name>

名称.

B.<url>

官方license页面的url.

C.<distribution>

项目分发的方式,可以选择

  • repo:从Maven仓库下载.
  • manual:手动安装.

D.<comments>

一些补充信息.

(2)组织

<organazation>,包含<name>,<url>,与<license>的类似.

(3)开发者

<developers>,包含一组<developer>,每个<developer>包含:

A.<id>

开发者id.

B.<name>

姓名.

C.<email>

邮箱.

D.<url>

主页url.

E.<organization>

所属组织.

F.<organizationUrl>

所属组织的主页url.

G.<roles>

角色,包含一组<role>,一个<role>描述一个角色.

H.<timeZone>

时区,可以以America/New_York或Europe/Berlin这样的形式,或者设置一个整数,范围[-11,12].

I.<properties>

开发者属性,如如何处理即时消息等.

(4)贡献者

<contributors>,包含一组<contributor>,类似于<developer>,包含<name>,<email>等元素.

5.环境配置

(1)问题管理

<issueManagement>,定义缺陷跟踪系统,如Bugzilla,TestTrack,ClearQuest等,包含<system>与<url>元素,<system>指定系统名字,<url>指定问题管理系统的url.

(2)持续集成管理

<ciManagement>,使用了触发器,包含了:

A.<system>

持续集成系统的名称.

B.<url>

持续集成系统的url.

C.<notifiers>

包含一组<notifier>,用来配置触发器,每个<notifier>包含:

a.<type>

如何发送通知,比如可以取值mail.

b.<sendOnError>

取值true/false,错误时发送.

c.<sendOnFailure>

取值true/false,失败时发送.

d.<sendOnSuccess>

取值true/false,成功时发送.

e.<sendOnWarning>

取值true/false,发生警告时发送.

f.<configuration>

相关配置,例如可以添加<address>,发送的地址.

(3)邮件列表

<mailingLists>,包含一组<mailingList>,表示邮件信息,包括:

A.<name>

邮件名称.

B.<subscribe>

订阅邮件地址或链接.

C.<unsubscribe>

取消订阅邮件或链接.

D.<post>

要发送的邮件地址.

E.<archive>

查看旧的邮件的url.

(4)软件配置管理(SCM)

<scm>,也叫Source Code/Control Management,允许配置代码库供web站点和其他插件使用.包含:

A.<connection>与<developConnection>

描述如何通过Maven连接到版本控制系统,其中connection需要读权限,developConnection需要写权限.

B.<tag>

代码标签,默认为HEAD.

C.<url>

公开的可浏览的仓库,例如ViewVC或Fisheye.

(5)前提条件

<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>

代替.

(6)仓库

<repositories>,包含一组<repository>,表示仓库的位置,每个<repository>包含:

A.<releases>

如何处理远征仓库的发布版本,包含:

  • <enabled>
    true/false,是否启用.
  • <updatePolicy>
    更新频率,Maven将本地pom的时间戳与远程进行比较,可以选择:always,daily(默认),interval:X(X是一个整数,表示X分钟),never.
  • <checksumPolicy>
    校验错误或校验失败时的策略,可以选择ignore,fail或warn.

B.<snapshots>

如何处理远程仓库的快照版本,包含的元素与<releases>一样.

C.<id>

远程仓库的标识符.

D.<name>

远程仓库的名称.

E.<url>

远程仓库的url.

F.<layout>

仓库布局类型,可以是default或legacy,Maven2.x为仓库提供了默认布局.

(7)插件仓库

<pluginRepositories>,插件的远程仓库列表,包含一组<pluginRepository>,与<repositories>中的<repository>类似.

(8)分发管理

<distributeManagement>,管理整个构建过程中的分发,可以把网站部署到远程服务器或者把构件部署到远程仓库.包含:

A.仓库

<repository>,仓库信息,包含:

  • <uniqueVersion>:唯一版本,取值true/false,表示是否生成一个唯一版本号或是使用部分地址作为版本号.
  • <name>:仓库名字.
  • <id>:仓库id.
  • <url>:指定仓库位置.
  • <layout>:布局,取值default或legacy.

还有一个叫<snapshotRepository>的元素,与<repository>类似,表示快照仓库.

B.站点部署

<site>,定义了如何部署项目的站点与文档.包含:

  • <name>:站点名称.
  • <id>:站点id.
  • <url>:站点url.

C.重定位

<relocation>,表示项目的新位置.包含:

  • <groupId>:新的<groupId>.
  • <artifactId>:新的<artifactId>.
  • <version>:新版本.
  • <message>:提示信息.

(9)配置文件

<profiles>,包含一组<profile>,每个<profile>可以定义不同的配置,包含的元素有:

  • <id>:配置文件的id,比如测试的可以叫test.
  • <build>:相关构建信息.
  • <modules>:模块信息.
  • <repositories>:远程仓库信息.
  • <pluginRepositories>:插件仓库信息.
  • <dependencies>:依赖信息.
  • <reporting>:报表信息.
  • <dependencyManagement>:依赖管理信息.
  • <distributeManagement>:分发管理
  • <activation>:activation是profile的关键,profile的强大之处是某些情况下才可以修改基本pom,这些情况通过activation指定.

<activation>包含以下元素:

A.<activeByDefault>

是否默认激活,true或false.

B.<jdk>

指定jdk版本.

C.<os>

<os>可以定义一些特定的操作系统属性,例如<name>,<family>,<arch>,<version>.

D.<property>

若Maven检测到该属性就会激活该属性所在的配置文件,可以指定<name>与<value>.

E.<file>

有<exists>与<missing>两个子元素,<exists>表示若存在<exists>元素中对应的文件,则激活此配置文件.<miissing>表示若不存在<missing>元素中对应的文件,则激活此配置文件.

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
5
获赞与收藏
30

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消