簡體   English   中英

多模塊 Maven/Spring 啟動項目使用<dependencies>對比<dependencymanagement>標簽?</dependencymanagement></dependencies>

[英]Multi-module Maven/Spring boot project using <dependencies> vs. <dependencyManagement> tags?

I refer to this post as I googled and still have some questions regarding the differences between using the <dependencies> XML tag in the pom.xml vs <dependencyManagement> XML tag in a multi-module Maven project : https://stackoverflow.com /questions/2619598/differences-between-dependencymanagement-and-dependencies-in-maven#:~:text=Dependency%20Management%20allows%20to%20consolidate,that%20inherits%20a%20common%20parent .

這是我的項目結構的一個平局。io - 它只是一個 Spring 引導/Maven 項目,具有根 pom.xml(從父 POM 繼承和三個子模塊) 在此處輸入圖像描述

根據該 ^ 帖子,似乎<dependencyManagement>標記可用於多模塊 Maven 項目以集中版本,scope 等。 用戶說您仍然必須在子模塊中的<dependencyManagement>封閉標記 AGAIN 中重新聲明任何依賴項......這是真的嗎? 如果是這樣,為什么不在父 POM 中使用常規封閉標記,因為所有子模塊都將繼承此依賴項? 我自己用 Spring Boot Starter Parent 測試了這個,我可以通過在我的根項目的父 POM 中聲明它來看到,所有依賴項,如 spring-core、tomcat-embed-core 等……都“注入”到我的項目和所有 3 個子模塊都可以使用這些依賴項

我想我不清楚使用<dependencyManagement>標記優於常規<dependencies>的優勢。 And If I understand the Maven POM inheritance correctly, all the Spring Boot Starter dependencies which are declared in the <parent> XML tag in my root POM.xml, are injected into my project and can be used by any child module (which is good和我想要的)。 此外,在我的根 POM.xml 中聲明的帶有<dependency>標記(不在<dependencyManagement>標記內)的任何依賴項都將被所有子模塊繼承,如果我不想要這種行為,我可以簡單地在使用該依賴項的特定子模塊(以避免重復)。

您可以在父項中定義通常的依賴項,並且它們被所有子項繼承(如您所說)。 如果這是您想要的行為,那么這很好。

但是如果你只需要某些模塊中的依賴,但又想確保整個項目的版本一致,那么你可以在父POM的dependencyManagement中定義版本,然后添加依賴條目(不帶版本)在您需要它們的模塊中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM