0

I have 2 different modules called A and B. I have also another module called C.

A uses C version 1.0

B uses C version 2.0

The problem is since C 2.0 is not backward-compatible even though the groupId and artifactId values are same for C 1.0 and C 2.0 I can't exclude one of them in my pom.xml and use the other one for A and B. Therefore exclusions tag doesn't work in this case. I need to inject a dependency to a specific module.

I found out I can use OSGI for this purpose but I couldn't make it work. If you have any OSGI tutorial or another solution it would be great.

Mehmet
  • 43
  • 1
  • 8
  • 1
    OSGi is a **massive** sledgehammer to crack a acorn in this case. Just exclude the dependency and include the specific version that you want. – Boris the Spider Mar 30 '14 at 09:06
  • @BoristheSpider Is there any way to use two different versions of a package in a single project? Package A is simply a jar package, I cannot (should not) modify it. On the other hand C 2.0 supports new features that I need to use in package B. – Mehmet Mar 30 '14 at 09:18
  • 1
    You are aware of using different classloaders – Thorbjørn Ravn Andersen Mar 30 '14 at 09:19

2 Answers2

1

Without using OSGI, there is the alternative of refactor the application in order to run A and B on separate WAR files, in the case this is being run in a web application server.

This way WAR 1 contains A and C 1.0, and WAR 2 contains B and C 2.0, and there is no interference in this case.

Angular University
  • 42,341
  • 15
  • 74
  • 81
  • It's a classic Java application, not a web application. AFAIK WAR option is only available for servlets. – Mehmet Mar 30 '14 at 09:15
  • then the alternative is either to split it into two separate applications if possible, otherwise it would be better to run in from an OSGI container, where the applications are two bundles that dependend on different bundle versions of C. You will need tu bundle C yourself if bundles are not available. Have a look at Apache Karaf lightweight OSGI container https://karaf.apache.org/ – Angular University Mar 30 '14 at 09:32
1

Try the Maven Shade plugin with relocation on project A and/or B. This will include the classes of C into your project. And because C gets a different package path, there won't be any class collisions anymore.

Robert Scholte
  • 11,889
  • 2
  • 35
  • 44