alecswan
alecswan

Reputation: 3680

Maven best practices: Explicit child dependencies vs transitive

I have a parent-pom.xml which declares a dependency A v1.0 which transitively depends on B v1.0 in <dependencyManagement> section.

My child-pom.xml references A v1.0 in <dependencies> section. However, the code of the child project also uses classes from dependency B v1.0.

The code of the child project compiles and runs without explicitly referencing B v1.0 because Maven resolves it transitively via A v1.0. What are the downsides of not explicitly referencing B v1.0 in <dependencies> section?

Upvotes: 3

Views: 617

Answers (1)

gizmo
gizmo

Reputation: 11909

Well, suppose you want to upgrade A to v1.1 and that this version does no longer use B, or use B v2.0 which has a different API. Doing so you'll break your code as it rely on something that does not exist anymore (B v1.0).

On the other side, if you had explicitly specified that your were using B in your child project then you would end up with one of the two options:

  • Everything works fine as you are still relying on B v1.0 and none of the code path of A you are using is actually using something of B that would be incompatible.
  • Your dependency on A is broken, but that can be easily discovered by looking at the dependency tree and identifying that the version of B has been bumped. You then have the choice to downgrade back your A dependency, upgrade your code to use the new B (if needed), or even to use OSGi to mitigate the use of incompatible versions of the same package.

Upvotes: 1

Related Questions