How to create self-containing (standalone) jar of

2019-01-27 05:53发布

问题:

I have a multi-module maven project. The parent POM and individual module's pom are working fine (because code in each module runs fine without any dependency issue and mvn:package goal runs without any warning/error). Now, I needed to create a jar for each module.

Project structure with their dependencies is roughly like this

ProjectABC
|
|--ModuleA
|     |-Log4J
|     |-Guava
|     |-pom.xml  
|
|--ModuleB
|     |-ModuleA
|     |-Commons
|     |-pom.xml         
|
|
|--MoudleC
|     |-ModuleB
|  
|-pom.xml

I followed this Maven multi-module project - copying all "package" JARS from submodules into parent/target/

And, able to get all my sub-module's jar into a mouduleJars folder, but I cannot run any jars because none of module's dependencies gets copied in a mouduleJars folder. For example if module A is dependent on Log4J I don't get log4j.jar in my mouduleJars folder, neither log4J is packaged inside moduleA.jar

Question

How to create self containing (standalone module jars) in a multi-module project. Or, how to get all the dependencies in a lib folder.

Temporary workaround

Right now, I use my IntelliJ IDEA ide to copy all dependencies (like log4J, Guvava etc) in a lib folder

and run maven generated moduleA.jar by adding the lib folder to the classpath. This runs fine, but I don't like this manual approach and wants maven to take care of it.

For who think it is duplicate or already answered and downvoters

My question is NOT asking Maven Assembly Submodules Multimodules Or this

Maven : Multimodule projects and versioning

I have already spent more than a day try to get this working with maven-assembly-plugin without any success.

Please let me know if need more details.

回答1:

If you want all the dependencies in the jars, you'll need to create an uberjar. Putting the dependencies in the jar is not the way jars normally work. An uberjar lets you do that though. Shade is a plugin that builds one in java. You can also do it this way: How can I create an executable JAR with dependencies using Maven?

If you want to create a lib folder, here's an answer that will explain how: force Maven2 to copy dependencies into target/lib



回答2:

I did this with the AppAssembler maven plugin. It auto copies all needed dependencies and creates shortcuts for your main classes. It's much easier then other plugins I tried.

For the uber-jar applications I use maven-shade-plugin for merging jars into one + proguard for shrinking, but AppAssembler is much quicker and stable to use.