Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)?

like image 671
Eleaar Avatar asked May 22 '11 08:05

Eleaar


1 Answers

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.

like image 68
Caps Avatar answered Nov 07 '22 14:11

Caps