Full Stack • Java • System Design • Cloud • AI Engineering

DevOps2026-06-12

Maven vs Gradle: Complete Build Tools Guide with Diagrams & Real Examples

Comprehensive guide to Maven and Gradle build tools with detailed diagrams, data flow charts, real-world examples, and best practices for Java projects

Maven vs Gradle: Complete Build Tools Guide

📋 Table of Contents

  1. What is a Build Tool?
  2. Maven Deep Dive
  3. Gradle Deep Dive
  4. Maven vs Gradle Comparison
  5. Best Practices
  6. Interview Questions

What is a Build Tool?

A build tool automates the software build process, transforming source code into executable applications.

Core Functions

┌─────────────────────────────────────────────────────────┐
│              Build Tool Responsibilities                │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  1. Dependency Management                               │
│     └─ Download & manage external libraries            │
│                                                         │
│  2. Compilation                                         │
│     └─ Convert source code to bytecode                 │
│                                                         │
│  3. Testing                                             │
│     └─ Run unit & integration tests                    │
│                                                         │
│  4. Packaging                                           │
│     └─ Create JAR/WAR/EAR files                        │
│                                                         │
│  5. Deployment                                          │
│     └─ Deploy to repositories/servers                  │
│                                                         │
│  6. Quality Checks                                      │
│     └─ Code coverage, static analysis                  │
│                                                         │
└─────────────────────────────────────────────────────────┘

Build Process Flow

Source Code → Compile → Test → Package → Deploy
     │           │        │        │         │
     ↓           ↓        ↓        ↓         ↓
  .java       .class    JUnit    .jar     Nexus/
  files       files    tests    file   Artifactory

Maven Deep Dive

What is Maven?

Maven is a build automation and project management tool that uses convention over configuration.

Key Characteristics:

  • XML-based configuration (pom.xml)
  • Standardized project structure
  • Declarative approach
  • Plugin-based architecture
  • Central repository system

Maven Project Structure

my-project/
├── pom.xml                    ← Build configuration
├── src/
│   ├── main/
│   │   ├── java/             ← Application source code
│   │   │   └── com/
│   │   │       └── example/
│   │   │           └── App.java
│   │   ├── resources/        ← Configuration files
│   │   │   └── application.properties
│   │   └── webapp/           ← Web resources (if web app)
│   │       └── WEB-INF/
│   └── test/
│       ├── java/             ← Test source code
│       │   └── com/
│       │       └── example/
│       │           └── AppTest.java
│       └── resources/        ← Test resources
└── target/                   ← Build output (generated)
    ├── classes/
    ├── test-classes/
    └── my-project-1.0.jar

Maven Build Lifecycle

┌──────────────────────────────────────────────────────────┐
│              Maven Build Lifecycle Phases                │
└──────────────────────────────────────────────────────────┘

validate
   │  Validate project structure and configuration
   ↓
compile
   │  Compile source code (src/main/java → target/classes)
   ↓
test
   │  Run unit tests (src/test/java)
   ↓
package
   │  Package compiled code (create JAR/WAR)
   ↓
verify
   │  Run integration tests and checks
   ↓
install
   │  Install package to local repository (~/.m2/repository)
   ↓
deploy
   │  Deploy to remote repository (Nexus/Artifactory)
   ↓

Complete Maven POM Example

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <modelVersion>4.0.0</modelVersion>
    
    <!-- Project Coordinates -->
    <groupId>com.codewithvenu</groupId>
    <artifactId>ecommerce-api</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <!-- Project Information -->
    <name>E-Commerce API</name>
    <description>RESTful API for E-Commerce Platform</description>
    
    <!-- Properties -->
    <properties>
        <java.version>17</java.version>
        <spring.boot.version>3.2.0</spring.boot.version>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    
    <!-- Parent POM -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
        <relativePath/>
    </parent>
    
    <!-- Dependencies -->
    <dependencies>
        <!-- Spring Boot Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- Spring Boot Data JPA -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        
        <!-- PostgreSQL Driver -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        
        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        
        <!-- Spring Boot Test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    <!-- Build Configuration -->
    <build>
        <plugins>
            <!-- Spring Boot Maven Plugin -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            
            <!-- Maven Compiler Plugin -->
            <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>
            
            <!-- Maven Surefire Plugin (Unit Tests) -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
        </plugins>
    </build>
    
    <!-- Repositories -->
    <repositories>
        <repository>
            <id>central</id>
            <url>https://repo.maven.apache.org/maven2</url>
        </repository>
    </repositories>
    
