依赖管理和依赖关系在maven中的区别 [英] differences between dependencymanagement and dependencies in maven

查看:126
本文介绍了依赖管理和依赖关系在maven中的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

依赖性管理和依赖关系之间有什么区别?
我已经看到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 it's 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中的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