Skip to main content

πŸš€ Maven vs Gradle – The Complete Dependency & Packaging Guide

πŸš€ Maven vs Gradle – The Complete Dependency & Packaging Guide

As developers, we all have faced this moment πŸ‘‰ "Why my JAR is not running? Where did my dependency go?" πŸ˜‚
Let’s break down Maven vs Gradle concepts in a fun, colorful, and interview-friendly way.


πŸ”Œ 1. What is a Plugin & Why Do We Need It?

πŸ‘‰ Plugin = Tool that adds extra power to your build system.
Without plugins, Maven/Gradle can’t compile, test, or package.

  • Maven: Uses <plugin> inside pom.xml. Example: maven-compiler-plugin, spring-boot-maven-plugin.
  • Gradle: Uses plugins { } block. Example: id 'java', id 'org.springframework.boot'.

πŸ˜… Without Plugins?
Your project is like Iron Man without his suit — just a normal guy!

<!-- Maven Example -->

<plugin>

  <groupId>org.apache.maven.plugins</groupId>

  <artifactId>maven-compiler-plugin</artifactId>

  <version>3.11.0</version>

  <configuration>

    <source>17</source>

    <target>17</target>

  </configuration>

</plugin>

// Gradle Example

plugins {

    id 'java'

    id 'org.springframework.boot' version '3.3.0'

}


πŸ“¦ 2. BOM Imports

BOM = Bill of Materials = Dependency version manager.
It avoids version conflicts (a.k.a. "Jar Hell" πŸ”₯).

  • Maven: Uses <dependencyManagement> with import scope.
  • Gradle: Uses platform() or bomImports.
<!-- Maven Example -->

<dependencyManagement>

  <dependencies>

    <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-dependencies</artifactId>

      <version>3.3.0</version>

      <type>pom</type>

      <scope>import</scope>

    </dependency>

  </dependencies>

</dependencyManagement>

// Gradle Example

dependencies {

    implementation platform("org.springframework.boot:spring-boot-dependencies:3.3.0")

    implementation "org.springframework.boot:spring-boot-starter-web"

}

πŸ’‘ Real Time: Spring Boot uses BOM to keep web, security, data-jpa jars aligned in versions.


πŸ› ️ 3. Dependency Scopes

Gradle Scopes:

  • implementation → Used in main code & exported in JAR.
  • testImplementation → Only for tests (JUnit, Mockito).
  • compileOnly → Available at compile-time but NOT in JAR.
  • annotationProcessor → Special for tools like Lombok, MapStruct.

Maven Scopes:

  • compile (default) → Like Gradle implementation.
  • test → Like Gradle testImplementation.
  • provided → Like Gradle compileOnly.
  • system → Rare, local JAR.

πŸ’‘ Lombok Example:

<!-- Maven -->

<dependency>

  <groupId>org.projectlombok</groupId>

  <artifactId>lombok</artifactId>

  <version>1.18.30</version>

  <scope>provided</scope>

</dependency>

// Gradle

dependencies {

    compileOnly "org.projectlombok:lombok:1.18.30"

    annotationProcessor "org.projectlombok:lombok:1.18.30"

}

πŸ‘‰ Why both?
compileOnly → So IDE/compiler knows about @Getter, @Builder.
annotationProcessor → So Lombok generates bytecode during build.


πŸ₯€ 4. Flat JAR vs Fat JAR vs Boot JAR

  • Flat JAR → Only your code, no dependencies. (Normal library JAR).
  • Fat JAR → Your code + all dependencies bundled together.
  • Boot JAR → Spring Boot style fat JAR with embedded Tomcat/Jetty.

πŸ’‘ Real Example:
- Common library for microservices → Flat JAR.
- Final microservice → Boot JAR (so it can run java -jar directly).


πŸƒ 5. Special Case: MongoDB Example

Suppose your common library uses MongoDB driver, but not all microservices need it.
Solution πŸ‘‰ Don’t package Mongo inside the library JAR. Instead:

  • Mark Mongo as provided (Maven) / compileOnly (Gradle).
  • The actual microservice decides whether to include it or not.
<!-- Maven Common Library -->

