← Back to Advanced Java Course | Chapter 12: Build Tools | Lesson 3 of 5

Java Gradle Basics

Gradle is a modern build automation tool that uses a programmable build script -- in Groovy or Kotlin DSL -- instead of Maven's fixed XML format, offering more flexibility and generally faster builds.

What is Gradle?

Gradle is a modern build automation tool for Java (and other languages) that uses a programmable build script instead of a fixed XML format, offering more flexibility and generally faster incremental builds than Maven.

Example: What is Gradle?

java
public class Main {
	public static void main(String[] args) {
		System.out.println("Gradle uses a programmable build script instead of fixed XML");
	}
}

The build.gradle File

A Gradle project is configured through a build.gradle (or build.gradle.kts) file, which applies plugins like java to add standard tasks, declares repositories to search for dependencies, and lists the project's dependencies.

Example: The build.gradle File

java
public class Main {
	public static void main(String[] args) {
		String buildScript = "plugins { id 'java' }\nrepositories { mavenCentral() }\ndependencies { implementation 'com.example:lib:1.0' }";
		System.out.println(buildScript);
	}
}

Gradle Tasks

Gradle organizes work into tasks, such as compileJava, test, and build, and tasks can depend on each other so that running one automatically triggers the tasks it needs first, similar to Maven's lifecycle phases.

Example: Gradle Tasks

java
public class Main {
	public static void main(String[] args) {
		String[] tasks = {"compileJava", "test", "build"}; // build depends on test, which depends on compileJava
		for (String t : tasks) System.out.println(t);
	}
}

Kotlin DSL vs Groovy DSL

Gradle build scripts can be written in Groovy DSL (build.gradle), which is dynamically typed and concise, or Kotlin DSL (build.gradle.kts), which is statically typed and gives stronger IDE support and compile-time checking.

Example: Kotlin DSL vs Groovy DSL

java
public class Main {
	public static void main(String[] args) {
		String groovy = "build.gradle (Groovy, dynamically typed)";
		String kotlin = "build.gradle.kts (Kotlin, statically typed)";
		System.out.println(groovy + " vs " + kotlin);
	}
}

Gradle vs Maven

Gradle and Maven solve the same core problem -- compiling, testing, and packaging a project along with its dependencies -- but Gradle's script-based configuration is generally more flexible and its incremental build caching tends to be faster.

Example: Gradle vs Maven

java
public class Main {
	public static void main(String[] args) {
		System.out.println("Both compile/test/package; Gradle's caching tends to be faster");
	}
}
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.