31

Do you think it is a good practice to remove every transitive dependencies that can be found in a maven pom?

Example:
My project depends on A and B.
B is also a transitive dependency of A.
Should I keep B in my pom or remove it ?

What is the best:
having all known jars, even transitive one, declared on the pom or keeping only the top level jars ?

This is a little bit subjective, but I am trying to clean some huge poms (parent and children) with a lot of transitive dependencies. I want to keep my pom as simple as possible, but I want also them to be maintainable.

Guillaume
  • 5,488
  • 11
  • 47
  • 83

2 Answers2

27

If your project has direct dependencies on B then you should keep it even if B is a transitive dependency of A. It can be that in the next version A won't use B an you'll have to restructure the pom.xml.

Generally, Maven dependencies should reflect the logical project dependencies.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Thanks for the answer and the link. I like this advice: 'reflect the logical project dependencies' – Guillaume Nov 22 '10 at 09:08
  • 2
    Don't you think that you should make a distinction here between internal and external dependencies? I had similar question: http://stackoverflow.com/questions/20800571/are-there-any-reasons-to-keep-explicit-dependency-declaration-for-my-own-transit I still can't see a reason why I should bother with declaring all the dependencies between my own modules. If APIs will change and the project would fail to compile, then I would fix it and most likely improve the code maintainability along the way :) – ŁukaszBachman Dec 30 '13 at 08:59
  • How is the link related to the question/answer? – AlikElzin-kilaka Nov 18 '15 at 14:30
  • @AlikElzin-kilaka I've started MSTP as a plugin to analyze project dependencies and give recommendations what to include as a direct dependency and what to remove. This is exactly the question being asked here. However, I've never got to the production release, so I'll edit out the [link](http://confluence.highsource.org/display/MSTP/Home). – lexicore Nov 18 '15 at 16:13
15

I would prefer to avoid the declaration of the transitive dependencies, and explicitly include them in the pom if there is a good reason for that. Here are my arguments:

  • I try to keep the pom as simple as possible. With the transitive dependencies declared, even if they are used explicitly, the Maven pom becomes more verbose.

By declaring the transitive dependencies (even if you explicitly need them):

  • Redundancy in the declaration is introduced, because this information is already in the pom descriptor of the artifact that is required.

  • If a new version of the artifact required does not depend on the transitive dependency anymore, you have to remove the transitive dependency from your assembly yourself, if that transitive dependency is explicitly declared.

  • The information for the transitivity gets manipulated by declaring the transitive dependency explicitly.

It would make sense to include a dependency explicitly in the following case:

  • You have one two dependencies, say C and D, that require different versions of the transitive dependency B (or you require a specific version of B in your project). In this case you have to choose a version of B and explicitly define the transitive dependency T. (*)

Conclusion: I would try to avoid the declaration, unless it makes sense to declare the artifact specifically (like in the case (*)).

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
i000174
  • 1,107
  • 10
  • 15
  • If you want to participate in the similar discussion: http://stackoverflow.com/questions/20800571/are-there-any-reasons-to-keep-explicit-dependency-declaration-for-my-own-transit – ŁukaszBachman Dec 30 '13 at 09:00