什么是POM

POM全称为“Project Object Model”,意思是工程对象模型。Maven工程使用pom.xml来指定工程配置信息,和其他文本信息。该配置文件以xml为格式,使用xml语法表明信息。

快速预览

一个pom.xml文件主要包括以下元素信息:

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<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>

详解

The Basics

Maven Coordinates(Maven坐标)

groupId:artifactId:version构成了Maven工程的坐标系统。

  • groundId: 组织标识,例如:org.codehaus.mojo,在M2_REPO目录下,将是: org/codehaus/mojo目录。
  • artifactId:项目名称,例如:my-project,在M2_REPO目录下,将是:org/codehaus/mojo/my-project目录。
  • version:版本号,例如:1.0,在M2_REPO目录下,将是:org/codehaus/mojo/my-project/1.0目录。
  • packaging:打包格式,可选值:jar(默认值), maven-pluginejbwarearrarpar

POM Relationships(POM关系)

Maven主要用于处理项目之间的关系,包括依赖关系(和过渡依赖)、继承和聚合(多模块项目)。

Dependencies(依赖)

大多数每个项目都需要依赖其他人构建的项目,Maven则可以对这些依赖进行管理 。在
dependencies标签中指明所需要依赖的功能模块,Maven会自动下载编译和链接其依赖关系。另外,Maven屏蔽了这些依赖带来的更多的依赖项(依赖传递),让开发者只需要关注自己项目需要的依赖关系。

junit4

 <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>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<type>jar</type>
<scope>test</scope>
<optional>true</optional>
</dependency>
...
</dependencies>
...
</project>
  • groupId, artifactId, version: 用于精准定位dependency
  • classifier: 用于区分名字相同但内容不同的POM。例如:jdk15jdk14指明目标版本;sourcesjavadoc指明部署的是源码还是文档。
  • type: 与packging中的type相对应。
  • scope: 用于指明dependency作用范围,有5种值:
    1. compile: 默认的scope,表示dependency可以在所有的生命周期中使用。而且,这些dependencies会传递到依赖的项目中。适用于所有阶段,会随着项目一起发布。
    2. provided:跟compile相似,但是表明了dependencyJDK或者容器提供,例如Servlet API和一些Java EE APIs。另外该dependency 只能作用在编译和测试时,同时没有传递性。
    3. runtime:表示dependency不作用在编译时,但会作用在运行和测试时,如JDBC驱动,适用运行和测试阶段。
    4. test:表示dependency只在测试时使用,用于编译和运行测试代码。不会随项目发布。
    5. system:跟provided相似,但是在系统中要以外部JAR包的形式提供,maven不会在repository查找它。
  • systemPath:只在scopesystem时有效,指明dependency路径。
  • optional:指明该项目没有该dependency,依然可以正确运行。
Dependency Version Requirement Specification(依赖版本规范)
  • 1.0: “软性”要求 1.0 (推荐方式)
  • [1.0]: “硬性要求” 1.0
  • (,1.0]: <= 1.0
  • [1.2,1.3]: 1.2 <= x <= 1.3
  • [1.0,2.0): 1.0 <= x < 2.0
  • [1.5,): x >= 1.5
  • (,1.0],[1.2,): x <= 1.0 or x >= 1.2
  • (,1.1),(1.1,): 排除 1.1
Exclusions(排除)

排除掉dependency的依赖传递中的某个dependency。和optional不同,exclusions不会安装和使用该dependency,并从依赖树上去除它。例如,某个依赖树种某dependency可能会导致错误,则应该排除掉。

Inheritance(继承)

POM对象可以实现继承,子POM对象将从父POM继承各属性。

Aggregation(聚类(多模块))

一个pom打包项目通过聚合多个模块来构建,开发者不需要考虑模块间的依赖关系。

Properties(属性)

