blue-sky
blue-sky

Reputation: 53806

Share dependencies among multiple POM files

What is the best practice method of sharing multiple dependencies across Maven projects.

I can think of the following 2 possible approaches -

  1. Creating a parent POM file and using that same POM file in each project that is using the set of dependencies.

  2. Creating a new maven project with a POM file that contains the dependencies. Then referencing this project within all projects that require the common dependencies.

Are there any other approaches, which one should I use ?

Upvotes: 8

Views: 3692

Answers (5)

Nacho
Nacho

Reputation: 119

OPTION 2 - You'll need to specify the dependency with import on your dependencyManagement section:

<dependencyManagement>
<dependencies>
  <dependency>
    <groupId>com.test</groupId>
    <artifactId>bom</artifactId>
    <version>1.0.0</version>
    <type>pom</type>
    <scope>import</scope>
  </dependency>
</dependencies>

For further info: http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management

Upvotes: 1

Alexander Pogrebnyak
Alexander Pogrebnyak

Reputation: 45576

I think option 2 is better, because it's containment vs inheritance issue.

What if you have to share 2 dependency traits among your projects. With option 1 you are stuck at the single inheritance level, while option 2 gives you added flexibility.

Upvotes: 1

ndeverge
ndeverge

Reputation: 21564

Take a look at the user guide : http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Management (ie use <dependencyManagement> in a parent POM).

Upvotes: 1

Stijn Geukens
Stijn Geukens

Reputation: 15628

IMO it depends a bit on your exact use case.

Option 1 is certainly a good way to go but it can become more difficult/complex to manage if you already have a parent project and especially if you have several of those shared dependency sets (you might find yourself lost in a complex hierarchy of multiple parents). Another disadvantage of option 1 is that (AFAIK) it is not possible to exclude inherited dependencies so if your project extends the parent pom you have no choice but to inherit all the dependencies.

In that case option 2 is easier to manage, clearer and more flexible. Create a maven project of type POM and add a dependency to this where needed. With this solution it is possible to exclude certain dependencies from the POM project which again makes this solution more flexible.

Upvotes: 5

TrueDub
TrueDub

Reputation: 5070

Option 1 works well - I'd suggest that.

Upvotes: 1

Related Questions