-1

I refer to this post as I googled and still have some questions regarding the differences between using the <dependencies> XML tag in the pom.xml vs <dependencyManagement> XML tag in a multi-module Maven project: https://stackoverflow.com/questions/2619598/differences-between-dependencymanagement-and-dependencies-in-maven#:~:text=Dependency%20Management%20allows%20to%20consolidate,that%20inherits%20a%20common%20parent.

This is a draw.io of the structure of my project - its just a Spring boot/Maven project with a root pom.xml (Parent) and three child modules each with their own POM inheriting from parent POM: enter image description here

Per that ^ post, it seems that the <dependencyManagement> tag can be used in multi-module Maven projects to centralize the version, scope etc.. of a dependency. The user stated that you still have to re-declare any dependency in the <dependencyManagement> enclosing tag AGAIN in the child module... is this true? if so, why not just use the regular enclosing tag in the parent POM, because then all the child modules will inherit this dependency? I tested this myself with the Spring Boot Starter Parent, and I can see by declaring it in the parent POM of my root project, all the dependencies like spring-core, tomcat-embed-core, etc.. are "injected" into my project and all 3 child modules can use these dependencies

I guess I don't clearly understand the advantage to using <dependencyManagement>tag over regular <dependencies>. And If I understand the Maven POM inheritance correctly, all the Spring Boot Starter dependencies which are declared in the <parent> XML tag in my root POM.xml, are injected into my project and can be used by any child module (which is good and what I want). Also, any dependency declared in my root POM.xml with a <dependency> tag (not inside <dependencyManagement> tag) will be inherited by all child modules, and if I don't want this behavior, I can simply define the dependency in the specific child module that uses that dependency (to avoid repetition).

ennth
  • 1,698
  • 5
  • 31
  • 63

1 Answers1

3

You can define usual dependencies in the parent, and they are inherited by all children (as you said). If this is the behaviour that you want, then this is fine.

But if you need the dependency only in some of the modules, but you want to make sure that the version is consistent throughout your project, then you can define the version in the dependencyManagement of the parent POM and then add dependency entries (without version) in the modules where you need them.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • What about ? In my multi-module maven project, if I declare plugins like maven-surefire-plugin and surefire-junit47 plugin in the ROOT pom.xml (parent), do I need to redeclare all of these plugins in the child module POMS? – ennth Apr 29 '21 at 19:25
  • You can control the version and default configuration of the plugins using ``. Once you do that, you'll only need to declare that the config should be applied to a certain module by putting an entry with a `groupId` and `artifactId` in the `` section. For plugins that are applied automatically, you don't need even that. – crizzis Apr 29 '21 at 19:30
  • @ennth You should only define the maven-surefire-plugin but never the `surefire-junit47` provider. This is controlled by dependencies which means by defining a dependency to junit 4.... – khmarbaise Apr 29 '21 at 20:40