Creating an uber jar from within Java (NOT using Maven or Gradle)
Asked Answered
E

1

0

I would like to combine several jars into an Uberjar.

In Java I know how to shade dependencies using these two tools:

But I have a web service that needs to take a dynamic classpath and create an uberjar from it. But being that it is literally a backend server, Maven and Gradle are not tools available for use.

As you can see from the source code of these plugins, it's not just as easy as build a combined zip file with all their contents. You have to do some resolution on duplicate resources, and some special log4j cache configuration as well.

Is there a Java library that is capable of creating the same thing John Rengelman's shadow tool does, but without actually running from Maven/Gradle?

Earldom answered 27/12, 2021 at 14:58 Comment(3)
"But being that it is literally a backend server, Maven and Gradle are not tools available for use.". There's literally nothing that prevents your backend server from having Maven or Gradle installed.Cosentino
Well, it is considered a security risk to have compilation tools on a production server, so I understand why you wouldn't want to do that.Alwin
yeah that's what I was thinking too @Kayaman. It would be a little strange but Ultimately I think it's pretty doable. Last resort though.Earldom
E
1

Actually this turns out to be easy enough to do.

You create a ZipArchiveOutputStream and iterate through each jar file. You "apply" each jar to the output stream as you go.

But - you must make sure to exclude these files:

  • 'META-INF/*.RSA'
  • 'META-INF/*.SF'
  • 'META-INF/*.DSA'

This works, but results in this issue with logging: log4j2 ERROR StatusLogger Unrecognized conversion specifier

Then you have to properly handle Log4j2 plugin cache files as this class does:

https://github.com/edwgiz/maven-shaded-log4j-transformer/blob/master/src/main/java/io/github/edwgiz/log4j/maven/plugins/shade/transformer/Log4j2PluginCacheFileTransformer.java

This is now working for me.

Earldom answered 27/12, 2021 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.