</project>

Maven Commands

# Clean build artifacts
mvn clean

# Compile source code
mvn compile

# Run tests
mvn test

# Package application
mvn package

# Install to local repository
mvn install

# Deploy to remote repository
mvn deploy

# Run Spring Boot application
mvn spring-boot:run

# Skip tests during build
mvn package -DskipTests

# Run specific test
mvn test -Dtest=UserServiceTest

# Generate project from archetype
mvn archetype:generate -DgroupId=com.example -DartifactId=my-app

# Display dependency tree
mvn dependency:tree

# Update dependencies
mvn versions:display-dependency-updates

Maven Dependency Resolution Flow

┌─────────────────────────────────────────────────────────┐
│         Maven Dependency Resolution Process             │
└─────────────────────────────────────────────────────────┘

1. Read pom.xml
   │
   ↓
2. Check Local Repository (~/.m2/repository)
   │
   ├─ Found? → Use it
   │
   └─ Not Found?
      │
      ↓
3. Check Remote Repositories (Maven Central)
   │
   ↓
4. Download to Local Repository
   │
   ↓
5. Resolve Transitive Dependencies
   │
   ↓
6. Build Classpath
   │
   ↓
7. Ready for Build

Example:
spring-boot-starter-web
  ├─ spring-boot-starter
  │  ├─ spring-boot
  │  ├─ spring-boot-autoconfigure
  │  └─ logback-classic
  ├─ spring-web
  ├─ spring-webmvc
  └─ tomcat-embed-core

Gradle Deep Dive

What is Gradle?

Gradle is a modern, flexible build automation tool that uses Groovy or Kotlin DSL.

Key Characteristics:

  • Groovy/Kotlin DSL configuration
  • Highly customizable
  • Incremental builds
  • Build caching
  • Task-based execution

Gradle Project Structure

my-project/
├── build.gradle (or build.gradle.kts)  ← Build configuration
├── settings.gradle                      ← Multi-project settings
├── gradle/
│   └── wrapper/
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew                              ← Unix wrapper script
├── gradlew.bat                          ← Windows wrapper script
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/
│   │   │       └── example/
│   │   │           └── App.java
│   │   └── resources/
│   │       └── application.properties
│   └── test/
│       ├── java/
│       │   └── com/
│       │       └── example/
│       │           └── AppTest.java
│       └── resources/
└── build/                               ← Build output (generated)
    ├── classes/
    ├── libs/
    └── reports/

Gradle Build Lifecycle

┌──────────────────────────────────────────────────────────┐
│              Gradle Build Lifecycle Phases               │
└──────────────────────────────────────────────────────────┘

Initialization
   │  Determine which projects to build
   │  Execute settings.gradle
   ↓
Configuration
   │  Execute build.gradle for all projects
   │  Create task dependency graph
   ↓
Execution
   │  Execute tasks in dependency order
   │  Run only necessary tasks (incremental build)
   ↓

Task Execution Order:
compileJava → processResources → classes → jar → assemble → build

Complete Gradle Build File (Groovy DSL)

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.2.0'
    id 'io.spring.dependency-management' version '1.1.4'
}

group = 'com.codewithvenu'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = '17'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
    maven {
        url 'https://repo.spring.io/milestone'
    }
}

dependencies {
    // Spring Boot
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    
    // Database
    runtimeOnly 'org.postgresql:postgresql'
    
    // Lombok
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    
    // Testing
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.junit.jupiter:junit-jupiter'
}

tasks.named('test') {
    useJUnitPlatform()
    
    // Test configuration
    testLogging {
        events "passed", "skipped", "failed"
        exceptionFormat "full"
    }
    
    // Generate test reports
    reports {
        html.required = true
        junitXml.required = true
    }
}

// Custom task
task printVersion {
    doLast {
        println "Project version: ${version}"
    }
}

// Build optimization
tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
    options.compilerArgs << '-parameters'
}

// JAR configuration
jar {
    manifest {
        attributes(
            'Implementation-Title': project.name,
            'Implementation-Version': project.version,
            'Main-Class': 'com.codewithvenu.Application'
        )
    }
}

Complete Gradle Build File (Kotlin DSL)

plugins {
    java
    id("org.springframework.boot") version "3.2.0"
    id("io.spring.dependency-management") version "1.1.4"
}

group = "com.codewithvenu"
version = "1.0.0-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17

configurations {
    compileOnly {
        extendsFrom(configurations.annotationProcessor.get())
    }
}

