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:
major.minor.micro
release cycles, ex: 1.1.1, 1.1.2, 1.3.0, etc...My gut is telling me - get rid of the dependency spaghetti. Anyone will prove me wrong? Reactor (dependencies) graphs are more than welcome :-)
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.
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.
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.
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.
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.
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:
dependencyManagement
section, and use properties for your
versions. 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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With