当前位置:网站首页>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>
边栏推荐
- Mysql, sqlserver Oracle database connection mode
- R language ggplot2 visualization: use the ggqqplot function of ggpubr package to visualize the QQ graph (Quantitative quantitative plot)
- LeetCode_7_5
- 怎么在手机上买股票开户 股票开户安全吗
- Kirin Xin'an cloud platform is newly upgraded!
- How to buy bank financial products? Do you need a bank card?
- Solve the error reporting problem of rosdep
- MySQL、sqlserver oracle数据库连接方式
- Unable to link the remote redis server (solution 100%
- Classification automatique des cellules de modules photovoltaïques par défaut dans les images de lecture électronique - notes de lecture de thèse
猜你喜欢

Introduction to bit operation

Kunpeng developer summit 2022 | Kirin Xin'an and Kunpeng jointly build a new ecosystem of computing industry

项目经理『面试八问』,看了等于会了

Classification automatique des cellules de modules photovoltaïques par défaut dans les images de lecture électronique - notes de lecture de thèse

Compiler optimization (4): inductive variables

PMP对工作有益吗?怎么选择靠谱平台让备考更省心省力!!!

微信公众号OAuth2.0授权登录并显示用户信息

【RT-Thread env 工具安装】

Ways to improve the utilization of openeuler resources 01: Introduction

el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
随机推荐
Empowering smart power construction | Kirin Xin'an high availability cluster management system to ensure the continuity of users' key businesses
Semantic SLAM源码解析
多个线程之间如何协同
Matplotlib drawing 3D graphics
What does "true" mean
Jerry's headphones with the same channel are not allowed to pair [article]
2022年投资哪个理财产品收益高?
Redis——基本使用(key、String、List、Set 、Zset 、Hash、Geo、Bitmap、Hyperloglog、事务 )
我的创作纪念日
Ucloud is a basic cloud computing service provider
指定opencv非标准安装的版本
Implement secondary index with Gaussian redis
R语言ggplot2可视化:使用ggpubr包的ggdensity函数可视化分组密度图、使用stat_overlay_normal_density函数为每个分组的密度图叠加正太分布曲线
Specify the version of OpenCV non-standard installation
ant desgin 多选
The project manager's "eight interview questions" is equal to a meeting
Tp6 realize Commission ranking
MySQL、sqlserver oracle数据库连接方式
模拟实现string类
Navicat连接2002 - Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘解决