repositories {
    mavenCentral()
}

dependencies {
    // Spring Boot
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-validation")
    
    // Database
    runtimeOnly("org.postgresql:postgresql")
    
    // Lombok
    compileOnly("org.projectlombok:lombok")
    annotationProcessor("org.projectlombok:lombok")
    
    // Testing
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.junit.jupiter:junit-jupiter")
}

tasks.withType<Test> {
    useJUnitPlatform()
    
    testLogging {
        events("passed", "skipped", "failed")
        exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
    }
}

tasks.withType<JavaCompile> {
    options.encoding = "UTF-8"
    options.compilerArgs.add("-parameters")
}

Gradle Commands

# List all tasks
./gradlew tasks

# Clean build
./gradlew clean

# Compile
./gradlew compileJava

# Run tests
./gradlew test

# Build project
./gradlew build

# Run application
./gradlew bootRun

# Skip tests
./gradlew build -x test

# Run specific test
./gradlew test --tests UserServiceTest

# Generate dependency report
./gradlew dependencies

# Check for dependency updates
./gradlew dependencyUpdates

# Build with info logging
./gradlew build --info

# Build with debug logging
./gradlew build --debug

# Continuous build (watch for changes)
./gradlew build --continuous

# Build with build cache
./gradlew build --build-cache

# Refresh dependencies
./gradlew build --refresh-dependencies

Gradle Task Dependency Graph

build
  │
  ├─ assemble
  │    │
  │    ├─ jar
  │    │    │
  │    │    └─ classes
  │    │         │
  │    │         ├─ compileJava
  │    │         └─ processResources
  │    │
  │    └─ bootJar (if Spring Boot)
  │
  └─ check
       │
       └─ test
            │
            ├─ testClasses
            │    │
            │    ├─ compileTestJava
            │    └─ processTestResources
            │
            └─ classes

Maven vs Gradle Comparison

Feature Comparison Table

Feature Maven Gradle
Configuration XML (pom.xml) Groovy/Kotlin DSL
Learning Curve Easier Steeper
Flexibility Limited Highly flexible
Performance Good Excellent
Build Speed Slower on large projects Faster (incremental builds)
Caching Limited Advanced build cache
Dependency Management Excellent Excellent
Custom Logic Via plugins (harder) Native DSL (easier)
Multi-module Projects Good Better
IDE Support Excellent Excellent
Android Development Not preferred Default tool
Enterprise Adoption Very high Growing
Community Larger Growing fast
Documentation Extensive Good

Performance Comparison

Build Time Comparison (Large Project with 100+ modules)

Maven:
├─ Clean Build:     ████████████████████ 120s
├─ Incremental:     ████████████ 60s
└─ With Tests:      ████████████████████████ 180s

Gradle:
├─ Clean Build:     ████████████ 60s
├─ Incremental:     ████ 20s (with cache)
└─ With Tests:      ████████████ 90s

Gradle Advantages:
✓ Incremental compilation
✓ Build cache
✓ Parallel execution
✓ Task output caching
✓ Configuration cache

Build Process Comparison

┌─────────────────────────────────────────────────────────┐
│                Maven Build Process                      │
└─────────────────────────────────────────────────────────┘

Source Code
    ↓
Read pom.xml
    ↓
Resolve Dependencies (Maven Central)
    ↓
Execute Lifecycle Phases (Sequential)
    ├─ validate
    ├─ compile
    ├─ test
    ├─ package
    └─ install
    ↓
Generate Artifacts


┌─────────────────────────────────────────────────────────┐
│                Gradle Build Process                     │
└─────────────────────────────────────────────────────────┘

Source Code
    ↓
Read build.gradle
    ↓
Configuration Phase (Build Task Graph)
    ↓
Resolve Dependencies (Maven Central, JCenter, etc.)
    ↓
Execute Tasks (Parallel when possible)
    ├─ Check Cache (Skip if up-to-date)
    ├─ compileJava (Incremental)
    ├─ test (Only changed tests)
    └─ jar
    ↓
Generate Artifacts (Cached for reuse)

Best Practices

Maven Best Practices

<!-- 1. Use Properties for Versions -->
<properties>
    <spring.version>3.2.0</spring.version>
    <junit.version>5.10.0</junit.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring.version}</version>
    </dependency>
</dependencies>

<!-- 2. Use Dependency Management -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- 3. Use Profiles for Different Environments -->
<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <spring.profiles.active>dev</spring.profiles.active>
        </properties>
    </profile>
    
    <profile>
        <id>prod</id>
        <properties>
            <spring.profiles.active>prod</spring.profiles.active>
        </properties>
    </profile>
