Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between dependencyManagement and dependencies in Maven

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.

For example:

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> 

Then in the child of Pro-par, I can use the 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? Why not define it directly in the needed module?

like image 789
hguser Avatar asked Apr 12 '10 02:04

hguser


People also ask

What is dependencyManagement in Maven?

What Is Maven Dependency Management? Dependency management in Maven allows teams to manage dependencies for multi-module projects and applications. These can consist of hundreds or even thousands of modules. Using Maven can help teams define, create, and maintain reproducible builds.

What is the difference between plugins and dependencies in Maven?

A plugin is an extension to Maven, something used to produce your artifact (maven-jar-plugin for an example, is used to, you guess it, make a jar out of your compiled classes and resources). A dependency is a library that is needed by the application you are building, at compile and/or test and/or runtime time.

What is the default scope of Maven dependency?

compile This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.

Why do we need dependencies?

No project task exists in isolation. With the possible exception of the very first project task, each task or activity relies on other activities in some way. Whether it's waiting on the output of another task or for resources to be freed up, you need to manage many dependencies throughout any project.


Video Answer


2 Answers

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:

  • 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.

like image 177
dcoder Avatar answered Oct 04 '22 20:10

dcoder


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.

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.

like image 33
Pascal Thivent Avatar answered Oct 04 '22 18:10

Pascal Thivent