v9.6.1 Jun 26 · three weeks ago · Releases
Gradle
Gradle is a build tool for JVM languages like Java and Kotlin. It has built-in support across the JetBrains IDE suite.
Installing Gradle
Jetbrains IDEs Gradle built-in
| Java/JVM | Download | IntelliJ IDEA |
| C/C++ | Download | CLion |
| Android | Download | Android Studio |
Install alternatives
CLI
| Download | gradle/wrapper + gradlew + gradlew.bat | Portable CLI |
| Download | Gradle CLI | Legacy CLI |
IDE
| Download | VS Code + Extension Pack for Java | Gradle third-party |
| Download | Eclipse + Buildship Gradle Integration | Legacy IDE |
| Download | NetBeans | Legacy IDE |
Gradle Project Layout
- Project
- build — build directory configurable
- src/sourceSet — source directory configurable
- settings.gradle.kts — project script
- build.gradle.kts — build script
Gradle Wrapper
./gradlew |
Gradle Wrapper | project installation |
gradle |
Gradle | machine installation |
Wrapper Layout
- Project
- gradle
- wrapper
— bootstrap directory configurable
- gradle-wrapper.jar — downloader
- gradle-wrapper.properties — version config
- wrapper
— bootstrap directory configurable
- gradlew — executable for MacOS / Linux
- gradlew.bat — executable for Windows
- gradle
Gradle Docs — Understanding the Wrapper files
Wrapper CLI
Initialize Wrapper
gradle :wrapperGradle Docs — Adding the Gradle Wrapper
Update Wrapper
./gradlew :wrapper --gradle-version latestGradle Docs — Upgrading the Gradle Wrapper
Configure Wrapper
./gradlew :wrapper --gradle-version 9.0.0 --distribution-type bin./gradlew :wrapper --network-timeout 10000./gradlew :wrapper --retries 0./gradlew :wrapper --retry-back-off-ms 500./gradlew :wrapper --validate-url # or --no-validate-urlGradle Docs — Gradle Wrapper · Command Line Options
Wrapper properties
4 collapsed lines
distributionUrl=https\://services.gradle.org/distributions/gradle-9.0.0-bin.zip The URL to download the Gradle distribution from —
gradle-version-type.zip
-
version can be in the format 0.0.0 or one of:
-
type can be one of:
bin binary only, lightweight recommended
all binary + source
4 collapsed lines
Gradle Docs — Wrapper
gradle/gradle — gradle-wrapper.properties
Configure wrapper files
tasks { }} Projects
Develop Projects alongside Dependencies
Multi-project Layout
- Project
- build
- src
- settings.gradle.kts
- build.gradle.kts
- OtherProject
- build
- src
- settings.gradle.kts
- build.gradle.kts
Gradle Docs — Composite Build Layout
Project dependency
Add dependency
includeBuild("../OtherProject")dependencies { implementation("me.username:other-project:1.0.0")}OtherProject configuration
Define group:name:version
rootProject.name = "other-project"version = "1.0.0"group = "me.username" Modules
Share Scripts across modules
Modules Layout
- Project
- build
- src
- module
- build
- src
- build.gradle.kts
- settings.gradle.kts
- build.gradle.kts
Gradle Docs — Multi-Project Layout
Modules project scripts
Add module to Project
include("module")Add module as dependency
dependencies { implementation(project(":module"))} Source Sets
Utilize Source as Tasks
Default source sets Layout
- src
- main
- source
- resources
- test
- source
- resources
- main
Configure default source sets
4 collapsed lines
plugins { `java` Apply a JVM language plugin
}
sourceSets { JVM language plugins add sourceSets which can be configured here
Gradle Docs — Custom Sourcet Set paths SourcetSet
main { java.srcDirs("src/$name/java") The src / main / java directory can be configured
resources.srcDirs("src/$name/resources") resources.srcDirs("src/$name/resources") The src / main / resources directory can be configured
} test { java.srcDirs("src/$name/java") The src / test / java directory can be configured
resources.srcDirs("src/$name/resources") The src / test / resources directory can be configured
}} Gradle Docs — Custom Sourcet Set paths
Minimal source set Layout
- src
- source
- resources
4 collapsed lines
plugins { `java` Apply a JVM language plugin
}
sourceSets { main { java.srcDirs("src/java") resources.srcDirs("src/resources") Move main source set to src
} test { java.srcDirs() resources.srcDirs() Remove test source set directory
}}
tasks { testClasses { enabled = false Disable :testClasses task
}}Source sets Layout
- src
- sourceSet
- source
- resources
- sourceSet
Source set Tasks
-
:sourceSetClasses-
:processSourceSetResources— copies resources in build/resources/sourceSet -
:compileSourceSetSource— compiles source in build/classes/source/sourceSet
-
Creating source sets
4 collapsed lines
plugins { `java` Apply a JVM language plugin
}
sourceSets { val custom by registering {} sourceSets can be added
Gradle Docs — Defining new source sets SourcetSet
}
fun DependencyHandlerScope.customImplementation(it: Any) = add("customImplementation", it)fun DependencyHandlerScope.customCompileOnly(it: Any) = add("customCompileOnly", it)fun DependencyHandlerScope.customRuntimeOnly(it: Any) = add("customRuntimeOnly", it) Define Dependency configuration functions per sourceSet
dependencies { customImplementation("org.processing:core:4.0.0") Dependencies can be added per sourceSet
}
val SourceSetContainer.custom get() = sourceSets.named("custom").get() A sourceSet accessor can be defined
tasks { val customJar by registering(Jar::class) { A :sourceSetJar task can be defined
archiveClassifier = sourceSets.custom.name from(sourceSets.custom.output) }} Builds
Build Layout
- build
- classes / source / sourceSet
- package
- *.class
- package
- resources / sourceSet
- folder
- *
- folder
- libs
- *.jar
- tmp
- task
- *
- task
- classes / source / sourceSet
- src
- sourceSet / source
- package
- *.source
- package
- sourceSet / resources
- folder
- *
- folder
- sourceSet / source
Configure build directory
layout.buildDirectory = file("build")Gradle Scripts
Gradle DSLs
DSL · Domain Specific Language
| Kotlin DSL | Default Gradle DSL |
| Groovy DSL | Legacy Gradle DSL |
Gradle Script Classes
| settings.gradle.kts | Settings() |
|
| build.gradle.kts | Project() |
Settings script
Optional — allows multi-project layout
rootProject.name = "project-name" Usually same as directory name
6 collapsed lines
include( "subproject-name", "subproject-name:nested-name", ...) Include modules by directory name
includeBuild("../other-project-name") Include Projects by directory name
Build script
Required — included in every module
plugins {3 collapsed lines
`java` Core Plugins are applied by name
id("com.gradleup.shadow") version "0.0.0" ...}
repositories {repositories { Repositories to download dependencies from are listed here
4 collapsed lines
mavenCentral() Maven Central is the most widely used repository for JVM-based libraries
mavenLocal() Maven Local is the local respository on your machine
maven("https://jitpack.io") ...}
dependencies {3 collapsed lines
implementation("org.processing:core:4.0.0") Module dependencies are the most common dependencies, they refer to a module in a repository
implementation(project(":subproject-name")) Project dependencies allow to include subprojects as dependencies
implementation(files("libs/core.jar")) 7 collapsed lines
/** Hibernate ORM supporting PostgreSQL */ implementation("org.hibernate:hibernate-core:7.0.0") /** PostgreSQL implementation */ runtimeOnly("org.postgresql:postgresql:42.0.0") /** Lombok runs and generates code at build time */ compileOnly("org.projectlombok:lombok:1.0.0") ...}
tasks {16 collapsed lines
register("customTaskName") { group = "group name" description = "Describe the task" It is good practice to group and describe tasks
dependsOn("taskName") Task dependencies ensure they run before this task
Gradle Docs — Ordering tasks
println("Task starting...") ... }
println("Task finished") ... } }} Shared Scripts
| Cross-project configuration | build.gradle.kts | |
| Convention plugins | buildSrc · build-logic | Boilerplate |
Gradle Docs — Convention plugins vs cross-project configuration
Cross-project configuration
allprojects { Configure the module and all its descendants at once
- Recommended to only be used in the root Project
...}
...}Gradle Docs — Cross-project configuration
Convention plugins
| Multi-Project Build | buildSrc | |
| Composite build | build-logic |
Gradle Docs — Convention Plugins Multi-Project Build Structures
Multi-Project Build Layout
- Project
- buildSrc
— special module
- build
- src / main / kotlin
- *.gradle.kts — convention plugins
- build.gradle.kts
- buildSrc
— special module
Composite Build Layout
- Project
- build-logic
— project configurable
- build
- src / main / kotlin
- *.gradle.kts — convention plugins
- build.gradle.kts
- settings.gradle.kts
- build-logic
— project configurable
Gradle Docs — Using build-logic
pluginManagement { includeBuild("build-logic")}Gradle Docs — Composite Build · Plugins
Convention Plugin Build Scripts
plugins { `kotlin-dsl` Apply the Kotlin DSL plugin
- Allows to write Convention plugins
using the Gradle Kotlin DSL
}
repositories {}...plugins { id("shared-config") Apply the Convention plugin by name
}Plugins
Core Plugins
Community Plugins
JVM Languages
JVM · Java Virtual Machine
Java
Gradle Docs — Java Plugin Java
Java Build Script
plugins {}
java { toolchain { toolchain { Specifies the JDK/JRE used by this project
languageVersion = JavaLanguageVersion.of(26) vendor = null }
}
sourceSets { sourceSets { The Java plugin adds sourceSets which can be configured here
Gradle Docs — Custom Sourcet Set paths SourcetSet
//#collapse main { main { java { setSrcDirs(listOf("src/main/java")) The src / main / java directory can be configured
}
resources { setSrcDirs(listOf("src/main/resources")) The src / main / resources directory can be configured
} }
test { test { java { setSrcDirs(listOf("src/test/java")) The src / test / java directory can be configured
}
resources { setSrcDirs(listOf("src/test/resources")) The src / test / resources directory can be configured
} } //#endcollapse //#collapse create("sourceSetName") {
} //#endcollapse}
tasks { tasks {
jar { jar { The :jar task can be configured
- if present,
:javadocJar and :sourcesJar can also be configured
archiveBaseName.set(project.name) archiveVersion.set("${project.version}") archiveExtension.set("jar") archiveAppendix.set("") archiveClassifier.set("") The default jar naming format is:
[baseName]-[appendix]-[version]-[classifier].[extension]
archiveFileName.set("executable.jar") Alternatively, the jar can be renamed altogether
Gradle Docs — Archive File Name
manifest { attributes["Main-Class"] = "me.self.Main" attributes["Implementation-Title"] = "Cool Project Name" ... //! blur } }
withType<JavaCompile> { withType<JavaCompile> { Both JavaCompile tasks of main and test can be configured at once
Gradle Docs — Java Compile
options.apply { options.apply { Compile Options can be configured here
Gradle Docs — Compile Options
Oracle Docs — Java Compiler Options
release = java.toolchain.languageVersion.get().asInt() compilerArgs = listOf() debugOptions.debugLevel = "source,lines,vars" encoding = null isDebug = true isDeprecation = false isFailOnError = true isIncremental = true isFork = false } }
withType<ProcessResources> { withType<ProcessResources> { Both ProcessResources tasks of main and test can be configured at once
- Run
:clean when editing this task to ensure up to date output
includeEmptyDirs = true
filesMatching("config.yml") { filesMatching("config.yml") { expand( mapOf( "version" to project.version, "name" to project.name, ... //! blur ) ) }
... //! blur }
javadoc { options { options { Javadoc options can be configured here
Gradle Docs — Minimal Javadoc Options
Oracle Docs — Javadoc Options
windowTitle = null header = null locale = null encoding = null memberLevel = JavadocMemberLevel.PROTECTED outputLevel = JavadocOutputLevel.QUIET jFlags = listOf()
doclet = null docletpath = null } }
test { test { The :test task's test framework can be configured here
Gradle Docs — Testing in Java & JVM projects Test Task
} } Inherited Base Build Script
Gradle Docs — Java Plugin
Java Project Layout
- src
- main
- java
- resources
- test
- java
- resources
- main
Inherited Base Project Layout
Gradle Docs — Java Plugin · Project layout
Java Tasks
-
:clean— removes the build directory -
:javadoc— generates Javadoc in build/docs-
:classes···
-
-
:build-
:assemble-
:jar— assembles .jar in build/libs-
:classes-
:processResources— copies resources in build/resources -
:compileJava— compiles .java → .class in build/classes
-
-
-
-
:check-
:test— runs the unit tests-
:testClasses-
:processTestResources— copies resources in build/resources -
:compileTestJava— compiles .java → .class in build/classes-
:classes···
-
-
-
-
-
Optional Java Tasks
-
:assemble-
:sourcesJar— assembles -sources.jar in build/libs -
:javadocJar— assembles -javadoc.jar in build/libs-
:javadoc···
-
-
Kotlin
Gradle Plugins — Kotlin JVM Plugin Kotlin
Kotlin Build Script
//#collapseimport org.jetbrains.kotlin.gradle.tasks.KotlinCompile//#endcollapse
plugins { plugins { kotlin("jvm") version "2.0.0" The Gradle Kotlin DSL has built-in Kotlin support
}
repositories {}
kotlin { kotlin { jvmToolchain(26) Setting a toolchain via the kotlin {} extension updates the toolchain for java {} as well
Kotlin Docs — Java Toolchain Support
compilerOptions { Kotlin compiler options can be configured here
Kotlin Docs — Compiler options in KGP
Kotlin compiler options
freeCompilerArgs.addAll(listOf()) }}
sourceSets { sourceSets { The Kotlin plugin adds sourceSets which can be configured here
Kotlin Docs — Kotlin and Java sources SourcetSet
//#collapse main { main { kotlin { setSrcDirs(listOf("src/main/kotlin")) The src / main / kotlin directory can be configured
}
resources { setSrcDirs(listOf("src/main/resources")) The src / main / resources directory can be configured
} }
test { test { kotlin { setSrcDirs(listOf("src/test/kotlin")) The src / test / kotlin directory can be configured
}
resources { setSrcDirs(listOf("src/test/resources")) The src / test / resources directory can be configured
} } //#endcollapse}
tasks { tasks {
withType<KotlinCompile> { Both KotlinCompile tasks of main and test can be configured at once
- Requires
import
compilerOptions { freeCompilerArgs.addAll(listOf()) } }
} Inherited Java Build Script
Kotlin Docs — Gradle Plugin
Kotlin Project Layout
- src
- main
- kotlin
- resources
- test
- kotlin
- resources
- main
Inherited Java Project Layout
Kotlin Docs — Kotlin and Java sources
Kotlin Tasks
-
:classes-
:processResources -
:compileKotlin— compiles .kt → .class in build/classes-
:compileJava
-
-
-
:testClasses-
:processTestResources -
:compileTestKotlin— compiles .kt → .class in build/classes-
:compileTestJava
-
-
JVM Targets
Application
| Jar | .jar |
JVM |
| JPackage | Native · JVM bundled | |
| GraalVM Native | Native |
Jar
Builds an executable
.jar
plugins { `java` Apply a JVM language plugin
}
tasks { jar { manifest { } }}- Run
:jar - Open build / libs / .jar
Oracle Docs — JAR Manifest · Main Class
JPackage
Builds a native installer or portable executable with bundeled
.jar+ JVM
plugins { `java` Apply a JVM language plugin
id("org.panteleyev.jpackageplugin") version "2.0.0" Apply the JPackage plugin
}
repositories { gradlePluginPortal() Required to download Plugins
}
version = "0.0.0" A version is required by JPackage
tasks { dependsOn("build")
input = layout.buildDirectory.dir("libs") destination = layout.buildDirectory.dir("dist")
mainClass = "me.username.Main" mainJar = jar.get().archiveFileName.get()
type = ImageType.APP_IMAGE
winConsole = true }}- Windows — Install WiX Toolset v3
- Run
:jpackage - Open build / dist / .exe
GitHub — JPackage Gradle Plugin
GraalVM Native
Builds a native executable
plugins { `java` Apply a JVM language plugin
id("org.graalvm.buildtools.native") version "1.1.0" Apply the GraalVM Native plugin
}
binaries { named("main") { } }}- Download GraalVM JDK
- Unarchive GraalVM JDK and place in
~/.jdks - Set Environment Variable
GRAALVM_HOMEto location of GraalVM JDK - Run
:nativeCompile - Open build / native / nativeCompile / .exe
Library
Publish Dependency
plugins { `java` Apply a JVM language plugin
`signing` x
}
publishing { publishing { The Maven publish plugin can be configured here
publications { publications { Maven publications are listed here
register<MavenPublication>("maven") { register<MavenPublication>("maven") { Creating a Maven publication will generate tasks from its given name for each repository
Gradle Docs — Maven Publication
groupId = "${project.group}" artifactId = "${project.name}"
pom { name = project.name description = project.description url = "https://www.username.me/project" packaging = "jar" inceptionYear = "2026"
licenses { license { name = "MIT" url = "https://opensource.org/license/MIT" } } developers { developer { id = "username" name = "Full Name" email = "contact@username.me" url = "https://username.me" organization = "Organization" organizationUrl = "https://organization.com" roles = listOf("developer") timezone = "UTC" properties = mapOf() } } contributors { contributor { name = "Full Name" email = "contact@contributor.me" url = "https://username.me" organization = "Organization" organizationUrl = "https://organization.com" roles = listOf("developer") timezone = "UTC" properties = mapOf() } } organization { name = "Organization" url = "https://organization.com" } scm { // source control management connection = "scm:git:https://github.com/OWNER/REPO.git" developerConnection = "scm:git:ssh://github.com/OWNER/REPO.git" url = "https://github.com/OWNER/REPO" tag = "v${version}" } issueManagement { system = "GitHub" url = "https://github.com/OWNER/REPO/issues" } ciManagement { system = "GitHub Actions" url = "https://github.com/OWNER/REPO/actions" } distributionManagement { downloadUrl = "https://github.com/OWNER/REPO/releases" groupId = "${project.group}".replaceFirst("me.", "io.") artifactId = "${project.name}" version = "${project.version}" message = "Artifact moved to $groupId:$artifactId" } }
mailingLists { mailingList { name = "Development" subscribe = "dev-subscribe@username.me" unsubscribe = "dev-unsubscribe@username.me" post = "dev@username.me" archive = "https://username.me/archive/dev" otherArchives = setOf() } }
with(asNode()) { //! blur appendNode("name", mapOf("key" to "value"), "value") //! blur .appendNode("child-name", mapOf("key" to "value"), "child-value") } //! blur } }
artifacts.removeIf { artifact -> artifact.classifier in setOf("sources", "javadoc") classifier = "artifact-name" extension = "zip" builtBy(tasks["taskName"]) The task used to produce this atifact can be referenced
} artifact(tasks["taskName"]) atifacts can also be included by task output
} }
repositories {
maven { name = "GitHubPackages" url = uri("https://maven.pkg.github.com/OWNER/REPOSITORY") credentials { username = System.getenv("GITHUB_ACTOR") password = System.getenv("GITHUB_TOKEN") } }
maven { name = "Nexus" url = uri("http://nexus.username.me/repository/repository-name") credentials { username = System.getenv("NEXUS_USER") password = System.getenv("NEXUS_PASS") } }
maven { name = "Artifactory" url = uri("http://artifactory.username.me/artifactory/libs-snapshot-local") credentials { username = System.getenv("ARTIFACTORY_USER") password = System.getenv("ARTIFACTORY_PASS") } } } }
signing { sign(publishing.publications["maven"])}Settings
Version Catalog
Centralized config for dependencies
Version Catalog Layout
- Project
- gradle
- libs.versions.toml — version catalog configurable
- gradle
Version Catalog Config
[versions] kotlin = "2.0.0"processing = "4.0.0"processing-net = "3.0.0"...
[libraries] [libraries] Dependencies with their referenced versions are listed here
Gradle Docs — Version Catalog · Libraries
processing-core = { module = "org.processing:core", version.ref = "processing" }processing-net = { module = "org.processing:net", version.ref = "processing-net" }...
[bundles] processing = [ "processing-core", "processing-net" ]...
[plugins] shadow = { id = "com.gradleup.shadow", version = "9.0.0" }... Referencing Version Catalog
plugins { plugins {
kotlin("jvm") version libs.versions.kotlin Versions can be referenced directly in useful cases
alias(libs.plugins.shadow) Plugins can be referenced
}
dependencies { dependencies {
implementation(libs.bundles.processing) Bundles add many dependencies at once:
org.processing:core:4.0.0
org.processing:net:3.0.0
} Properties
Centralized config for properties
Properties Layout
- Project
- gradle.properties — properties
Properties Config
key=value Project properties can be defined here
- They are accessible at Build time through Scripts
- They can be defined via CLI arguments prefixed with
-P
- They can be defined via Environment variables prefixed with
ORG_GRADLE_PROJECT_
systemProp.key=value System properties with namespace systemProp can be defined here
- They are accessible at Runtime through Scripts and Source
- They can be defined via CLI arguments prefixed with
-D
- They can be defined via Environment variables with
GRADLE_OPTS
28 collapsed lines
org.gradle.caching=falseorg.gradle.caching.debug=falseorg.gradle.configuration-cache=falseorg.gradle.configureondemand=false# org.gradle.console=org.gradle.console.interactive=trueorg.gradle.continue=falseorg.gradle.daemon=trueorg.gradle.daemon.idletimeout=10800000org.gradle.debug=false# org.gradle.java.home=org.gradle.java.installations.auto-detect=trueorg.gradle.java.installations.auto-download=trueorg.gradle.java.installations.paths=org.gradle.java.installations.fromEnv=org.gradle.jvmargs=-Xmx512m "-XX:MaxMetaspaceSize=384m"org.gradle.logging.level=lifecycleorg.gradle.parallel=falseorg.gradle.priority=normalorg.gradle.projectcachedir=.gradleorg.gradle.problems.report=true# org.gradle.tooling.parallel=org.gradle.unsafe.isolated-projects=falseorg.gradle.unsafe.isolated-projects.diagnostics=falseorg.gradle.vfs.verbose=falseorg.gradle.vfs.watch=trueorg.gradle.warning.mode=summary# org.gradle.workers.max= Gradle properties with namespace org.gradle can be configured here
- They are System properties
20 collapsed lines
# --- Gradle System Properties ---# systemProp.gradle.wrapperUser=# systemProp.gradle.wrapperPassword=# systemProp.gradle.user.home=# systemProp.https.protocols=# --- Java System Properties ---# systemProp.file.separator=# systemProp.java.class.path=# systemProp.java.home=# systemProp.java.vendor=# systemProp.java.vendor.url=# systemProp.java.version=# systemProp.line.separator=# systemProp.os.arch=# systemProp.os.name=# systemProp.os.version=# systemProp.path.separator=# systemProp.user.dir=# systemProp.user.home=# systemProp.user.name= System properties with namespace systemProp can be configured here
Gradle Docs — System properties reference
Oracle Docs — System properties
Gradle Docs — gradle.properties
Properties Priority
| CLI arguments | -D -P |
||
| gradle.properties | ~/.gradle Project module | ||
| Environment variables | GRADLE_OPTS ORG_GRADLE_PROJECT_* |
Gradle Docs — Priority for configurations
Project properties
Gradle Docs — Accessing a project property
System properties
Oracle Docs — Reading System Properties