Maven multi-module: aggregate common dependencies in a single one?

Eleaar picture Eleaar · May 22, 2011 · Viewed 14.1k times · Source

I have searched for such a question without finding anything, so here I go.

I have a multi-module maven project. Multiple modules all inherit the same parent, where common dependencies are defined. Among them, there is one my own modules, a 'common' one, where some common functionality is implemented.

My question is: What would be a better practice for common dependencies: Define them all explicitly in the parent, like I currently do? Or define them in a 'common' module, which other modules reference, and then rely on transitivity (like a single-entry-point for common dependencies)?

Answer

Caps picture Caps · Jul 26, 2011

It's best to use the dependencyManagement tag in your parent pom to define your dependencies and their versions then reference these dependencies in your sub modules where needed. When you require other sub modules in your project (ie your common submodule from another submodule) then the dependencies will be found transitively. For example:

In your parent pom:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.7</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

In your common pom (notice there is no version or scope):

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

And then you can just reference your common submodule from other submodules are you are already.