Create Your First Gradle Project
Introduction
This chapter creates a real Gradle Java application two ways: gradle init (fast template) and a manual minimal layout (deep understanding). You will learn what each file does, run ./gradlew build, and execute a Hello World with the application plugin.
Prerequisites
- JDK 21
- Gradle on PATH or use Wrapper after generation (installation chapters)
- Optional: IntelliJ IDEA for import
Standard Gradle Java Layout
Same src convention as Maven—Gradle adopted it for compatibility.
Method A: gradle init (Recommended First)
Pick an empty parent folder:
mkdir ~/projects
cd ~/projects
gradle initAnswer the wizard (typical learning choices):
| Prompt | Suggested answer |
|---|---|
| Type | application |
| Implementation language | Java |
| Build script DSL | Groovy (Kotlin .kts is fine too) |
| Test framework | JUnit Jupiter |
| Project name | hello-gradle |
| Java version | 21 |
| Generate Wrapper | yes |
After init:
cd hello-gradle
./gradlew tasks
./gradlew build
./gradlew runWindows:
gradlew.bat build
gradlew.bat runYou should see BUILD SUCCESSFUL and application output in the console.
Key Files After init
settings.gradle
// Root project name shown in IDEA and build output
rootProject.name = 'hello-gradle'Multi-module projects add include 'submodule' here.
build.gradle (simplified example)
| Block | Purpose |
|---|---|
plugins | Enables Java compile, test, run tasks |
group / version | Project coordinates (like Maven GAV) |
java.toolchain | Compile with JDK 21 |
repositories | Where to download libraries |
dependencies | Libraries and scopes (implementation, testImplementation) |
application.mainClass | Entry point for gradlew run |
gradle/wrapper/gradle-wrapper.properties
Pins Wrapper version:
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12.1-bin.zipCommit Wrapper files so teammates need only JDK 21.
gradle.properties (optional)
org.gradle.jvmargs=-Xmx2g -Dfile.encoding=UTF-8
org.gradle.parallel=trueMethod B: Manual Minimal Project
Create structure by hand:
mkdir -p manual-gradle/src/main/java/com/example
mkdir -p manual-gradle/src/test/java/com/example
cd manual-gradlesettings.gradle:
rootProject.name = 'manual-gradle'build.gradle:
src/main/java/com/example/HelloApp.java:
package com.example;
public class HelloApp {
public static void main(String[] args) {
System.out.println("Hello from Gradle!");
}
}Generate Wrapper (needs global gradle once):
gradle wrapper --gradle-version 8.12.1
./gradlew build
./gradlew runCommon Gradle Tasks
| Task | What it does |
|---|---|
clean | Deletes build/ |
compileJava | Compiles main sources |
test | Runs JUnit tests |
build | check + assemble (compile, test, jar) |
run | Runs application main class |
jar | Packages compiled classes |
./gradlew clean buildOutputs land under build/libs/ (JAR name includes project version).
Import into IntelliJ
- Open the folder containing
settings.gradle - Wait for Gradle sync
- Run
HelloAppfrom the gutter or Gradle → application → run
See IntelliJ and Gradle for JVM and distribution settings.
Compare with Maven Quickstart
| Step | Maven | Gradle |
|---|---|---|
| Scaffold | mvn archetype:generate | gradle init |
| Config file | pom.xml | build.gradle |
| Build | mvn package | ./gradlew build |
| Run | java -jar or exec plugin | ./gradlew run |
Mini Exercise: Change the Greeting
- Edit the
mainmethod message - Run
./gradlew run - Add a failing test, run
./gradlew test, fix it, run again
FAQ
build/ folder — commit to git?
No. Add build/ to .gitignore. Commit sources and Wrapper only.
Groovy vs Kotlin DSL?
Same concepts. Groovy build.gradle is common in Java samples; build.gradle.kts uses Kotlin syntax—popular in Kotlin projects.
gradlew: Permission denied on macOS/Linux?
Run chmod +x gradlew.
Init failed to pick Java 21?
Edit java { toolchain { languageVersion = JavaLanguageVersion.of(21) } } } manually.
What comes next?
Gradle Wrapper — deep dive into gradlew and upgrading versions.