Gradle

Modern build automation tool

⚡ What is Gradle?

Gradle is a modern build automation tool that combines the power and flexibility of Ant with dependency management and conventions of Maven. It uses Groovy or Kotlin DSL for configuration, making builds more readable and maintainable.


// Simple Gradle build script
plugins {
    id 'java'
    id 'org.springframework.boot' version '3.2.0'
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
                                    

Gradle Features

🚀

Performance

Incremental builds and build cache

./gradlew build --build-cache
🔧

Flexible DSL

Groovy or Kotlin configuration

task hello {
    doLast {
        println 'Hello Gradle!'
    }
}
🔌

Plugin System

Rich ecosystem of plugins

plugins {
    id 'java'
    id 'application'
}
📦

Multi-project

Support for complex project structures

include 'api', 'web', 'core'

🔹 Basic Gradle Project Structure

Standard Gradle project layout:

my-app/
├── build.gradle
├── settings.gradle
├── gradlew
├── gradlew.bat
├── gradle/
│   └── wrapper/
├── src/
│   ├── main/
│   │   ├── java/
│   │   │   └── com/example/App.java
│   │   └── resources/
│   │       └── application.properties
│   └── test/
│       ├── java/
│       │   └── com/example/AppTest.java
│       └── resources/
└── build/
    ├── classes/
    └── libs/

Key Files:

build.gradle - Build configuration

settings.gradle - Project settings

gradlew - Gradle wrapper script

🔹 Complete build.gradle Example

A comprehensive Gradle build script:

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

group = 'com.example'
version = '1.0.0'
sourceCompatibility = '17'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    // Spring Boot starters
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    
    // Database
    runtimeOnly 'com.h2database:h2'
    runtimeOnly 'mysql:mysql-connector-java'
    
    // Development tools
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    
    // Testing
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
}

tasks.named('test') {
    useJUnitPlatform()
}

// Custom task
task hello {
    doLast {
        println 'Hello from Gradle!'
    }
}

// JAR configuration
jar {
    archiveBaseName = 'my-app'
    archiveVersion = '1.0.0'
}

🔹 Gradle Commands

Essential Gradle commands using the wrapper:

# Build the project
./gradlew build

# Clean build directory
./gradlew clean

# Compile main classes
./gradlew compileJava

# Run tests
./gradlew test

# Run the application
./gradlew bootRun

# Create JAR file
./gradlew jar

# List all tasks
./gradlew tasks

# List dependencies
./gradlew dependencies

# Generate project reports
./gradlew build --scan

# Run with specific profile
./gradlew bootRun --args='--spring.profiles.active=dev'

Task Dependencies:

build depends on: compileJava, test, jar

test depends on: compileTestJava, classes

🔹 Gradle vs Maven

Key differences between Gradle and Maven:

Gradle Advantages:

  • Performance: Incremental builds and build cache
  • Flexibility: Groovy/Kotlin DSL for complex logic
  • Conciseness: Less verbose configuration
  • Multi-project: Better support for large projects

Maven Advantages:

  • Maturity: Longer history and wider adoption
  • IDE Support: Better integration in some IDEs
  • Simplicity: Declarative XML configuration
  • Standards: Strong conventions and standards

🔸 Configuration Comparison

// Gradle (build.gradle)
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'junit:junit:4.13.2'
}
<!-- Maven (pom.xml) -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>

🔹 Multi-project Build

Organize large applications with multiple modules:

// settings.gradle
rootProject.name = 'my-application'
include 'core', 'web', 'api'

// Root build.gradle
subprojects {
    apply plugin: 'java'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        testImplementation 'junit:junit:4.13.2'
    }
}

// web/build.gradle
dependencies {
    implementation project(':core')
    implementation 'org.springframework.boot:spring-boot-starter-web'
}

// api/build.gradle
dependencies {
    implementation project(':core')
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
}

🧠 Test Your Knowledge

What is the Gradle build configuration file called?