Maven中dependencyManagement管理項目依賴項
介紹
在開發(fā) Java
項目時,管理和協(xié)調(diào)依賴項的版本號是一項重要而繁瑣的任務(wù)。
而 Maven
提供了 <dependencyManagement>
元素,用于定義項目中所有依賴項的版本。它允許您指定項目中每個依賴項的版本號,而無需在每個模塊的 <dependencies>
部分中重復(fù)指定版本號。
本文將介紹 dependencyManagement
的作用,并演示如何在 Maven
項目中使用它來管理依賴項版本。
使用dependencyManagement可以統(tǒng)一管理項目中依賴包的版本號,當(dāng)需要變更版本號時只需在父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
,而無需重復(fù)指定版本號:
<dependencies> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> </dependency> </dependencies>
子模塊將從父級項目中繼承定義的版本號,確保項目中所有使用的依賴項版本一致。
dependencyManagement與dependencies的區(qū)別
dependencies相對于dependencyManagement,所有聲明在dependencies里的依賴都會自動引入,并默認(rèn)被所有的子項目繼承
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</artifactId> <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>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
總結(jié)
使用 dependencyManagement
元素可以簡化依賴項版本的管理,減少重復(fù)和錯誤的聲明。通過將版本號集中在一個地方進(jìn)行管理,確保項目中所有使用的依賴項的版本保持一致性。這不僅提高了開發(fā)效率,還能減少因依賴項版本不一致導(dǎo)致的問題。因此,在開發(fā) Java 項目時,我們應(yīng)該充分利用它來更好地管理項目的依賴項版本
到此這篇關(guān)于Maven中dependencyManagement管理項目依賴項的文章就介紹到這了,更多相關(guān)Maven dependencyManagement管理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中final關(guān)鍵字和final的四種用法實例
final關(guān)鍵字代表最終的、不可改變的,下面這篇文章主要給大家介紹了關(guān)于Java中final關(guān)鍵字和final的四種用法實例,文中通過圖文以及實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02SpringBoot集成Redis—使用RedisRepositories詳解
這篇文章主要介紹了SpringBoot集成Redis—使用RedisRepositories詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Java Spring JdbcTemplate基本使用詳解
JDBC已經(jīng)能夠滿足大部分用戶最基本的需求,但是在使用JDBC時,必須自己來管理數(shù)據(jù)庫資源如:獲取PreparedStatement,設(shè)置SQL語句參數(shù),關(guān)閉連接等步驟2021-10-10springboot3.X版本集成mybatis遇到的問題及解決
在將SpringBoot3.X版本與MyBatis集成時,直接參考基于SpringBoot2.X的配置方法會導(dǎo)致各種報錯,尤其是無法注入mapper的bean問題,這主要是因為SpringBoot3.X版本需要搭配MyBatis3.0.3及以上版本才能正常工作,通過更新maven配置至MyBatis3.0.3版本,可以解決這一問題2024-09-09Java SpringMVC數(shù)據(jù)響應(yīng)超詳細(xì)講解
Spring?MVC?是?Spring?提供的一個基于?MVC?設(shè)計模式的輕量級?Web?開發(fā)框架,本質(zhì)上相當(dāng)于?Servlet,Spring?MVC?角色劃分清晰,分工明細(xì),本章來講解SpringMVC數(shù)據(jù)響應(yīng)2022-04-04Classloader隔離技術(shù)在業(yè)務(wù)監(jiān)控中的應(yīng)用詳解
這篇文章主要為大家介紹了Classloader隔離技術(shù)在業(yè)務(wù)監(jiān)控中的應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08SpringMVC 接收前端傳遞的參數(shù)四種方式小結(jié)
這篇文章主要介紹了SpringMVC 接收前端傳遞的參數(shù)四種方式小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10