</profiles>

<!-- 4. Exclude Transitive Dependencies When Needed -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Gradle Best Practices

// 1. Use Version Catalogs (Gradle 7.0+)
// gradle/libs.versions.toml
[versions]
spring-boot = "3.2.0"
junit = "5.10.0"

[libraries]
spring-boot-web = { module = "org.springframework.boot:spring-boot-starter-web", version.ref = "spring-boot" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit" }

// build.gradle
dependencies {
    implementation libs.spring.boot.web
    testImplementation libs.junit.jupiter
}

// 2. Use buildSrc for Custom Logic
// buildSrc/src/main/groovy/Dependencies.groovy
class Dependencies {
    static final String SPRING_BOOT = "3.2.0"
    static final String JUNIT = "5.10.0"
}

// 3. Enable Build Cache
// gradle.properties
org.gradle.caching=true
org.gradle.parallel=true
org.gradle.configureondemand=true

// 4. Use Task Configuration Avoidance
tasks.register("myTask") {
    doLast {
        println "Task executed"
    }
}

// 5. Optimize Dependencies
configurations.all {
    resolutionStrategy {
        // Force specific versions
        force 'com.google.guava:guava:31.1-jre'
        
        // Cache dynamic versions
        cacheDynamicVersionsFor 10, 'minutes'
        
        // Cache changing modules
        cacheChangingModulesFor 0, 'seconds'
    }
}

Interview Questions

Q1: What is a build tool and why do we need it?

Answer: A build tool automates the software build process including:

  • Dependency Management: Automatically downloads and manages libraries
  • Compilation: Converts source code to bytecode
  • Testing: Runs automated tests
  • Packaging: Creates deployable artifacts (JAR/WAR)
  • Deployment: Publishes to repositories

Without a build tool, developers would need to:

  • Manually download each library
  • Manage classpath manually
  • Compile files one by one
  • Run tests manually
  • Create packages manually

Q2: What is Maven and what are its key features?

Answer: Maven is a build automation and project management tool for Java projects.

Key Features:

  • Convention over Configuration: Standard project structure
  • Declarative: XML-based configuration (pom.xml)
  • Dependency Management: Transitive dependency resolution
  • Plugin Architecture: Extensible via plugins
  • Build Lifecycle: Predefined phases (compile, test, package, etc.)
  • Repository System: Central repository for artifacts

Maven Coordinates:

<groupId>com.company</groupId>      <!-- Organization -->
<artifactId>my-app</artifactId>     <!-- Project name -->
<version>1.0.0</version>            <!-- Version -->

Q3: What is Gradle and how is it different from Maven?

Answer: Gradle is a modern build automation tool using Groovy or Kotlin DSL.

Key Differences:

Aspect Maven Gradle
Configuration XML (verbose) DSL (concise)
Flexibility Limited Highly flexible
Performance Good Better (incremental builds)
Learning Curve Easier Steeper
Customization Via plugins Native DSL

Gradle Advantages:

  • Incremental compilation
  • Build caching
  • Parallel execution
  • Better for multi-module projects
  • Default for Android development

Q4: Explain Maven build lifecycle phases

Answer: Maven has three built-in lifecycles: default, clean, and site.

Default Lifecycle Phases:

validate  → Validate project structure
compile   → Compile source code
test      → Run unit tests
package   → Create JAR/WAR
verify    → Run integration tests
install   → Install to local repository
deploy    → Deploy to remote repository

Example:

mvn clean install
# Executes: clean → validate → compile → test → package → verify → install

Each phase executes all previous phases automatically.


Q5: What is dependency scope in Maven?

Answer: Dependency scope controls when a dependency is available.

Scopes:

Scope Compile Test Runtime Example
compile Spring Framework
provided Servlet API
runtime JDBC Driver
test JUnit
system Local JAR

Example:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>  <!-- Only needed at runtime -->
</dependency>

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <scope>test</scope>  <!-- Only for testing -->
</dependency>

Q6: How does Gradle achieve faster builds?

Answer: Gradle uses several optimization techniques:

1. Incremental Builds:

Only recompiles changed files
Tracks input/output of each task
Skips up-to-date tasks

2. Build Cache:

Caches task outputs
Reuses outputs across builds
Shares cache across machines

3. Parallel Execution:

Runs independent tasks in parallel
Utilizes multi-core processors

4. Configuration Cache:

Caches configuration phase
Faster subsequent builds

Example:

# First build: 60 seconds
./gradlew build

# Second build (no changes): 2 seconds
./gradlew build

# With cache from another machine: 5 seconds
./gradlew build --build-cache

Q7: What is transitive dependency and how is it resolved?

Answer: A transitive dependency is a dependency of your dependency.

Example:

Your Project
  └─ Spring Boot Web (direct dependency)
      ├─ Spring Core (transitive)
      ├─ Spring MVC (transitive)
      └─ Tomcat (transitive)

Resolution:

Maven/Gradle automatically:
1. Downloads direct dependencies
2. Reads their pom.xml/build.gradle
3. Downloads their dependencies
4. Resolves version conflicts
5. Builds final classpath

Conflict Resolution:

If multiple versions exist:
- Maven: Uses "nearest definition" (shortest path)
- Gradle: Uses "newest version" by default

Example:

Project → A:1.0 → B:2.0
       → C:1.0 → B:1.0

Result: B:2.0 (Maven - nearest)
        B:2.0 (Gradle - newest)

Q8: When should you use Maven vs Gradle?

Answer:

Use Maven when:

  • ✓ Building standard Java/Spring Boot applications
  • ✓ Team prefers simplicity and convention
  • ✓ Organization already uses Maven
  • ✓ Need predictable, standardized builds
  • ✓ Easier onboarding for new developers

Use Gradle when:

  • ✓ Large multi-module projects
  • ✓ Need faster build times
  • ✓ Require custom build logic
  • ✓ Building Android applications
  • ✓ Team comfortable with DSL
  • ✓ Need advanced caching and incremental builds

Real-World Examples:

Maven: Spring Boot microservices, enterprise Java apps
Gradle: Android apps, Netflix, LinkedIn, large-scale systems

Q9: How do you handle dependency conflicts?

Answer:

Maven:

<!-- Exclude transitive dependency -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- Force specific version -->
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.1-jre</version>
        </dependency>
    </dependencies>
</dependencyManagement>

Gradle:

// Exclude transitive dependency
implementation('org.springframework.boot:spring-boot-starter-web') {
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
}

// Force specific version
configurations.all {
    resolutionStrategy {
        force 'com.google.guava:guava:31.1-jre'
    }
}

Q10: What are Maven profiles and when to use them?

Answer: Maven profiles allow different build configurations for different environments.

Example:

<profiles>
    <!-- Development Profile -->
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <spring.profiles.active>dev</spring.profiles.active>
            <database.url>jdbc:postgresql://localhost:5432/devdb</database.url>
        </properties>
    </profile>
    
    <!-- Production Profile -->
    <profile>
        <id>prod</id>
        <properties>
            <spring.profiles.active>prod</spring.profiles.active>
            <database.url>jdbc:postgresql://prod-server:5432/proddb</database.url>
        </properties>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <optimize>true</optimize>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

Usage:

# Development build
mvn clean install

# Production build
mvn clean install -Pprod

# Multiple profiles
mvn clean install -Pprod,security

Summary

Key Takeaways

Maven:

  • ✅ XML-based, convention over configuration
  • ✅ Easier learning curve
  • ✅ Standard project structure
  • ✅ Widely used in enterprise Java
  • ✅ Best for standard Spring Boot projects

Gradle:

  • ✅ DSL-based (Groovy/Kotlin)
  • ✅ Faster builds with caching
  • ✅ Highly flexible and customizable
  • ✅ Better for multi-module projects
  • ✅ Default for Android development

Decision Matrix

Choose Maven if:
├─ Standard Java/Spring Boot project
├─ Team prefers simplicity
├─ Organization uses Maven
└─ Need predictable builds

Choose Gradle if:
├─ Large multi-module project
├─ Need faster build times
├─ Require custom build logic
├─ Building Android apps
└─ Team comfortable with DSL

Build Tool Evolution

Ant (2000)
  │  Manual dependency management
  │  XML-based
  ↓
Maven (2004)
  │  Automatic dependency management
  │  Convention over configuration
  │  XML-based
  ↓
Gradle (2012)
  │  DSL-based
  │  Incremental builds
  │  Build caching
  │  High performance
  ↓
Future: Build tools continue to evolve with focus on:
  - Speed optimization
  - Better caching
  - Cloud-native builds
  - AI-assisted dependency management

Remember: Both Maven and Gradle are excellent tools. The choice depends on your project requirements, team expertise, and organizational standards. A good developer should be comfortable with both!