views:

472

answers:

5

My project uses Java libraries that have their own dependencies (Hadoop, Jetty for example). I end up with different versions of the same dependencies, like ant 1.4.5, 1.4.6. My project may want to use ant 1.4.7. This is a small example, can get more complicated with larger dependencies like HTTP Commons.

How do I get all the libraries and dependencies to play nice? Is there a way to isolate each library (Hadoop, Jetty) so they only use their dependencies?

+1  A: 

If you have the source code, compile everything together. If you don't, unfortunately you will have to target your source for the lowest-common-denominator with the target option to javac. You only have to do this if there are actual issues when running the application, which there rarely should be as long as the jvm is a current version (Java is very strict about binary compatibility with older versions).

Jason Coco
+2  A: 

You may choose to manage all of these with a depenency management framework - like OSGI. Take a look at the spring framework dynamic modules http://www.springsource.org/osgi

You can also take a look at the part of the framework where Eclipse implements OSGI. Take a look here http://www.eclipse.org/osgi/

The short answer is just to go for the lowest common denominator. Remember that the 'endorsed' directory is your friend - when it comes to managing conflicting dependencies.

hawkeye
+2  A: 

Maven will also generally handle this pretty well. If not completely, it will at least handle the bulk of it, then you can sort out the issues that are left over.

Of course that means that you have to change your build process, but it might be worth your while for not pulling your hair out over this.

Spencer K
+1  A: 

JarJar to the rescue!

An ant taks that both 1) packs many jars into one, and 2) allows you to rename dependencies in class files and thus load two versions of the same library!

Adrian
A: 

J G mentioned OSGi which was even my first thought when reading this question.

Having multiple versions of the same library is a strong point in OSGi. If we have some third party products already mentioned i think its fair to mention the specification behind it as well.

You can get it from the official osgi site http://osgi.org

Toni Menzel