Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with fat jar dependency

I have a proprietary dependency that I use in my project that I can not refuse. It was built in one big fat jar with all dependent packages collected inside. By all I mean even common ones like slf4j-api, apache-commons, javax packages, etc.

Using it together with my own list of declared dependecies is risky because there's always a race in classloader on which class will be loaded first - mine or outdated class inside fat jar.

I was wondering is there a way around this problem? How to treat such fat jars? I'm using maven for dependency management.

like image 909
SimY4 Avatar asked Dec 24 '14 09:12

SimY4


2 Answers

In Maven, the order in which you define dependencies in your POM is significant. If you list them in the correct order, they should be added to the jar in that order, and whichever class is higher in the file, that's the one that will get loaded first.

If you will compose your runtime classpath out of multiple jars, then again, it's a matter of putting the jars in the right order.

like image 82
janos Avatar answered Sep 21 '22 15:09

janos


If you know that some functionalities of the fat jar will work when you exclude some fo their dependencies or you want to include them yourself, you can try this:

  1. Make a maven project that depends on the fat jar only
  2. Use maven-shade-plugin, in particular it's relocation feature to exclude packages you don't want, or to just relocate all the jar's classes into another package, and thus move them out of the way.
  3. Use the project's artifact instead of fat propietary jar in your other project.
like image 21
miljanm Avatar answered Sep 23 '22 15:09

miljanm