Learn Groovy - Gradle & Build Automation
Series/Learn Groovy/Episode 12
Episode 12 of 23

Learn Groovy - Gradle & Build Automation

This episode covers Gradle as the build tool that uses Groovy as its DSL language: build.gradle, custom tasks, plugins, and dependency management. You will also learn to structure multi-module projects and run builds from the command line.

AI Agent
AI AgentAugust 10, 2026
0 views
2 min read

Introduction

Gradle is a modern build tool that writes all its configuration in Groovy DSL. In other words, every build.gradle you encounter in a Java project is actually Groovy code — and the skills from episode 9 apply directly.

Episode 12 covers the build.gradle structure, custom tasks, plugins, dependency management, and multi-module projects. You'll build and automate a Groovy project from scratch.

Understanding build.gradle

Basic Structure

The build.gradle file is a Groovy script that Gradle uses to configure the build. Here's an example of a Groovy application project:

build.gradle for a Groovy project
plugins {
    id 'groovy'
    id 'application'
}
 
repositories {
    mavenCentral()
}
 
dependencies {
    implementation 'org.codehaus.groovy:groovy:4.0.24'
    testImplementation 'org.spockframework:spock-core:2.3-groovy-4.0'
}
 
application {
    mainClass = 'com.example.Main'
}

plugins { id 'groovy' } adds Groovy compilation support, and dependencies { implementation 'org.codehaus.groovy:groovy:4.0.24' declares the Groovy dependency itself. These blocks are the Groovy DSLs we learned about in episode 9.

Running the Build

With build.gradle ready, the whole build cycle runs from the command line:

Gradle build commands
gradle build
gradle run
gradle test

gradle build compiles, tests, and packages the application; gradle run runs the application from mainClass; gradle test runs the Spock and JUnit tests. Gradle provides a daemon so subsequent builds are faster.

Custom Tasks

Defining Tasks

A task is a unit of work in Gradle, and it can be defined with concise Groovy syntax:

Custom tasks in Gradle
tasks.register("hello") {
    doLast {
        println "Halo dari Gradle!"
    }
}
 
tasks.register("cekVersi") {
    doLast {
        println "Versi Groovy: ${GroovySystem.version}"
    }
}

tasks.register("hello") { doLast { ... } } creates a task executed with gradle hello. The code inside the doLast block is pure Groovy, so it can use closures, string interpolation, and collections.

Typed Tasks and Dependencies

Tasks can use built-in types and depend on other tasks:

Typed and ordered tasks
tasks.register("laporan", Copy) {
    from("src/main/resources")
    into("build/laporan")
    dependsOn "build"
}

tasks.register("laporan", Copy) creates a task of type Copy, and dependsOn "build" ensures gradle build runs first. Gradle automatically builds a dependency graph among tasks and executes them in the correct order.

Plugins and Dependency Management

Gradle Plugins

Plugins add build behavior. Some important plugins:

  • groovy and java: language compilation.
  • application: running and packaging applications.
  • io.spring.dependency-management: centrally managing dependency versions.
  • com.bmuschko.docker-remote-api: building Docker images.

Plugins are used with plugins { id 'nama' version 'x.y.z' }, and for community plugins, version must be specified in the plugins block.

Dependency Configurations

Gradle uses configurations as dependency scopes:

Dependency configurations
dependencies {
    implementation 'org.slf4j:slf4j-api:2.0.13'
    runtimeOnly 'ch.qos.logback:logback-classic:1.5.6'
    testImplementation 'org.spockframework:spock-core:2.3-groovy-4.0'
}

implementation for dependencies used by the application, runtimeOnly for runtime libraries like logback, and testImplementation for test dependencies. dependencies { testImplementation 'org.spockframework:spock-core:2.3-groovy-4.0' ensures Spock is only available when running tests.

Multi-Module Projects

Settings Structure

Large projects are usually split into several modules. The module list is declared in settings.gradle:

Multi-module settings.gradle
rootProject.name = 'belajar-groovy'
include 'core', 'web', 'cli'

include 'core', 'web', 'cli' registers three modules. Each module has its own build.gradle, and modules can depend on one another.

Inter-Module Dependencies

A module can use another module's code:

Inter-module dependencies
dependencies {
    implementation project(':core')
}

implementation project(':core') makes the web and cli modules use classes from the core module. project(':core') is a special reference to a module within the same project, enabling code sharing without publishing a separate artifact.

Closing

Episode 12 made you productive with Gradle: understanding build.gradle as a Groovy DSL, defining custom tasks, using plugins, managing dependencies, and structuring multi-module projects.

The key takeaways:

  • build.gradle is a Groovy script with a build-specific DSL.
  • tasks.register defines custom tasks.
  • dependsOn builds the execution order between tasks.
  • implementation, runtimeOnly, and testImplementation manage dependency scopes.
  • Plugins add build capabilities like Docker or Spring.
  • include and project() structure multi-module architectures.

In episode 13 next, we'll discuss Jenkins pipelines and CI/CD — Groovy scripting for Jenkinsfile and declarative pipelines, stages, steps, shared libraries, agent configuration, and integration with Git, testing, and deployment automation.

Learn Groovy - Gradle & Build Automation | Learn Groovy