<dependency>

  <groupId>org.mongodb</groupId>

  <artifactId>mongodb-driver-sync</artifactId>

  <version>4.11.0</version>

  <scope>provided</scope>

</dependency>


πŸ“¦ 6. Default Packaging Type

  • Maven → Default jar (unless specified as pom or war).
  • Gradle → Default is also jar for java plugin, bootJar if Spring Boot plugin applied.

🎯 Interview Questions

  1. What’s the difference between implementation and compileOnly in Gradle?
  2. How does Maven’s provided compare to Gradle’s compileOnly?
  3. Why do we need both compileOnly and annotationProcessor for Lombok?
  4. What is a BOM and why is it needed?
  5. Flat JAR vs Fat JAR vs Boot JAR → which one for common libraries?
  6. What is Maven’s default packaging type?
  7. Without plugins, can Maven/Gradle build projects?

🎁 Wrapping Up

So next time someone asks: πŸ‘‰ "Why do we need BOMs, plugins, or scopes?" You can confidently say: "Because without them, our build is like a pizza without cheese!" πŸ•πŸ˜‚

Comments

Popular posts from this blog

🐱 Tomcat vs ⚡ Netty – Which One Should You Use?

🐱 Tomcat vs ⚡ Netty – Which One Should You Use? So recently I got curious about this too πŸ€”. Everywhere in Spring Boot tutorials we see Tomcat . Then suddenly while exploring Spring WebFlux , the name Netty pops up. And I was like – “Wait, who’s this Netty guy trying to replace Tomcat?” πŸ˜… Let’s break it down with real-time examples , icons , and fun comparisons . 🐱 Tomcat – The Traditional Web Server Type: Servlet Container (blocking I/O) World: Used with Spring MVC Style: Thread-per-request model πŸ‘©‍πŸ’» Pros: Stable, widely used, battle-tested Cons: Struggles with huge concurrent connections πŸ‘‰ Example in real life: Tomcat is like a restaurant with fixed waiters 🍴. - Each customer = one thread/waiter - If too many customers come in at once → waiters run out → customers wait outside πŸšͺ ⚡ Netty – The Reactive Rockstar Type: Asynchronous Event-Driven Network Framework World: Default for Spring WebFlux Style: Event-lo...

🎭 Spring’s Secret: Why @Transactional & Friends Betray You Silently

πŸ’‘ Lesson Learned — Not a Prod Bug, But a Real Pain No, this wasn’t a production outage. Nobody screamed at me. But I sat for 3 hours wondering: “Why the heck is my @Transactional not rolling back!?” 😡‍πŸ’« “Why is Redis cache not working?” 🀯 Turned out, the issue was one silent villain: 🧱 Self-invocation 🀷 What Is @Transactional ? If you're new: @Transactional = Tells Spring to start a DB transaction when a method is called. It’ll commit if everything’s okay. It’ll rollback if something fails. 🧠 Think of it like wrapping your code in: try { beginTransaction(); // your logic commit(); } catch(Exception e) { rollback(); } πŸ•΅️ Real-Life Analogy — The Gateway Community 🏘️ Let me tell you about my society — it has a strict watchman at the gate. Here’s how it works: πŸ›‚ Watchman = Spring Proxy 🏠 Your apartment = Your service class πŸšͺ Your room = A method inside that class πŸƒ Scenario 1: Outsider Visits Your friend from outside...

🌟 My Journey – From Zero to Senior Java Tech Lead 🌟

 There’s one thing I truly believe… If I can become a Java developer, then anyone in the world can. πŸ’― Sounds crazy? Let me take you back. πŸ•“ Back in 2015… I had zero coding knowledge . Not just that — I had no interest in coding either. But life has its own plans. In 2016, I got a chance to move to Bangalore and joined a Java course at a training center. That’s where it all started — Every day, every session made me feel like: "Ohhh! Even I can be a developer!" That course didn’t just teach Java — it gave me confidence . πŸ§ͺ Two Life-Changing Incidents 1️⃣ The Interview That Wasn't Planned Halfway through my course, I had to urgently travel to Chennai to donate blood to a family member. After that emotional rollercoaster, I found myself reflecting on my skills and the future. The next day, as I was preparing for my move to Bangalore to complete the remaining four months of my course, I randomly thought — "Let me test my skills... let me just see...