What is the difference between dependencyManagement
and dependencies
?
I have seen the docs at Apache Maven web site.
It seems that a dependency def
If the dependency was defined in the top-level pom's dependencyManagement element, the child project did not have to explicitly list the version of the dependency. if the child project did define a version, it would override the version listed in the top-level POM’s dependencyManagement section. That is, the dependencyManagement version is only used when the child does not declare a version directly.
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.
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 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.
Just in my own words, your parent-project
helps you provide 2 kind of dependencies:
<dependencies>
section in your parent-project
are inherited by all the child-projects
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
. The <dependencyManagement>
in my point of view (correct me if I am wrong) is just useful by helping you centralize the version of your dependencies. It is like a kind of helper feature.
It's like you said; dependencyManagement
is used to pull all the dependency information into a common POM file, simplifying the references in the child POM file.
It becomes useful when you have multiple attributes that you don't want to retype in under multiple children projects.
Finally, dependencyManagement
can be used to define a standard version of an artifact to use across multiple projects.
In Eclipse, there is one more feature in the dependencyManagement
. When dependencies
is used without it, the unfound dependencies are noticed in the pom file. If dependencyManagement
is used, the unsolved dependencies remain unnoticed in the pom file and errors appear only in the java files. (imports and such...)