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

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.


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.


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 (*)).