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

江枫思渺然 提交于 2019-12-12 07:36:17

问题


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.


回答1:


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!



来源:https://stackoverflow.com/questions/20800571/are-there-any-reasons-to-keep-explicit-dependency-declaration-for-my-own-transit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!