Maven中的Properties是占位符,可以在POM中任何一个地方使用符号${X},其中X是该Property。有以下5中形式:

  • env.X:系统变量,区分大小写。例如:${env.PATH}表示系统系统路径变量。
  • project.x:POM中相应的值。例如:<project><version>1.0</version></project>可以通过${project.version}访问。
  • settings.x settings.xml中相应的值。例如,<settings><offline>false</offline></settings>可以通过${settings.offline}访问。
  • Java System Properties:例如:${java.home}
  • x:POM中<properties />标签中的值。例如:<properties><someVar>value</someVar></properies>可以通过${someVar}访问。

Build Settings

Build(构建)

build元素分为两块:”project build”和”profile build”。

build

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

The BaseBuild Element Set

BaseBuild

 <build>
<defaultGoal>install</defaultGoal>
<directory>${basedir}/target</directory>
<finalName>${artifactId}-${version}</finalName>
<filters>
<filter>filters/filter1.properties</filter>
</filters>
...
</build>


defaultGoal

    :默认执行行为。
  • directory:构建目录。
  • finalName:文件名。
  • filter:过滤器。

The Build Element Set

Build:

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

  

定义工程目录。

Reporting(报告)

生成工程报告,如javadoc。

Reporting

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

More Project Information

  • name:工程名。
  • description:工程描述。
  • url:工程URL。
  • inceptionYear:开始时间。

Licenses(许可)

Licenses

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


name, url和comments

    :许可描述。
  • distribution:指明工程如何分布。repo,代表可以从Maven repository下载;manual,代表必须手动安装。

Organization(组织)

大多数项目都是由某组织(公司、私人组织等)管理。这是最基本的信息。

Organization

1
2
3
4
<organization>
<name>Codehaus Mojo</name>
<url>http://mojo.codehaus.org</url>
</organization>

Developers(开发者)

Developers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<developers>
<developer>
<id>jdoe</id>
<name>John Doe</name>
<email>jdoe@example.com<email>
<url>http://www.example.com/jdoe</url>
<organization>ACME</organization>
<organizationUrl>http://www.example.com</organizationUrl>
<roles>
<role>architect</role>
<role>developer</role>
</roles>
<timezone>America/New_York</timezone>
<properties>
<picUrl>http://www.example.com/jdoe/pic</picUrl>
</properties>
</developer>
</developers>
  • id, name, email:开发者身份标识。
  • organization, organizationUrl:个人从属组织信息。
  • roles:开发者在项目中的角色。
  • timezone:时区。
  • properties:个人属性。

Contributors(参与者)

开源项目往往有很多Contributors参与,基本元素节点信息与Developers类似。

Environment Settings

Issue Management(问题管理)

定义了缺陷跟踪系统(Bugzilla、TestTrack ClearQuest等)的使用,主要用于生成项目文档。

Continuous Integration Management(连续集成管理)

用于自动化构建。

Mailing Lists(邮件列表)

项目开发人员联系方式。

SCM(软件配置管理)

SCM(Software Configuration Management,也叫Source Code/Control Management),用于版本控制。

Prerequisites(先决条件)

工程构建的先决条件。

Repositories(仓库)

Repositories

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<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>
  • releases, snapshots:正式版和快照版(开发版)。
  • enabled:是否启用。
  • updatePolicy:更新频率。
  • checksumPolicy:校验政策。
  • layout:Maven仓库布局。

Activation(激活)

Activation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<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>

在某些条件下修改工程配置。

reference

[1] https://sawyersun.github.io/2016/06/14/pom/

