Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any reasons to keep explicit dependency declaration for my own transitive dependencies in Maven?

I have been reading for a while about an explicit versus transitive (implicit) dependency declaration in Maven. Most of the people tend to agree that you should always explicitly declare the libraries that your project depends on, mostly to avoid versions mismatch.

That is perfectly reasonable, but how should we tackle our internal dependencies? I see absolutely no reason to keep the explicit dependency between the modules, if they can be resolved via transitive mechanism.

My use case scenario:

  • my team develops software in major.minor.micro release cycles, ex: 1.1.1, 1.1.2, 1.3.0, etc...
  • with each release we increment the versioning scheme for all of our modules in the project (so A:1.0, B:1.0 become A:1.1, B:1.1)
  • we are using reactor projects, nested as far as two levels deep

My gut is telling me - get rid of the dependency spaghetti. Anyone will prove me wrong? Reactor (dependencies) graphs are more than welcome :-)

An example:

My parent project uses following modules (external dependencies omitted):

Project:1.1   
 - core:1.1
 - +-- util:1.1 
 -   +-- xml-helper:1.1
 - logic:1.1
 - +-- util:1.1 
 -   +-- xml-helper:1.1
 - gui:1.1

The question is: should I declare xml-helper:1.1 as a dependency in core's and logic's pom.xml? That dependency will be automatically resolved (transitive) as I'm using util module.

If I declare it, I get a larger pom to maintain.

If I skip it, I might get into troubles when dependencies evolve over time.

like image 718
ŁukaszBachman Avatar asked Dec 27 '13 12:12

ŁukaszBachman


People also ask

How do you exclude all transitive dependencies of a Maven dependency?

Exclude the transitive dependencyOpen the dependency POM and find the transitive dependency you want to exclude. Copy groupId and artifactId . In your project POM, underneath your active dependency, enter exclusions and using code completion paste the copied info of the dependency you want to exclude.

What are the advantages of transitive dependency management?

The simple advantage is that you don't need to think about the transitive dependencies. This means you only need to think: I would like to use Tika lib and don't need to bother about their dependencies... So If you use a class of Tika core in your code you have the dependency available.

Does Maven support transitive dependencies?

Transitive Dependencies. Maven avoids the need to discover and specify the libraries that your own dependencies require by including transitive dependencies automatically. This feature is facilitated by reading the project files of your dependencies from the remote repositories specified.

How can you exclude a transitive dependency from downloading it?

Multiple transitive dependencies can be excluded by using the <exclusion> tag for each of the dependency you want to exclude and placing all these exclusion tags inside the <exclusions> tag in pom. xml. You will need to mention the group id and artifact id of the dependency you wish to exclude in the exclusion tag.


1 Answers

You've got a few questions here, so I'll try and answer them all.

That is perfectly reasonable, but how should we tackle our internal dependencies?

It looks like you have a module based project. To mitigate version conflicts I would suggest one of 2 possibilities:

  1. Put your common dependencies in your parent modules dependencyManagement section, and use properties for your versions.
  2. Use a base pom, and put your dependencies in its dependencyManagement section.

Check this answer out for more information.

My gut is telling me - get rid of the dependency spaghetti. Anyone will prove me wrong?

I think this is a subjective question, and may have been asked before but I'll still give my opinion.

No. I think you're on the right track. I'm not totally sold on the "declare your transitive dependencies if you're using them" opinion. One of the great benefits of using maven is that you get the transitive dependencies. If you had to declare dependencies for everything you used, I'd imagine that your poms would grow to be unmanageable beasts pretty quickly.

The question is: should I declare xml-helper:1.1 as a dependency in core's and logic's pom.xml?

Again, I would lean towards using dependencyManagement as opposed to declaration in each pom.

I hope this helps!

like image 188
javamonkey79 Avatar answered Sep 28 '22 04:09

javamonkey79