SyntaxStudy
Sign Up
Java Gradle Basics and build.gradle
Java Beginner 1 min read

Gradle Basics and build.gradle

Gradle is a modern, flexible build tool that uses a Groovy or Kotlin DSL instead of XML, resulting in more concise and readable build scripts. It combines the best of Maven (convention over configuration, dependency management via Maven Central) and Ant (programmatic flexibility). Gradle uses an incremental build model — it tracks the inputs and outputs of each task and only re-runs tasks whose inputs have changed, making it significantly faster than Maven for large projects. A Gradle build is composed of projects and tasks. The build.gradle file declares the project configuration: plugins (java, application, spring-boot, etc.), repositories (mavenCentral(), google()), and dependencies organised by configuration (implementation, testImplementation, compileOnly, runtimeOnly). The java plugin adds standard tasks like compileJava, processResources, test, and jar to the project automatically. The Gradle wrapper (gradlew / gradlew.bat) is a script included in every Gradle project that downloads and uses a specific version of Gradle, ensuring all developers and CI servers use the same build tool version without requiring a global installation. Running ./gradlew tasks lists all available tasks. The settings.gradle file defines the root project name and, for multi-project builds, declares all subprojects with include(":module-name").
Example
// build.gradle (Groovy DSL)

plugins {
    id 'java'
    id 'application'
}

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

java {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
}

repositories {
    mavenCentral()
}

dependencies {
    // implementation: available at compile + runtime, NOT exposed to consumers
    implementation 'com.google.guava:guava:32.1.2-jre'

    // compileOnly: only at compile time (e.g. Lombok, annotation processors)
    compileOnly 'org.projectlombok:lombok:1.18.30'
    annotationProcessor 'org.projectlombok:lombok:1.18.30'

    // testImplementation: only in test source set
    testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

application {
    mainClass = 'com.example.Main'
}

test {
    useJUnitPlatform() // required for JUnit 5
}

// Custom task
tasks.register('printVersion') {
    doLast {
        println "Building version: $version"
    }
}