Maven中dependencyManagement管理项目依赖项
目录
- 介绍
- dependencyManagement与dependencies的区别
- 总结
介绍
在开发 Java 项目时,管理和协调依赖项的版本号是一项重要而繁琐的任务。
而 Maven 提供了 <dependencyManagement> 元素,用于定义项目中所有依赖项的版本。它允许您指定项目中每个依赖项的版本号,而无需在每个模块的 <dependencies> 部分中重复指定版本号。
本文将介绍 dependencyManagement 的作用,并演示如何在 Maven 项目中使用它来管理依赖项版本。
使用dependencyManagement可以统一管理项目中依赖包的版本号,当需要变更版本号时只需在父pom中修改即可;如果某个子项目需要指定一个特殊的版本号时,只需要在自己项目的pom.XML中显示声明一个版本号即可,此时子项目会使用自己声明的版本号,而不继承父项目的版本号
下面是一个简单的示例,说明了如何在 Maven 项目中使用它
首先在项目的父 pom.xml 文件中添加如下配置:
<project>
...
<properties>
<swagger.version>3.0.0</swagger.version>
</properties>
<dependencyManagement>
<dependencies>
<!--swagger3-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
...
</project>
然后,在其他 Maven 子模块的 pom.xml 文件中,只需声明依赖项的 groupId 和 artifactId,而无需重复指定版本号:
<dependencies> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> </dependency> </dependencies>
子模块将从父级项目中继承定义的版本号,确保项目中所有使用的依赖项版本一致。
dependencyManagement与dependencies的区别
dependencies相对于dependencyManagement,所有声明在dependencies里的依赖都会自动引入,并默认被所有的子项目继承
dependencyManagement里只是声明依赖,并不会自动引入,因此子项目需要显示声明依赖。在子项目中声明了依赖项,且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,则会使用子项目中指定的版本⚠️注意:一个无子工程的独立工程中如果使用dependencyManagement,那么它自己的pom.xml文件引入的依赖也可以不指定版本<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.ume.qa</groupId>
<artifactId>SpringCloudGateway</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>3.0.1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</acuEBQksZrtifactId>
<version>3.0.3</version>
</dependency>
<!-- SpringCloud alibaba nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-cloud-context</artifactId>
<groupId>org.springframework.cloud</groupId>
</exclusion>
<exclusion>
<artifactId>spring-cloud-commons</artifactId>
<groupId>org.springframework.cloud</groupId>
</exclusion>
<exclusion>
<artifactId>spring-cloud-starter</artifactId>
<groupId>org.springframework.cloud</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!--fegin组件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>3.0.2</version>
<exclusions>
<exclusion>
<artifactId>spring-cloud-commons</artifactId>
<groupId>org.springframework.cloud</groupId>
</exclusion>
<exclusion>
<artifactId>spring-cloud-starter</artifactId>
<groupId>org.springframework.cloud</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- Feign Client for loadBalancing -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
<version>3.0.2</version>
<exclusions>
<exclusion>
<artifactId>spring-cloud-commons</artifactId>
<groupId>org.springframework.cloud</groupId>
</exclusion>
<exclusion>
<artifactId>spring-cloud-context</artifactId>
<groupId>org.springframework.cloud</groupId>
编程客栈 </exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId&jsgt;spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>i编程mport</scope>
编程</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
总结
使用 dependencyManagement 元素可以简化依赖项版本的管理,减少重复和错误的声明。通过将版本号集中在一个地方进行管理,确保项目中所有使用的依赖项的版本保持一致性。这不仅提高了开发效率,还能减少因依赖项版本不一致导致的问题。因此,在开发 Java 项目时,我们应该充分利用它来更好地管理项目的依赖项版本
到此这篇关于Maven中dependencyManagement管理项目依赖项的文章就介绍到这了,更多相关Maven dependencyManagement管理内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
加载中,请稍侯......
精彩评论