Maven jar without dependencies by default - maven

Why does jar built with maven-jar not contain all dependencies libs by default (you need to use jar-with-dependencies option)

Because the vast majority of JAR modules have the use case of being in the end modules of an application (WAR, EAR, executable JAR).
The use case of directly having a fat executable JAR is rather limited compared to that modularity use case
.
And think about it, when you have a slim JAR, you always have the ability to create another module that will assemble that JAR with another in order to create an application. If you directly generate a fat JAR, you will reduce the ease of way your module can be integrated to others.

Related

Does maven-shade-plugin allow for packed dependency jars to be included?

maven-shade-plugin includes the dependencies unpacked within the uber jar, which makes sense because of the shading that is involved.
Is there a way to allow for a specific 'safe' dependency to be included as a whole jar (not unpacked into classes)? I could not see such a thing in the docs.

Creating uber jar with maven

My project inherits it's compile dependencies from parent and I have no control over it - can't change them to provided. Additionally, I have added another dependency 'a:b:1.0.0' to my project's pom. I want to include only 'a:b:1.0.0' with it's own dependencies (recursively ) to my uber jar.
Seems like neither assembly nor shade plugin doesn't support such case.
How this could be done ?
Thanks
Shading recursively has some significant disadvantages. Especially, the problem of duplicate files from multiple dependencies being overwritten with only a single version of the file. This can cause some pretty annoying problems to troubleshoot at runtime. You'd be better off using something like spring boot to build a standalone jar where instead of shading files into a single hierarchy, will embed dependent libraries into itself as a subdirectory and include on the classpath for you.
http://docs.spring.io/spring-boot/docs/current/maven-plugin/repackage-mojo.html

Maven: Jar with dependencies VS jar without dependencies

I am currently working in a Java project, and we use maven to build the final jar. There are two ways to build the jars as we all know, i.e. one single jar with-dependencies, and a jar without dependencies. In the latter case, we need to add dependent jars to the classpath as well.
In principle both can work, personally I prefer one jar with dependencies, but in the project team members decided to use separate jar without dependencies. So hereby I would like to know which choice is better?
This question has no answer, since it depends on what you need to do.
If you're writing an utility package or component, that could be a dependency of another project, then there's no point in having all the dependencies inside it - it's better to leave dependency resolution to a dependency manager, like Maven.
If you, instead, are writing a full application packaged as a jar, I mean something with a MainClass that can be executed with java -jar myjar, then having the dependencies together will make distribution easier.
Consider that, for instance, if you're writing a webapp, that'll be packaged as a WAR, it normally comes with dependencies.

Using maven-shade-plugin, how can one use dependencyReducedPom?

I'm working on an open-source project (neo4j-connector) which make intensive use of maven-shade-plugin to include in packaged RAR the neo4j application without referencing multiple jars (seems like a limitation of maven-rar-plugin).
Anyway, when doing so, the neo4j-connector-impl (which uses the shade plugin) pom references neo4j as a dependency, which is not totally exact, as neo4j source code is embedded in neo4j-connector-impl jar.
I've noticed there is a createDependencyReducedPom flag that allows one to generate a pom containing only non-shaded components. How can i use that pom instead of the standard one for dependencies of that project ?

Exclude unused parts of dependencies from jar (Maven)

We have a small project with some heavy-weight dependencies (e.g. Spring) of which we only use small parts. Therefore, the JAR we get when packing with all dependencies weighs several megabytes, even for out two-class-server. This seems unnecessary.
Is there a way to restrict JAR assembly to actually used (class) files?
You can use the maven-shade-plugin to create a Jar-with-dependencies (your project and the dependencies merged into one big jar) while limiting the classes or packages that are added to that jar. See the includes / excludes page for reference.
If you don't want to manually specify what needs to be included, perhaps there is a way to integrate ProGuard with your build.
It's not possible to include only classes which are used. But you can exclude dependencies from your depencies to reduce the JAR size. Only drawback: you need to know what you can exclude and what not.

Resources