dependencyManagement和dependencies之间的区别是什么?
我在Apache Maven网站上看过文档。
在dependencyManagement下定义的依赖项似乎可以在其子模块中使用,而无需指定版本。
例如:
父项目(Pro-par)在dependencyManagement下定义了一个依赖项:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8</version>
</dependency>
</dependencies>
</dependencyManagement>
然后在Pro-par的子函数中,我可以使用junit:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
但是,我想知道是否有必要在父pom中定义junit ?为什么不在需要的模块中直接定义它呢?
我在这个问题上迟到了,但我认为它值得一个比公认的回答更清晰的回答(公认的回答是正确的,但没有强调实际重要的部分,这需要您自己推断)。
在父POM中,<dependencies>和<dependencyManagement>之间的主要区别是:
Artifacts specified in the <dependencies> section will ALWAYS be included as a dependency of the child module(s).
Artifacts specified in the <dependencyManagement> section, will only be included in the child module if they were also specified in the <dependencies> section of the child module itself. Why is it good you ask? Because you specify the version and/or scope in the parent, and you can leave them out when specifying the dependencies in the child POM. This can help you use unified versions for dependencies for child modules, without specifying the version in each child module.
用我自己的话来说,你的父项目帮助你提供了两种依赖:
implicit dependencies : all the dependencies defined in the <dependencies> section in your parent-project are inherited by all the child-projects
explicit dependencies : allows you to select, the dependencies to apply in your child-projects. Thus, you use the <dependencyManagement> section, to declare all the dependencies you are going to use in your different child-projects. The most important thing is that, in this section, you define a <version> so that you don't have to declare it again in your child-project.
在我看来,<dependencyManagement>(如果我错了请纠正我)只是通过帮助您集中依赖项的版本而有用。它就像一种辅助功能。
作为最佳实践,您的<dependencyManagement>必须在父项目中,其他项目将继承它。一个典型的例子是通过声明Spring父项目来创建Spring项目。
在我看来,还有一件事没有被充分强调,那就是不想要的继承。
下面是一个增量的例子:
我在父母的遗言中声明:
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
</dependencies>
繁荣!我把它放在我的子A、子B和子C模块中:
由子poms继承的隐式
一个单独的地方来管理
不需要在child pooms中重新声明任何内容
如果我想,我仍然可以在子B中重新调用和覆盖到18.0版本。
但是,如果我最终在Child C中不需要番石榴,在未来的Child D和Child E模块中也不需要番石榴呢?
他们仍然会继承它,这是不希望的!
这就像Java的God Object代码一样,从类中继承了一些有用的部分,同时也继承了大量不需要的东西。
这就是<dependencyManagement>发挥作用的地方。当你把它添加到你的父pom,你所有的子模块停止看到它。因此,你被迫进入每个单独的模块,确实需要它,并再次声明它(子A和子B,但没有版本)。
显然,您不会为Child C这样做,因此您的模块仍然是精简的。
有几个答案概述了maven的< dependencies >和<dependencyManagement>标记之间的区别。
但是,下面简要阐述了几点:
<dependencyManagement> allows to consolidate all dependencies (used at child pom level) used across different modules -- clarity, central dependency version management
<dependencyManagement> allows to easily upgrade/downgrade dependencies based on need, in other scenario this needs to be exercised at every child pom level -- consistency
dependencies provided in <dependencies> tag is always imported, while dependencies provided at <dependencyManagement> in parent pom will be imported only if child pom has respective entry in its <dependencies> tag.