当前位置:网站首页>pom.xml 配置文件标签:dependencies 和 dependencyManagement 区别
pom.xml 配置文件标签:dependencies 和 dependencyManagement 区别
2022-07-07 17:52:00 【whiteye太白】
dependencyManagement 依赖管理
通常会在一个项目的最顶层的父pom中使用dependencyManagement,
使用pom.xml中的dependencyManagement元素能让所有子项目在引用一个依赖时不用列出版本号。这样子项目中该依赖的版本号将使用父项目pom文件中该依赖的版本。
子项目某依赖没有指定版本号时,maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用这个dependencyManagement元素中指定的版本。
这样的好处就是: 如果有多个子项目都引用同一样依赖,则可以避免在每个使用的子项目里都声明一个版本号,这样当想升级或者切换到另一个版本时,只需要在顶层父容器里更新,而不需要一个一个子项目的修改;另外如果某个子项目需要另一个版本,只需要自己pom中声明版本号即可。
注意:
- dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖
- 如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom。
- 如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。
dependencies 依赖关系
自动引入声明在dependencies里的所有依赖,并默认被所有的子项目继承。如果项目中不写依赖项,则会从父项目
继承(属性全部继承)声明在父项目dependencies里的依赖项。
1. 单一模块情况
<!--只是对版本号进行管理,不会实际引入jar-->
<dependencyManagement>
<dependencies>
<dependency>
<!--jar包身份限定-->
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<!--版本号的声明-->
<version>3.2.7</version>
</dependency>
</dependencies>
</dependencyManagement>
<!--会实际下载声明的依赖jar包-->
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<!--不声明version 标签,则会继承dependencyManagement-->
</dependency>
</dependencies>
2. 多模块情况
父模块 pom.xml
<!--parent-module父模块pom.xml-->
<properties>
<!--统一管理jar包版本。集中在父模块properties标签中定义所有依赖的版本号。-->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.eclipse.persistence.jpa.version>1.2.6</org.eclipse.persistence.jpa.version>
<javaee-api.version>1.8</javaee-api.version>
</properties>
<dependencyManagement>
<!--定义公共依赖的版本号-->
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<version>${org.eclipse.persistence.jpa.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${javaee-api.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
子模块 pom.xml
<!--son-module子模块pom.xml-->
<!--继承父类-->
<parent>
<!--声明父类的身份信息-->
<artifactId>parent-module</artifactId>
<groupId>com.ppd</groupId>
<version>0.0.1-SNAPSHOT</version>
<!--声明父类的pom文件路径-->
<relativePath>../parent-module/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>son-module</artifactId>
<packaging>ejb</packaging>
<!--依赖关系-->
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<!--未声明则继承父类version、scope-->
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<!--声明则不继承父类version-->
<version>1.8<version/>
<!--继承父类scope-->
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<!--未声明则继承父类version-->
<scope>provided</scope>
</dependency>
</dependencies>
边栏推荐
猜你喜欢

Nunjuks template engine

2022如何评估与选择低代码开发平台?

CMD command enters MySQL times service name or command error (fool teaching)

Introduction to bit operation

开源重器!九章云极DataCanvas公司YLearn因果学习开源项目即将发布!

模拟实现string类

9 atomic operation class 18 Rohan enhancement

J ü rgen schmidhub reviews the 25th anniversary of LSTM papers: long short term memory All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversar
让这个 CRMEB 单商户微信商城系统火起来,太好用了!

RESTAPI 版本控制策略【eolink 翻译】
随机推荐
LeetCode 515(C#)
LeetCode 648(C#)
A pot of stew, a collection of common commands of NPM and yarn cnpm
【STL】vector
R语言ggplot2可视化:使用ggpubr包的ggqqplot函数可视化QQ图(Quantile-Quantile plot)
how to prove compiler‘s correctness
开源重器!九章云极DataCanvas公司YLearn因果学习开源项目即将发布!
开源OA开发平台:合同管理使用手册
RESTAPI 版本控制策略【eolink 翻译】
Mysql, sqlserver Oracle database connection mode
银行理财产品怎么买?需要办银行卡吗?
9 atomic operation class 18 Rohan enhancement
多个线程之间如何协同
Netease Yunxin participated in the preparation of the standard "real time audio and video service (RTC) basic capability requirements and evaluation methods" issued by the Chinese Academy of Communica
R language ggplot2 visualization: use the ggdensity function of ggpubr package to visualize the packet density graph, and use stat_ overlay_ normal_ The density function superimposes the positive dist
My creation anniversary
Kirin Xin'an joins Ningxia commercial cipher Association
Throughput
Navicat连接2002 - Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘解决
让这个 CRMEB 单商户微信商城系统火起来,太好用了!