问题描述
dependencyManagement
和 dependencies
有什么区别?我在 Apache Maven 网站上看过文档.似乎在 dependencyManagement
下定义的依赖项可以在其子模块中使用,而无需指定版本.
What is the difference between dependencyManagement
and dependencies
?I have seen the docs at Apache Maven web site.It seems that a dependency defined under the dependencyManagement
can be used in its child modules without specifying the version.
例如:
父项目(Pro-par)在dependencyManagement
下定义了一个依赖:
A parent project (Pro-par) defines a dependency under the dependencyManagement
:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8</version>
</dependency>
</dependencies>
</dependencyManagement>
然后在 Pro-par 的孩子中,我可以使用 junit:
Then in the child of Pro-par, I can use the junit:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
但是,我想知道是否有必要在父pom中定义junit?为什么不直接在需要的模块中定义呢?
However, I wonder if it is necessary to define junit in the parent pom? Why not define it directly in the needed module?
推荐答案
依赖管理允许整合和集中管理依赖版本,而无需添加所有子项继承的依赖项.当您有一组项目(即多个)继承一个公共父项时,这尤其有用.
Dependency Management allows to consolidate and centralize the management of dependency versions without adding dependencies which are inherited by all children. This is especially useful when you have a set of projects (i.e. more than one) that inherits a common parent.
dependencyManagement
的另一个极其重要的用例是控制传递依赖项中使用的工件版本.如果没有例子,这很难解释.幸运的是,文档中对此进行了说明.
Another extremely important use case of dependencyManagement
is the control of versions of artifacts used in transitive dependencies. This is hard to explain without an example. Luckily, this is illustrated in the documentation.
这篇关于Maven中dependencyManagement和dependencies的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!