๐ 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>
insidepom.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>
withimport
scope. - Gradle: Uses
platform()
orbomImports
.
<!-- 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 aspom
orwar
). - Gradle → Default is also
jar
forjava
plugin,bootJar
if Spring Boot plugin applied.
๐ฏ Interview Questions
- What’s the difference between implementation and compileOnly in Gradle?
- How does Maven’s provided compare to Gradle’s compileOnly?
- Why do we need both
compileOnly
andannotationProcessor
for Lombok? - What is a BOM and why is it needed?
- Flat JAR vs Fat JAR vs Boot JAR → which one for common libraries?
- What is Maven’s default packaging type?
- 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
Post a Comment