Sub topic 9
Your First Gradle Project and build.gradle.kts
You already know the standard project layout and how a Maven pom.xml describes a project. Gradle is another build tool for Java. A build tool is a program that compiles, tests, and packages your code for you. Gradle uses the same folder layout as Maven, so the Java files from earlier chapters work without changes. What changes is the file that describes the build.
In this chapter you will create a small Gradle project by hand, read its build script line by line, and run it.
What Makes Gradle Different
Maven describes a project with XML, a format made of tags like <version>. Gradle describes a project with a build script. A build script is a small program that Gradle runs to learn about your project.
Gradle build scripts can be written in two languages:
| Script file | Language | Notes |
|---|---|---|
| `build.gradle` | Groovy DSL | The older style. You will see it in many existing projects. |
| `build.gradle.kts` | Kotlin DSL | The default for new Gradle projects. Editors can check it and autocomplete it. |
A DSL (domain-specific language) is a small language made for one job. Here the job is describing builds. This tutorial uses the Kotlin DSL. You do not need to know Kotlin. The script reads almost like a settings list.
The Files in a Gradle Project
A single-project Gradle build needs two files at the top level, next to the src folder:
settings.gradle.ktsis the settings file. It names the build and lists which projects belong to it.build.gradle.ktsis the build script. It says how to build the project.
Here is the full layout you will create:
hello/
├── settings.gradle.kts
├── build.gradle.kts
└── src/
├── main/java/com/example/App.java
└── test/java/com/example/AppTest.javaStep 1: Create the Folders and Java Code
Make the folders:
mkdir -p hello/src/main/java/com/example
mkdir -p hello/src/test/java/com/example
cd helloSave this class as src/main/java/com/example/App.java:
package com.example;
public class App {
public static void main(String[] args) {
System.out.println(greeting("World"));
}
static String greeting(String name) {
return "Hello, " + name + "!";
}
}Save this test as src/test/java/com/example/AppTest.java:
package com.example;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class AppTest {
@Test
void greetsByName() {
assertEquals("Hello, Maven!", App.greeting("Maven"));
}
}Step 2: Write the Settings File
Create settings.gradle.kts with one line:
rootProject.name = "hello"The root project is the top-level project of the build. Its name becomes part of the JAR file name. It works like Maven's artifactId. If you leave this file out, Gradle uses the folder name. A later chapter adds more projects to this file.
Step 3: Write the Build Script
Create build.gradle.kts:
plugins {
application
}
repositories {
mavenCentral()
}
dependencies {
implementation("com.google.guava:guava:33.2.1-jre")
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
application {
mainClass = "com.example.App"
}
tasks.named<Test>("test") {
useJUnitPlatform()
}Each name { ... } part is a block. A block groups related settings. The sections below explain them one at a time.
The plugins Block
A plugin adds abilities to Gradle. On its own, Gradle knows nothing about Java. The application plugin teaches Gradle to compile Java, run tests, build a JAR, and run your program. It also turns on the java plugin for you. Without a plugin, the build script has almost nothing to do.
The repositories Block
A repository is a server that stores libraries. mavenCentral() tells Gradle to download libraries from Maven Central, the same place Maven uses. Gradle keeps the downloaded files in its own cache folder, ~/.gradle/caches, so it downloads each file only once.
The dependencies Block
A dependency is a library your code needs. Gradle writes each one as a single string, "group:name:version". These are the same coordinates as Maven's groupId, artifactId, and version, joined with colons:
| Maven tag | Gradle string part | Example |
|---|---|---|
| `groupId` | before the first colon | `org.junit.jupiter` |
| `artifactId` | middle | `junit-jupiter` |
| `version` | after the last colon | `5.10.2` |
The word in front of each string, such as implementation or testImplementation, says where the library is needed. implementation means the main code uses it. testImplementation means only tests use it. These words do a job like Maven's scopes. The chapter on dependency configurations explains them in detail.
The JUnit launcher line has no version. Gradle picks a version that matches the JUnit library already listed.
The java Block and Toolchains
A toolchain is the JDK that Gradle uses to compile and run your code. Here the script asks for Java 17. If the JDK that runs Gradle is a different version, Gradle looks for a Java 17 JDK on your machine and uses that one. This keeps the build the same on every computer. It is like maven.compiler.release in a POM, but stronger, because it picks the actual JDK.
The application Block
mainClass names the class that holds the main method. Use the full name, with the package. Gradle needs it to run the program.
The test Task Setting
The last block sets up the task that runs tests. useJUnitPlatform() tells Gradle to use the JUnit 5 test engine. If you forget this line, Gradle finds no tests to run and still reports success. That is a common beginner mistake.
Step 4: Build and Run
From the hello folder, run:
gradle build
gradle rungradle build compiles the code, runs the tests, and packages a JAR. The first run is slow because Gradle downloads libraries and starts a background helper called the Gradle Daemon. Later runs are faster. gradle run prints:
Hello, World!The output goes into a build folder:
| Path | What it holds |
|---|---|
| `build/classes/java/main` | Compiled `.class` files |
| `build/libs/hello.jar` | The packaged JAR |
| `build/reports/tests/test/index.html` | A web page showing test results |
| `build/distributions` | ZIP and TAR files with start scripts, made by the `application` plugin |
Common Mistakes
- Wrong file name. The file must be
build.gradle.kts. If it is namedbuild.gradle, Gradle reads it as Groovy, and the Kotlin syntax fails. - Single quotes. Kotlin strings use double quotes.
'com.example.App'is an error in the Kotlin DSL. - Missing
useJUnitPlatform(). Tests are silently skipped. - Wrong
mainClass.gradle runfails with "Could not find or load main class". Check the package name. - Files in the wrong folder. Gradle only looks in
src/main/javaandsrc/test/javaby default.
Exercise
Your build.gradle.kts lists JUnit with testImplementation, but gradle build passes even though one test should fail. What line is most likely missing, and where does it go?
Show answer
The line useJUnitPlatform() is missing. It goes inside the tasks.named<Test>("test") { ... } block. Without it, Gradle does not use the JUnit 5 engine, finds no tests, and reports success.
Exercise
Write the Gradle dependency line for the Maven dependency with groupId com.google.code.gson, artifactId gson, and version 2.11.0, used by the main code.
Show answer
implementation("com.google.code.gson:gson:2.11.0")
Exercise
You change rootProject.name in settings.gradle.kts from "hello" to "greeter" and run gradle build. What is the new JAR file called, and in which folder is it?
Show answer
The JAR is build/libs/greeter.jar. The root project name sets the JAR's base name.