11

I've got a Maven project that contains two dependencies, A and B. Each of these depends transitively on C, but they depend on different versions of C. Let's say that A depends on C version 1, and B depends on C version 2.

Unfortunately, A is not bytecode-compatible with version 2, nor B with version 1. (As it happens, A is source-compatible with version 2, but I don't think that will help us here.)

This means that I need both versions of the transitive dependency in my project, and I need A to use version 1, and B to use version 2.

Is there a way of doing this?

I had assumed that I would need to use the shade plugin to relocate the package name of A and all its dependencies, but this doesn't seem to be possible. If I shade A, its dependencies don't get shaded, and it still picks up version 2, and fails to run.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
  • I dont think that its possible because jdk is going to just load one version not both. – Mr37037 Jun 01 '15 at 14:06
  • You could take a look here : http://stackoverflow.com/questions/13620281/what-is-the-maven-shade-plugin-used-for-and-why-would-you-want-to-relocate-java?rq=1 The first answer states that it's possible. The second answer provides some example that could be useful to you. – Melou Jun 01 '15 at 14:27
  • 4
    2 year old question. Still relevant. – Manu Manjunath Jan 08 '18 at 14:26

2 Answers2

3

Create another project wrapper A named A-wrapper. Relocate C in A-wrapper. Then in your main project, depends on A-wrapper and B.

I've met a similar problem on pb2 and pb3 and it is resolved using this way. https://stackoverflow.com/a/41394239/1395722

a.l.
  • 1,085
  • 12
  • 29
-1

Assuming dependency A requires v1 of C and dependency B requires v2 of C. You can create an uber jar of A containing v1 of C but changing the packaging using shade plugin, Example jar A has contents of C with new packaging "v1.c.something". Do the same for B, so jar B has contents of C with new packaging "v2.c.something". You need to include only the conflicting dependencies not all.

Udit Seth
  • 1
  • 1
  • The questioner's point is that both A and B are not in your control to simply shade the dependency C. – viji Dec 15 '22 at 23:54