什么是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. oracle权限详解

    一.权限分类:系统权限:系统规定用户使用数据库的权限.(系统权限是对用户而言). 实体权限:某种权限用户对其它用户的表或视图的存取权限.(是针对表或视图而言的). 二.系统权限管理:1.系统权限分类: ...

  2. 获得android手机的联网状态

    获得android手机的联网状态   在Android平台上开发基于网络的应用,必然需要去判断当前的网络连接情况.下面的代码,作为例子,详细说明了对于当前网络情况的判断. 先看一个自己定义的应用类. ...

  3. 第三百零八节,Django框架,models.py模块,数据库操作——链表结构,一对多、一对一、多对多

    第三百零八节,Django框架,models.py模块,数据库操作——链表结构,一对多.一对一.多对多 链表操作 链表,就是一张表的外键字段,连接另外一张表的主键字段 一对多 models.Forei ...

  4. e1087. 用For循环做数组的遍历

    The for statement can be used to conveninently iterate over the elements of an array. The general sy ...

  5. (转)x264代码详细阅读之x264.c,common.c,encoder.c

    转自:http://alphamailpost.blog.163.com/blog/static/201118081201281103931932/ x264代码详细阅读第一之x264.chttp:/ ...

  6. 第四章 事务(MYBatis)

    一个使用 MyBatis-Spring 的主要原因是它允许 MyBatis 参与到 Spring 的事务管理中.而不是给 MyBatis 创建一个新的特定的事务管理器,MyBatis-Spring 利 ...

  7. Xshell通过ssh方式连接Linux服务器,通过Xftp进行文件传输

    准备工作: 一.Linux服务器一台,并配置ssh(本文以腾讯云服务器为例). 1.生成秘钥 打开腾讯云控制台,依次选择:总览->云服务器->SSH秘钥 点击创建秘钥,输入秘钥名称,点击确 ...

  8. iOS-WKWebView使用

    使用代码:可直接粘贴到自己项目中使用 .h #import "BaseViewController.h" @interface LinkNewsController : BaseV ...

  9. UVA 11542 - Square(高斯消元)

    UVA 11542 - Square 题目链接 题意:给定一些数字.保证这些数字质因子不会超过500,求这些数字中选出几个,乘积为全然平方数,问有几种选法 思路:对每一个数字分解成质因子后.发现假设要 ...

  10. MySQL<添加、更新与删除数据>

    添加.更新与删除数据 添加数据 为表中所有字段添加数据 INSERT INTO 表名(字段名1,字段名2,……) VALUES(值1,值2,……); insert into 表名 values(值1, ...