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> & ...
随机推荐
- GOF23设计模式汇总
转自:http://www.cnblogs.com/zhili/p/DesignPatternSummery.html#3037698 C#设计模式总结 一.引言 经过这段时间对设计模式的学习,自己的 ...
- 关于Cocos2d-x运行项目时弹框崩溃的解决
想要运行工程的时候,跳出一个框说停止cantnot open the window,还提到什么GLVM之类的,这是显卡驱动出现问题,如果是远程连接电脑的话,很有可能就是用来远程连接的那台电脑的显卡驱动 ...
- c# winform 自动关闭messagebox 模拟回车
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- mysql 中查看指定表的字段名 (可根据字段变量生成c#后台代码)
select DISTINCT data_type from COLUMNS where table_name='表名' 用ConCat();构造生成代码.....
- 【Java面试题】29 设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。
本题并不难,实现方式有很多种,有很多种结构. 方法一:利用内部类实现,两个实现加减的类实现Runnable接口,然后再实现4个具体线程. 代码: public class ManyThreads { ...
- [转]查看处于被锁状态的表:v$locked_object dba_objects v$session all_objects v$sqlarea v$lock
oracle官网当一个用户发出select..for update的错作准备对返回的结果集进行修改时,如果结果集已经被另一个会话锁定,就是发生阻塞.需要等另一个会话结束之后才可继续执行.可以通过发出 ...
- 调整swap分区大小-Linux下安装Oracle时报swap不够解决方法
调整swap分区大小 方法一:如果磁盘有剩余的空间,用分区工具新建一个swap分区.并写到/etc/fstab里面.再 #swapon -a方法二:可以用一个文件做交换分区. su root cd / ...
- OpenCV学习:播放avi视频文件
#if 0 //播放avi视频文件(IplImage) #include <opencv2/opencv.hpp> using namespace std; #pragma comment ...
- 记录下Lambda常用的表现形式
纯粹记录下Lambda的表现形式: (x, y) => x * y;//多参数,隐式类型=>表达式 x => x * ;//单参数,隐式类型=>表达式 x => { ; ...
- oracle客户端免安装配置、64位机器PL/SQL和VS自带的IIS连接问题
一.oracle客户端免安装配置 1.到oracle官网下载Oracle InstantClient, 把它解压缩到单独目录,例如C:\OracleClient,2. 添加环境变量 ORACLE_HO ...