Maven : Should I keep or remove declared dependencies that are also transitives dependencies?
Asked Answered
A

2

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.

Anjaanjali answered 19/11, 2010 at 15:59 Comment(0)
P
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.

Plexiglas answered 19/11, 2010 at 16:25 Comment(4)
Thanks for the answer and the link. I like this advice: 'reflect the logical project dependencies'Anjaanjali
Don't you think that you should make a distinction here between internal and external dependencies? I had similar question: #20801071 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 :)Wessels
How is the link related to the question/answer?Ut
@Ut 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.Plexiglas
O
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 (*)).

Ommiad answered 10/11, 2011 at 15:27 Comment(1)
If you want to participate in the similar discussion: #20801071Wessels

© 2022 - 2024 — McMap. All rights reserved.