POM(project Object Model) Maven包管理依赖 pom.xml文件
什么是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-plugin,ejb,war,ear,rar,par。
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。例如:
jdk15和jdk14指明目标版本;sources和javadoc指明部署的是源码还是文档。 - type: 与
packging中的type相对应。 - scope: 用于指明
dependency作用范围,有5种值:
1. compile: 默认的scope,表示dependency可以在所有的生命周期中使用。而且,这些dependencies会传递到依赖的项目中。适用于所有阶段,会随着项目一起发布。
2. provided:跟compile相似,但是表明了dependency由JDK或者容器提供,例如Servlet API和一些Java EE APIs。另外该dependency只能作用在编译和测试时,同时没有传递性。
3. runtime:表示dependency不作用在编译时,但会作用在运行和测试时,如JDBC驱动,适用运行和测试阶段。
4. test:表示dependency只在测试时使用,用于编译和运行测试代码。不会随项目发布。
5. system:跟provided相似,但是在系统中要以外部JAR包的形式提供,maven不会在repository查找它。 - systemPath:只在
scope为system时有效,指明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.xsettings.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文件的更多相关文章
- POM (Project Object Model)简介
1 概念介绍 一个项目所有的配置都放置在 POM 文件中:定义项目的类型.名字,管理依赖关系,定制插件的行为等等.比如说,你可以配置 compiler 插件让它使用 java1.5 来编译. < ...
- maven工程中防止mapper.xml文件被漏掉、未加载的方法
maven工程中防止mapper.xml文件被漏掉.未加载的方法 就是在pom.xml文件中添加以下内容 <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉. --&g ...
- pom.xml(Project Object Model) 文件简单介绍
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- 项目jar包管理,使用 .userlibraries 文件增加jar包的可移植性,明确jar包依赖,多项目共用jar包里
当一个普通的项目,在不适用maven 等jar包管理工具的时候,通常我都会直接把jar 包复制lib下,并且在build path 中直接添加额外jar包,或者使用user_libraries包所用的 ...
- pom中定义某jar包的依赖,但并不使用该jar包,那最后部署的应用中会有这个jar包么?
关于这个问题,首先得明确这个jar包的依赖是怎么定义的,我们知道在maven的pom文件中,会有:dependencymanagement和dependency2个部分 一般我们说在depende ...
- Maven手动添加依赖的jar文件到本地Maven仓库
原文出处:https://www.iteblog.com/archives/646.html 寫得很好,轉走僅供學習,望諒解! Apache Maven,是一个软件(特别是Java软件)项目管理及自动 ...
- (10)centos7 包管理、远程传文件
一.RPM red package manager 红帽包管理工具 -q 查询 -a 已安装的所有rpm 1.查询已安装的rpm列表 -qa 查看所有的rpm安装包 rpm -qa | grep py ...
- Maven项目不打包*.hbm.xml文件
<build> <finalName>basic</finalName> <plugins> <plugin> <groupId> ...
- Spring Maven 包的依赖
<properties> <spring.version>4.3.11.RELEASE</spring.version> </properties> & ...
随机推荐
- ADT下载地址(含各版本)
2015/05/07 新增 ADT-23.0.6.zip2015/01/18 新增(未测试,不知下载过程中是否有问题,请网友自行测试,最好能把测试结果告知,谢谢)ADT-23.0.3.zipADT ...
- 为什么要把session存入数据库
比如网易的通行证,一个session能进入很多的网易下的网站
- 第二百八十二节,MySQL数据库-MySQL视图
MySQL数据库-MySQL视图 1.视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. 2.也 ...
- linux -- #!/bin/bash
#!/bin/bash是指此脚本使用/bin/bash来解释执行. 其中,#!是一个特殊的表示符,其后,跟着解释此脚本的shell路径. bash只是shell的一种,还有很多其它shell,如:sh ...
- 【Spark】RDD机制实现模型
RDD渊源 弹性分布式数据集(RDD).它是MapReduce模型一种简单的扩展和延伸.RDD为了实现迭代.交互性和流查询等功能,须要保证RDD具备在并行计算阶段之间能够高效地数据共享的功能特性.RD ...
- mysql 5.7 详细图文安装教程
官网下载太慢,可以上下载频道下载(mysql-installer-community-5.7.13.0.msi): http://download.csdn.net/download/tan3739/ ...
- 【Windows】win10应用商店被删后恢复方法!
以管理员身份运行PowerShell,输入以下命令后回车(可直接复制粘贴): Get-AppxPackage -AllUsers| Foreach {Add-AppxPackage -DisableD ...
- Matlab中imread函数使用报错“不应为MATLAB 表达式”分析
问题描述: 使用imread读取特定路径下的文件时,会提示出错! >> mytest错误: 文件:mytest.m 行:10 列:87不应为 MATLAB 表达式. 出错行: Images ...
- 笔记:写Java程序,并输出
建一个文件名为 demo.java的文件 //写框架文件 public class Demo{ //写入口文件 public static void main(String[] args){ Syst ...
- 第四章 Spring.Net 如何管理您的类___对象的作用域
Spring.Net 中对象的作用域,就是描述对象的部署模式 ,Spring.Net 中对象可以通过两种模式布署: ① singleton(单例模式) ② 非singleton 也叫非单例模式(或者叫 ...