Master Gradle: Install, Configure, and Automate Your Builds

This guide walks you through Gradle’s core concepts, from installing the tool and initializing a project to creating custom tasks, managing plugins and dependencies, using the Kotlin DSL, and leveraging the Gradle Wrapper for reliable builds across environments.

Programmer DD
Programmer DD
Programmer DD
Master Gradle: Install, Configure, and Automate Your Builds

What is Gradle?

Gradle is a modern build automation tool that replaces XML‑based scripts with a Groovy (or Kotlin) DSL, offering high performance, multi‑language support, and frequent releases.

Key Features

Handles large‑scale builds efficiently.

Supports multiple languages and platforms.

Focuses on build speed and incremental execution.

Transparent feature roadmap and active community.

Installation

Download the binary from gradle.org/install , unzip it, and add GRADLE_HOME/bin to your PATH. Verify the installation with gradle -v.

Quick Start

mkdir gradle-demo
cd gradle-demo
gradle init

The command creates a basic project structure:

├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle

Creating a Task

Define a copy task in build.gradle using Groovy:

task copy(type: Copy, group: "Custom", description: "Copies sources to the dest directory") {
    from "src"
    into "dest"
}

Or with Kotlin DSL:

tasks.create<Copy>("copy") {
    description = "Copies sources to the dest directory"
    from("src")
    into("dest")
}

Task Execution Order

Tasks can depend on each other using dependsOn:

project('projectA') {
    task taskX(dependsOn: ':projectB:taskY') {
        doLast { println 'taskX' }
    }
}
project('projectB') {
    task taskY {
        doLast { println 'taskY' }
    }
}

Plugins

Gradle ships with many built‑in plugins. Apply the base plugin in Groovy:

plugins {
    id "base"
}

Or in Kotlin:

plugins {
    id("base")
}

Java Plugin

Apply the Java plugin to get compilation, testing, and packaging tasks:

plugins {
    id 'java'
}

Typical Java tasks include compileJava, processResources, jar, test, and clean.

Dependency Management

Declare repositories (Maven Central, Ivy, flatDir) and dependencies:

repositories {
    mavenCentral()
    ivy { url "http://ivy.example.com" }
    flatDir { dirs 'libs' }
}

dependencies {
    implementation 'org.apache.commons:commons-lang3:3.5'
    api 'commons-httpclient:commons-httpclient:3.1'
    compile files('libs/commons-lang.jar', 'libs/log4j.jar')
    compile('com.example:m:1.0') { exclude group: 'org.unwanted', module: 'x' }
}

Kotlin Support

Add the Kotlin JVM plugin and stdlib dependency:

plugins {
    id "org.jetbrains.kotlin.jvm" version "1.5.21"
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:1.5.21"
}

sourceSets {
    main.kotlin.srcDirs += 'src/main/myKotlin'
    main.java.srcDirs += 'src/main/myJava'
}

Gradle Wrapper

The wrapper ensures a consistent Gradle version per project. It reads gradle-wrapper.properties where distributionUrl defines the Gradle distribution to download.

Real‑World Build Script Example

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'jacoco'
apply plugin: 'checkstyle'
apply plugin: 'pmd'
apply plugin: 'findbugs'
apply plugin: 'eclipse'
apply plugin: 'idea'

group = 'Common'
version = '1.0.0'

description = """Giant common library"""

repositories { mavenCentral() }

sourceSets {
    main { resources { srcDir "src/main/profiles/${profile}" } }
}

dependencies {
    compile 'ch.qos.logback:logback-core:1.0.13'
    compile 'org.testng:testng:6.8.7'
    // ... other dependencies ...
}

checkstyle { ignoreFailures = true; sourceSets = [sourceSets.main] }
findbugs { ignoreFailures = true; sourceSets = [sourceSets.main] }

tasks.withType(Compile) { options.encoding = "UTF-8" }

test { useTestNG(); jacoco { excludes = ["org.*"] } }

Common Commands

gradle tasks

– list all available tasks. gradle build – assemble and test the project. gradle clean – delete the build directory. gradle properties – show project properties.

References

Gradle tutorial: Gradle入门教程

Gradle guide: Official Gradle guide

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBuild Automationdependency managementGradleKotlintask
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.