Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
556 views
in Technique[技术] by (71.8m points)

maven - Maven中dependencyManagement和依赖关系之间的区别(Differences between dependencyManagement and dependencies in Maven)

What is the difference between dependencyManagement and dependencies ?

(dependencyManagementdependencies什么区别?)

I have seen the docs at Apache Maven web site.

(我已经在Apache Maven网站上看到了这些文档。)

It seems that a dependency defined under the dependencyManagement can be used in its child modules without specifying the version.

(似乎可以在依赖项管理下定义的dependencyManagement在其子模块中使用,而无需指定版本。)

For example:

(例如:)

A parent project (Pro-par) defines a dependency under the dependencyManagement :

(父项目(专业人士)在dependencyManagement下定义了一个dependencyManagement :)

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8</version>
    </dependency>
 </dependencies>
</dependencyManagement>

Then in the child of Pro-par, I can use the junit:

(然后在Pro-par的子级中,我可以使用junit:)

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
 </dependencies>

However, I wonder if it is necessary to define junit in the parent pom?

(但是,我想知道是否有必要在父pom中定义junit吗?)

Why not define it directly in the needed module?

(为什么不直接在所需的模块中定义它?)

  ask by hguser translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm fashionably late to this question, but I think it's worth a clearer response than the accepted one (which is correct, but doesn't emphasize the actual important part, which you need to deduce yourself).

(对于这个问题,我时兴地晚了,但是我认为这比被接受的答案更明确的回答是值得的(这是正确的,但并不强调您需要推论的实际重要部分)。)

In the parent POM, the main difference between the <dependencies> and <dependencyManagement> is this:

(在父POM中, <dependencies><dependencyManagement>之间的主要区别是:)

Artifacts specified in the <dependencies> section will ALWAYS be included as a dependency of the child module(s).

(<dependencies>部分中指定的工件将始终作为子模块的依赖项包括在内。)

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.

(如果在<dependencyManagement>部分中指定的工件,也必须在子模块本身的<dependencies>部分中指定,则它们将仅包含在子模块中。)

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.

(因为您在父级中指定了版本和/或范围,并且可以在子级POM中指定依赖项时将其忽略。)

This can help you use unified versions for dependencies for child modules, without specifying the version in each child module.

(这可以帮助您将统一版本用于子模块的依赖性,而无需在每个子模块中指定版本。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...