Reputation: 133
I am building a JPA library that can be consumed in other projects. What is the best approach for publishing the same? Should i publish as a fat jar with all dependencies or publish only the classes?
Kindly recommend a build plugin for building and publishing common utilities or libraries
Upvotes: 1
Views: 760
Reputation: 5045
The short: Never EVER publish anything as a fat jar. Your library should have a pom file that declare your libraries compile/runtime dependencies. There is no difference between publishing a JPA or any other library type. So just use the default Maven/Gradle publish mechanism
The longer: Your project's pom file should list the required runtime dependencies for your library, so when another project include you jar file as a dependency they get the transitive dependencies automatically. There is a vital difference between this and a fat jar. If your library requires Guava version 16, and you include it in another project that already have use version 20 of Guava, Maven/Gradle lets you select the version you want to use (or you can have both but it is a bad idea). When you start including other peoples code inside you jar, you loose this posibility, as you fat jar contains specific version of 3rd party libraries determined when you build your library. I have seen several MethodNotFoundError because someone included an old version of the library inside another jar. During development this could work because you also have the new version of the library, and it is first in the classpath, but at runtime the fat jar could be first, and the old versions of the class shadowed the newer version.
It is important to understand that when the JVM load jars from a folder, for instance in a webapp (WEB-INF/lib) you cannot control the order of the jar files in the classpath, this is why having fat jars or multiple versions of the same library is a very bad idea. It is even possible that two machines can read the same folder content in different order, because the default sort order is file system dependent.
Best of luck
Upvotes: 5