POM(project Object Model) Maven包管理依赖 pom.xml文件的更多相关文章

  1. POM (Project Object Model)简介

    1  概念介绍 一个项目所有的配置都放置在 POM 文件中:定义项目的类型.名字,管理依赖关系,定制插件的行为等等.比如说,你可以配置 compiler 插件让它使用 java1.5 来编译. < ...

  2. maven工程中防止mapper.xml文件被漏掉、未加载的方法

    maven工程中防止mapper.xml文件被漏掉.未加载的方法 就是在pom.xml文件中添加以下内容 <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉. --&g ...

  3. pom.xml(Project Object Model) 文件简单介绍

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  4. 项目jar包管理,使用 .userlibraries 文件增加jar包的可移植性,明确jar包依赖,多项目共用jar包里

    当一个普通的项目,在不适用maven 等jar包管理工具的时候,通常我都会直接把jar 包复制lib下,并且在build path 中直接添加额外jar包,或者使用user_libraries包所用的 ...

  5. pom中定义某jar包的依赖,但并不使用该jar包,那最后部署的应用中会有这个jar包么?

    关于这个问题,首先得明确这个jar包的依赖是怎么定义的,我们知道在maven的pom文件中,会有:dependencymanagement和dependency2个部分   一般我们说在depende ...

  6. Maven手动添加依赖的jar文件到本地Maven仓库

    原文出处:https://www.iteblog.com/archives/646.html 寫得很好,轉走僅供學習,望諒解! Apache Maven,是一个软件(特别是Java软件)项目管理及自动 ...

  7. (10)centos7 包管理、远程传文件

    一.RPM red package manager 红帽包管理工具 -q 查询 -a 已安装的所有rpm 1.查询已安装的rpm列表 -qa 查看所有的rpm安装包 rpm -qa | grep py ...

  8. Maven项目不打包*.hbm.xml文件

    <build> <finalName>basic</finalName> <plugins> <plugin> <groupId> ...

  9. Spring Maven 包的依赖

    <properties> <spring.version>4.3.11.RELEASE</spring.version> </properties> & ...

随机推荐

  1. 给原型扩展一下tirm方法

    方便以后,直记录下来 <script type="text/javascript"> //给原型护展tirm方法 String.prototype.trim=funct ...

  2. ZooKeeper源码分析:Quorum请求的整个流程(转)

    Quorum请求是转发给Leader处理,并且需要得一个Follower Quorum确认的请求.这些请求包括: 1)znode的写操作(OpCode.create,OpCode.delete,OpC ...

  3. hibernate、struts、spring mvc的作用

    Hibernate工作原理及为什么要用?原理:1.读取并解析配置文件2.读取并解析映射信息,创建SessionFactory3.打开Sesssion4.创建事务Transation5.持久化操作6.提 ...

  4. 将ORACLE数据库更改为归档模式;写出步骤

    解答:具体步骤如下: 1),以exp方式在线备份数据库到指定位置: 2),观察当前数据库是以服务器参数文件(spfile)方式启动还是以参数文件(pfile)方式启动: SQL> show pa ...

  5. C#中的程序集和命名空间

    C#中的程序集和命名空间 如果说命名空间是类库的逻辑组织形式,那么程序集就是类库的物理组织形式.只有同时指定类型所在的命名空间及实现该类型的程序集,才能完全限定该类型.<精通.NET核心技术-- ...

  6. (转)Linux下/etc/rc.local与/etc/init.d的区别与联系

    Linux下/etc/rc.local与/etc/init.d的区别与联系 2012-10-13 20:14:52|  分类: Linux学习|字号 订阅     1./etc/rc.local 这是 ...

  7. jquery -- checkbox选中无选中状态

    最近在工作中使用jquery操作checkbox,使用下面方法进行全选.反选: var ischecked=allCheckObj.is(':checked'); ischecked?checksOb ...

  8. linux -- Ubuntu 命令技巧合集

    http://www.nenew.net/UbuntuSkills.html#.E6.9F.A5.E7.9C.8B.E8.BD.AF.E4.BB.B6xxx.E5.AE.89.E8.A3.85.E5. ...

  9. DB2多行转一行【XML方式】

    分组然后合并,然后去除XML标签 SELECT replace(replace(replace(xml2clob(xmlagg(xmlelement(name A, [字段]))),'</A&g ...

  10. 【JavaScript】使用setInterval()函数作简单的轮询操作

    轮询(Polling)是一种CPU决策怎样提供周边设备服务的方式,又称"程控输出入"(Programmed I/O). 轮询法的概念是.由CPU定时发出询问.依序询问每个周边设备是 ...