🎯 There are 2 types of JARs in Maven:
| JAR Type | What it contains | Need dependencies separately? |
|---|---|---|
| Normal JAR | Only your code (.class files) | ✅ Yes, dependencies needed on target machine |
| Fat JAR / Uber JAR | Your code + all dependencies inside one big JAR | ❌ No, everything is bundled |
🔧 Example: Using Normal JAR
When you run:
It creates:
But this JAR:
-
Does not include your dependencies (like Gson, Spring Boot, etc.)
-
You will get
ClassNotFoundExceptionon the target machine unless:-
You also copy all 3 dependency JARs
-
Set
CLASSPATHor use-cpwhen running
-
Example:
💥 Better Way: Create a Fat JAR
This includes your code + all dependencies, so you only need one JAR to run anywhere.
✅ Add this Maven Plugin to pom.xml
🔨 Then run:
You’ll get a file like:
Now just copy this one JAR to any machine and run:
✅ Summary
| Situation | You Need on Target Machine |
|---|---|
Built normal JAR with mvn package | ❗ All 3 dependencies separately |
Built fat JAR with jar-with-dependencies | ✅ Just 1 JAR (includes everything) |
No comments:
Post a Comment