Q:

Maven dependencies with different versions of the same sub-dependency

I have this scenario with my Maven dependencies:

X1 needs D2.2.
Y1 needs Z1 which needs D2.1.

If I force everyone to use D2.2, then Z1 fails at runtime because it needs a version of a class in D2.1 (which is no longer in D2.2).  If I force everyone to use D2.1, then X1 fails because it needs a newer version of a class, which is now in D2.2.  Upgrading Z1 to the latest version has no effect because it still uses D2.1.  The same goes for X1 and Y1.
How can I make this work?
Thanks,
Alvaro

A:

You have just encountered the diamond dependency problem. It's a real pain, and it shows up wherever there is a order-like relationship. Dependency is order-like, so is inheritance, which is the reason that multiple inheritance is not permitted in Java. It even shows up in inference, with the so-called Nixon Diamond as the diamond dependency analog.
To solve this, if you have access to Z1, or if it's open source, patch it / fork it / modify it to use D2.2, then modify/fork/patch Y1 to use your patched Z1.
I recommend doing patching if you have your own private/mirrored artifact repository. Create a build job that clones the open-source version, make file modifications, build it and publish to your artifact repo. Alternatively, or in addition, make the open source change.
Good luck.

