tags:

views:

134

answers:

5

My application uses Http client2.0.2 jar file and my unit test suit uses Http client3.1 jar file.

How do I configure two version of same jar file in single application?

+2  A: 

Maybe you could use jarjar to embed Http client 2.0.2 into your app and let your unit tests use the more recent version.

Gregory Pakosz
+3  A: 

You need to set up two different classpaths. For the tests (which you probably run with a tool like ant), add http client 3.1 to the classpath.

When you deploy, then deploy http client 2.0.2 to the app server. Omit test classes and the other version of http client.

There is a drawback, though: You can't test classes which use the old http client (well, at least not locally). Therefore, I suggest to upgrade the application to the new version of the library.

Aaron Digulla
+1: clean solution ... it's how netbeans manage this issue ...
wj
+1  A: 

I really don't believe you should be testing against one jar, and deploying against another. I think that's the real issue here.

Brian Agnew
I'm using open source for unit testing, this open source internally used latest version http client (3.1). But I have used old version of jar file in Application.
Thomman
+1  A: 

You can have different versions of the same class (or library) in your application. That's not a big problem, you 'only' have to use different classloaders for your libraries.

You'd have to create a URLClassLoader to load your open source test application (parent classloader would be the system class loader), maybe another classloader for the 3.1 client version as a child of the first one. Then remove these libraries from the class path and resolve and 'start' the 'main' class from your open source application. It should either work directly or at least give enough hints (NoClassDefFoundError) for the missing steps (didn't try it before, sorry, it's pretty theoretical). Have a look at the JavaDoc for the java.lang.Classloader class (and its extenders)

If there was a requirement to use different versions of the same library in a single application, I'd use OSGi as a framework, because there you get this functionality for free. The client bundle could depend on the 2.0.2 client version, the test bundle on client version 3.1. You just have to edit the Manifest files and start the application with an OSGi framework (like Equinox).

Andreas_D
+1  A: 

Netbeans do this very well : you have 2 separate folders for the imported libraies, one for the test (Test Librairies) and the other for your app (Librairies) ...

If your IDE don't support this you can do it manually as suggested by Aaron Digulla and define differents classpaths in your ant config ... you can follow this tutorial to do it ...

wj