dependencies:
就算在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(全部继承)
dependencyManagement:
只是声明依赖,并不实现引入,因此子项目需要显示声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。
例子:
父项目pom.xml
-
<properties>
-
<springframework.version>2.5.6</springframework.version>
-
<junit.version>4.7</junit.version>
-
</properties>
-
<dependencyManagement>
-
-
<dependencies>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-core</artifactId>
-
<version>${springframework.version}</version>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-beans</artifactId>
-
<version>${springframework.version}</version>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-context</artifactId>
-
<version>${springframework.version}</version>
-
</dependency>
-
</dependencies>
-
</dependencyManagement>
这里使用dependencyManagement声明的依赖既不会给父项目引入依赖,也不会给他的子模块引入依赖,不过这段配置是会被继承
用到父项目的依赖的子项目pom.xml配置
-
<dependencies>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-core</artifactId>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-beans</artifactId>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-context</artifactId>
-
</dependency>
-
</dependencies>
不需要父项目的依赖的子项目pom.xml配置,只需要引入父项目的依赖即可
-
<dependencies>
-
<dependency>
-
<groupId>dom4j</groupId>
-
<artifactId>dom4j</artifactId>
-
<version>${dom4j.version}</version>
-
</dependency>
-
-
<dependency>
-
<groupId>junit</groupId>
-
<artifactId>junit</artifactId>
-
</dependency>
-
</dependencies>
这里没有声明父项目的依赖,那么该依赖就不会被引入。这正是dependencyManagement的灵活性所在。
import依赖范围
import范围只有在denpendencyManagement元素下才有效果
如果你想要把项目A项目的依赖用于另外一个项目就需要使用import范围将这配置导入
-
<dependencyManagement>
-
-
<dependencies>
-
<dependency>
-
<groupId>com.mvnbook.account</groupId>
-
<artifactId>account-parent</artifactId>
-
<version>1.0-SNAPSHOT</version>
-
<type>pom</type>
-
<scope>import</scope>
-
</dependency>
-
</dependencies>
-
</dependencyManagement>
上述代码中type的值为pom,import范围由于其特殊性,一般都是指向打包类型为pom的模块。如果有多个先忙,他们使用的版本都是一致的,则就可以定义一个使用
dependencyManagement专门管理依赖的POM,然后在各个项目中导入这些依赖管理配置
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2018-12-26 java 字符串和集合互相转换