dependencyManagement and scope

ben75 picture ben75 · Mar 5, 2013 · Viewed 25.7k times · Source

I usually put a <dependencyManagement> section in parent-project/pom.xml. This <dependencyManagement> section contains declaration and version for all dependencies of my children modules like this (i.e. without the <scope> element):

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

In all children modules (i.e. moduleX/pom.xml), I have:

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

Obviously, in this example I'm repeating the <scope>test</scope> multiple times for the same dependency (once in every child module needing junit).

My question is:
What are the best practices regarding <scope> declaration?
Is it better to put it in the <dependencyManagement>?
Or is it better to put it in the <dependencies> section of the child module (like in this post)? And why?
Is there any definitive answer to this question?

Answer

Lucas picture Lucas · Dec 23, 2013

A little late to the party, but I'll add my two cents. I recently ran into a very difficult to debug issue. I have a parent pom for managing dependencies across multiple projects. I had it set with all the dependencies common amongst them and included groupId, artifactId, version and the most common scope. My thinking would be that I would not have to include scope in the actual dependency section in each project if it fell in line with that most common scope. The problem occurred when some of those dependencies showed up as transitive dependencies. For example if

  • A depends on B at compile scope
  • B depends on C at compile scope
  • C is set to provided in dependencyManagement of parent

Then A's transitive dependency on C is determined to be provided. I'm not really sure if that makes sense or not, but it certainly is confusing.

Anyway, save yourself the hassle, and leave scope out of your dependencyManagement.