0

I have two version of the same jar file. (version 1 and 2). My problem is that i want classes in a specific package to use one version and classes in another package to use the other version.Both the packages are under the same maven project.

Tried to add both the jar files as dependencies in the POM, but the second entry overrides the first one and only one version is added as dependency.

Is there a way to achieve this in Maven.?

7
  • 6
    Good lord, why would you need this? Commented May 23, 2014 at 9:39
  • 2
    re-write the code so it only needs one Commented May 23, 2014 at 9:40
  • If you really nedd, check osgi concepts, loading multiple versions of jar files. You need multiple class loaders Commented May 23, 2014 at 9:40
  • 1
    @christopher can think of one reason someone'd want to try this, and it's a bogus reason. That's when there's a bug in v1 that's fixed in v2 but there's been major API changes between v1 and v2 that would require refactoring a lot of code in the application. It's of course bogus as there's good reason to do just that... Commented May 23, 2014 at 9:41
  • Some of our packages are not yet ready to move to the new version. So those packages are to be using the old version. Commented May 23, 2014 at 9:41

2 Answers 2

1

Think about DLL Hell. The only way you can get various versions of the same class to coexist in a single JVM is to load each using a different class loader, and you don't want to go that way.

Rewrite your code so all of it works with the newest version of the library, or rewrite it so it doesn't need whatever changes in the library require you to use v2, your choice.

Sign up to request clarification or add additional context in comments.

Comments

0

Token ugly solution...

Split your project into two modules, where each module uses a different version of the dependency. To avoid the class-loader problems referenced in jwenting's excellent answer, use the Maven Shade Plugin to rename the dependency packages in one of the modules.

See Relocating Classes for an example of doing this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.