2

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.

Community
  • 1
  • 1
Watt
  • 3,118
  • 14
  • 54
  • 85

2 Answers2

4

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

Community
  • 1
  • 1
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • My aim is to execute code in ModuleB. As you can see from the diagram, ModuleB is dependent on ModuleA as well. Please feel free to suggest a better apporach – Watt Oct 11 '13 at 19:22
  • +1, both the answers link you provided looking promising. I will try these out and get back to you. – Watt Oct 11 '13 at 19:40
  • Copying all the dependencies to a lib folder worked for me. I have added `lib/` in all module's maven-jar-plugin. I wait till end of day for few more good information from others on it, then accept your answer. uberjar looks great too, I will experiment with that later. – Watt Oct 11 '13 at 20:08
0

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.

Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68