← 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
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		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
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		String buildScript = "plugins { id 'java' }\nrepositories { mavenCentral() }\ndependencies { implementation 'com.example:lib:1.0' }";
		// Print a line to the console
		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
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		String groovy = "build.gradle (Groovy, dynamically typed)";
		String kotlin = "build.gradle.kts (Kotlin, statically typed)";
		// Print a line to the console
		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
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println("Both compile/test/package; Gradle's caching tends to be faster");
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using the compile configuration in a build file, which was removed in newer Gradle versions; use implementation or api.
  2. Copying Groovy syntax into a Kotlin DSL file, which needs parentheses and double quotes such as implementation("group:name:1.0").
  3. Running the installed gradle instead of the project's ./gradlew wrapper, which can use a different Gradle version.
🔒

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.