How to deal with sugar addiction

I have my own addiction, which I use in my project, and I can’t refuse. It was built in one large thick jar with all the dependent packages collected inside. I mean even the usual ones like slf4j-api, apache-commons, javax packages, etc.

Using it together with my own list of declared dependencies is risky because there is always a race in classloader where the class will be loaded first - my or an obsolete class inside a live flag.

I was wondering if there is a way to solve this problem? How to treat such fatty cans? I am using maven for dependency management.

+5
source share
4 answers

I think there is a different way. I can wrap this jar library in my own classloader

URLClassLoader c1 = new URLClassLoader(new Url[] { new URL("file:lib/fatJarDep.jar"}); 

and create a factory that will create the classes of this library using this isolated classloader

 Class.forName("className", true, c1); 
+1
source

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

If you create your path from a runtime of several cans, then again, the question is about putting the cans in the correct order.

+2
source

If the jar has all the dependency classes retrieved internally, you cannot exclude them from loading classes, so the same dependecy function in your project may conflict.

You have to edit the fat jar and manually remove the classes to make the light version, then install it in your repo and access it in your pom.

+1
source

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

  • Create a maven project that only depends on a thick can
  • Use the maven-shade-plugin , in particular this relocation function, to eliminate unwanted packages or simply move all jar classes to another package, and thus remove them from the path.
  • Use a project artifact instead of oily impregnation in another project.
+1
source

Source: https://habr.com/ru/post/1209824/


All Articles