Maven 主模塊和子模塊pom.xml依賴聲明
前言
今天想到了一個問題,如果一個依賴只有子模塊用到了,是放入子模塊的 pom.xml 呢,還是放入父模塊的 pom.xml 呢?
理論上當(dāng)然是子模塊單獨聲明更符合邏輯。但是以上問題的場景來源有兩個:
- 為了方便,或者考慮到其它子模塊或許以后會用到此依賴的可能性。
- 單模塊項目改造為多模塊后,原本的依賴全部聲明在父模塊 pom.xml 中,考慮是否要大量遷移到用到的子模塊中。
進而引申出的問題:
如果依賴全部放入父模塊,部分子模塊沒有用到這些依賴,是否會增加這些子模塊打包后的代碼體積?
背景知識
dependencies與dependencyManagement的區(qū)別
- 父項目中的 <dependencies></dependencies> 中定義的所有依賴,在子項目中都會直接繼承。
- 在父項目中的 <dependencyManagement></dependencyManagement> 中定義的所有依賴,子項目并不會繼承,我們還要在子項目中引入我們需要的依賴,才能進行使用。此時我們在子項目中不用設(shè)置版本。
實驗
為了回答這個問題:“如果依賴全部放入父模塊,部分子模塊沒有用到這些依賴,是否會增加這些子模塊打包后的代碼體積?”。我們拿一個 maven 多模塊項目打包測試一下。
實驗材料:

如圖,一個多模塊項目。
其中 wx-common 模塊只是放了一些 enums:

父模塊依賴:
<properties>
<java.version>11</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
<wx-common-version>0.0.1-SNAPSHOT</wx-common-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.2.6.RELEASE</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
</dependency>
<dependency>
<groupId>com.jellyfishmix.interchange</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<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.jellyfishmix.interchange</groupId>
<artifactId>wx-common</artifactId>
<version>${wx-common-version}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</dependencyManagement>
wx-common 模塊無單獨引入的依賴。
wx-common 模塊單獨打包后的大?。?982 bytes):

接下來我們把父模塊的依賴都放入 <dependencyManagement></dependencyManagement> 中,這樣子模塊就不會全部繼承這些依賴,而是需要在子模塊的 pom.xml 中也進行聲明,子模塊才能繼承對應(yīng)的依賴。
按照博主的猜想, 子模塊最初繼承了很多父模塊的依賴,當(dāng)單獨打包子模塊時,這些依賴被打入了子模塊jar包中。而這些繼承過來的父模塊的依賴中,有很多是子模塊不需要的,因此子模塊單獨打出的包,會有不少冗余體積 。
我們把父模塊的依賴都挪入 <dependencyManagement></dependencyManagement> 中,而 子模塊又沒有在自己的 pom.xml 中聲明這些依賴,也就不會繼承這些依賴,這樣子模塊單獨打出的包,會不會減少很多體積呢 ?
按我們的推測,把父模塊的依賴都放入 <dependencyManagement></dependencyManagement> 中,然后對子模塊單獨打包(3982 bytes):

可以看到打包出來的 jar,并沒有按照我們預(yù)先設(shè)想的,體積減少了很多,而是和之前的體積一模一樣(都是3982 bytes)。
看到這個結(jié)果,博主百思不得其解。難道 子模塊繼承的父模塊的依賴,如果在子模塊中沒有被使用,在子模塊單獨打包時,就不會被打入 jar 嗎?
我們再做一個實驗來驗證猜想,現(xiàn)在父模塊的依賴還是在 <dependencyManagement></dependencyManagement> 中,需要在子模塊的 pom.xml 中也進行聲明,子模塊才能繼承對應(yīng)的依賴。我們給子模塊的 pom.xml 多聲明幾個依賴:
<!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.2.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.json/json --> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20190722</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.2.2.RELEASE</version> </dependency>
然后對子模塊單獨打包(4175 bytes):

可以看到我們的 jar 包體積確實增加了(4175 - 3982 = 193 bytes),但這些增加的代碼體積,應(yīng)該是我們的 pom.xml 中新增的一對對 <dependency></dependency> 的體積,而不是真正引入的依賴的代碼。
因此,博主確信了推測: 子模塊繼承的父模塊的依賴/子模塊聲明的依賴,如果在子模塊中沒有被使用,在子模塊單獨打包時,就不會被打入 jar 。
進一步實驗來確認(rèn)推測,我們在子模塊中使用一下聲明的依賴。只在子模塊中加兩個注解: @FeignClient(name = "interchange-wx") ,對子模塊單獨打包(4259 bytes):

打包結(jié)果(4259 - 4175 = 84 bytes)。
因此,maven 打包加入的依賴代碼應(yīng)該是被調(diào)用到的部分代碼,沒有被調(diào)用到的依賴代碼不會被加入打包后的 jar 包中。
實驗結(jié)論
- 子模塊繼承的父模塊的依賴/子模塊聲明的依賴,如果在子模塊中沒有被使用,在子模塊單獨打包時,就不會被打入 jar 。
- maven 打包加入的依賴代碼是被調(diào)用到的部分代碼,沒有被調(diào)用到的依賴代碼不會被加入打包后的 jar 包中。
推薦做法
對于 “依賴放入子模塊還是父模塊” 這個問題,推薦將依賴放入父模塊的 <dependencyManagement></dependencyManagement> 中,然后子模塊有需要的依賴,在子模塊的 pom.xml 中聲明。這樣便于在父模塊中統(tǒng)一管理依賴版本,避免子模塊依賴版本不一致造成的混亂或沖突。
到此這篇關(guān)于Maven 主模塊和子模塊pom.xml依賴聲明的文章就介紹到這了,更多相關(guān)Maven pom.xml依賴聲明內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
配置hadoop環(huán)境mapreduce連接不上hdfs解決
這篇文章主要為大家介紹了配置hadoop環(huán)境mapreduce連接不上hdfs解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
JNI實現(xiàn)最簡單的JAVA調(diào)用C/C++代碼
這篇文章主要介紹了JNI實現(xiàn)最簡單的JAVA調(diào)用C/C++代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
SpringBoot項目速度提升之延遲初始化(Lazy Initialization)詳解
延遲初始化(Lazy?Initialization)是一種在需要時才創(chuàng)建或加載對象的策略,以減少啟動時間和資源消耗,本文就來講講延遲初始化的具體使用吧2023-05-05
詳解Java的Hibernate框架中的List映射表與Bag映射
這篇文章主要介紹了Java的Hibernate框架中的List映射表與Bag映射,Hibernate是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2015-12-12
springboot 在idea中實現(xiàn)熱部署的方法
這篇文章主要介紹了springboot 在idea中實現(xiàn)熱部署的方法,實現(xiàn)了熱部署,在每一次作了修改之后,都會自動的重啟,非常節(jié)約時間,感興趣的小伙伴們可以參考一下2018-10-10

