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.

Build tool alternatives

Maven Legacy
Ant Legacy

Gradle Docs — Gradle vs Maven

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

Gradle Docs — Installation

IDE

Download VS Code + Extension Pack for Java Gradle third-party
Download Eclipse + Buildship Gradle Integration Legacy IDE
Download NetBeans Legacy IDE

Gradle Docs — Supported IDEs

Gradle Project Layout

Gradle Wrapper

./gradlew Gradle Wrapper project installation
gradle Gradle machine installation

Gradle Docs — Gradle Wrapper

Wrapper Layout

Gradle Docs — Understanding the Wrapper files

Wrapper CLI

Initialize Wrapper
Initialize Wrapper
gradle :wrapper

Gradle Docs — Adding the Gradle Wrapper

Update Wrapper
Update Wrapper
./gradlew :wrapper --gradle-version latest

Gradle Docs — Upgrading the Gradle Wrapper

Configure Wrapper
Switch Wrapper Version
./gradlew :wrapper --gradle-version 9.0.0 --distribution-type bin
Edit Wrapper Parameters
./gradlew :wrapper --network-timeout 10000
./gradlew :wrapper --retries 0
./gradlew :wrapper --retry-back-off-ms 500
./gradlew :wrapper --validate-url # or --no-validate-url

Gradle Docs — Gradle Wrapper · Command Line Options

Wrapper properties

gradle-wrapper.properties
4 collapsed lines
distributionBase=GRADLE_USER_HOME

The home of where the unpacked Gradle distribution should be stored

  • GRADLE_USER_HOME at ~/.gradle
  • PROJECT at .
distributionPath=wrapper/dists

Relative path from distributionBase where the unpacked Gradle distribution is stored

zipStoreBase=GRADLE_USER_HOME

The home of where the archived Gradle distribution should be stored

  • GRADLE_USER_HOME at ~/.gradle
  • PROJECT at .
zipStorePath=wrapper/dists

Relative path from zipStoreBase where the archived Gradle distribution is stored

distributionUrl=https\://services.gradle.org/distributions/gradle-9.0.0-bin.zip

The URL to download the Gradle distribution from — gradle-version-type.zip

Gradle Docs — Distribution URL Version Type

4 collapsed lines
networkTimeout=10000

The network timeout to use during the Gradle distribution download, in ms

retries=0

The number of retries to attempt the Gradle distribution download

retryBackOffMs=500

The delay to wait between Gradle distribution download retries, in ms

validateDistributionUrl=true

Enables the validation of the Gradle distribution URL

Gradle Docs — Wrapper
gradle/gradle — gradle-wrapper.properties

Configure wrapper files

build.gradle.kts
tasks {
wrapper {

The Wrapper task can be configured here
Gradle Docs — Wrapper Task

jarFile = file("gradle/wrapper/gradle-wrapper.jar")

The wrapper directory can be configured

scriptFile = file("gradlew")

The gradlew scripts can be configured

}
}

Gradle Docs — Wrapper Task

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

settings.gradle.kts (Project)
includeBuild("../OtherProject")
build.gradle.kts (Project)
dependencies {
implementation("me.username:other-project:1.0.0")
}

OtherProject configuration

Define group:name:version

settings.gradle.kts (OtherProject)
rootProject.name = "other-project"
build.gradle.kts (OtherProject)
version = "1.0.0"
group = "me.username"

Gradle Docs — Composite Builds

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

settings.gradle.kts
include("module")

Add module as dependency

build.gradle.kts
dependencies {
implementation(project(":module"))
}

Gradle Docs — Multi-Project Builds

Source Sets

Utilize Source as Tasks

Default source sets Layout

  • src
    • main
      • source
      • resources
    • test
      • source
      • resources

Configure default source sets

build.gradle.kts
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
build.gradle.kts
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

Source set Tasks

Gradle Docs — SourceSet Tasks

Creating source sets

build.gradle.kts
4 collapsed lines
plugins {
`java`

Apply a JVM language plugin

}
sourceSets {
val custom by registering {}
}
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)
}
}

Gradle Docs — Defining new source sets

Builds

Build Layout

  • build
    • classes / source / sourceSet
      • package
        • *.class
    • resources / sourceSet
      • folder
        • *
    • libs
      • *.jar
    • tmp
      • task
        • *






  • src
    • sourceSet / source
      • package
        • *.source
    • sourceSet / resources
      • folder
        • *

Configure build directory

build.gradle.kts
layout.buildDirectory = file("build")

The build directory can be customized

Gradle Docs — Build Directory

Gradle Scripts

Gradle DSLs

DSL · Domain Specific Language

Kotlin DSL Default Gradle DSL
Groovy DSL Legacy Gradle DSL

Gradle Docs — Best Practices · Use Kotlin DSL

Gradle Script Classes

settings.gradle.kts Settings()
build.gradle.kts Project()

Gradle Docs — Anatomy of a Build Script

Settings script

Optional — allows multi-project layout

settings.gradle.kts
rootProject.name = "project-name"

Usually same as directory name

  • Use kebab-case
  • Defaults to directory name

Gradle Docs — Naming recommendations

6 collapsed lines
include(
"subproject-name",
"subproject-name:nested-name",
...
)

Include modules by directory name

  • Sub project paths must use : insead of / (path:to:subproject)

Gradle Docs — Settings · include

includeBuild("../other-project-name")

Include Projects by directory name

  • Path can be relative or absolute

Gradle Docs — Settings · include build

Build script

Required — included in every module

build.gradle.kts
plugins {
plugins {

Plugins are listed here

Gradle Docs — Plugins Plugin Block

3 collapsed lines
`java`

Core Plugins are applied by name

  • `java`
  • `groovy`
  • `scala`
  • `antlr`
  • `application`
  • `maven-publish`

Gradle Docs — Core Plugins

id("com.gradleup.shadow") version "0.0.0"

Third-party plugins are applied with id(...) and version

Gradle Plugin Portal

...
}
repositories {
repositories {

Repositories to download dependencies from are listed here

Gradle Docs — Repository Types Repository Block

4 collapsed lines
mavenCentral()

Maven Central is the most widely used repository for JVM-based libraries

  • mavenCentral() is a shortcut for maven("https://repo.maven.apache.org/maven2")

Maven Central
Gradle Docs — Maven Central

mavenLocal()

Maven Local is the local respository on your machine

  • By default it is located at ~/.m2/repository

Gradle Docs — Local Maven Repository Maven Local

maven("https://jitpack.io")

Other repositories can be referenced with maven(...)

Gradle Docs — maven

...
}
dependencies {
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

  • The format of the dependency notation is group:name:version

Gradle Docs — Module dependencies

implementation(project(":subproject-name"))

Project dependencies allow to include subprojects as dependencies

  • project() or project(":") includes the root project as a dependency

Gradle Docs — Project dependencies Project method

implementation(files("libs/core.jar"))

File dependencies allow to include local jars as dependencies

Gradle Docs — File dependencies

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")

Dependency configurations are specified for each dependency:

  • implementation(...) included default
  • compileOnly(...) included only at build time useful for annotation processors
  • runtimeOnly(...) included only at runtime useful for driver/engine implementations

Gradle Docs — Dependency Configurations

...
}
tasks {
tasks {

Tasks are configured here

Gradle Docs — Tasks Task Block

3 collapsed lines
named("taskName") {

Configuring Tasks can be done with named(...)

  • Task inputs and outputs can be configured here

Gradle Docs — Built-in Task Types

enabled = false
}
16 collapsed lines
register("customTaskName") {

Creating Tasks can be done with register(...)

  • Use camelCase for task names

Gradle Docs — Task

group = "group name"
description = "Describe the task"

It is good practice to group and describe tasks

  • group the category of the task (lowercase)
  • description the description of the task

Gradle Docs — Task group and description

dependsOn("taskName")

Task dependencies ensure they run before this task
Gradle Docs — Ordering tasks

finalizedBy("taskName")

Runs a task after this task
Gradle Docs — Finalizer tasks

doFirst {

Runs at the beginning of the task

println("Task starting...")
...
}
doLast {

Runs at the end of the task

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

build.gradle.kts (Project)
allprojects {

Configure the module and all its descendants at once

  • Recommended to only be used in the root Project

Gradle Docs — allprojects

...
}
subprojects {

Configure all descendant modules at once
Gradle Docs — subprojects

...
}

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
      • build.gradle.kts

Gradle Docs — Using buildSrc

Composite Build Layout
  • Project
    • build-logic project configurable
      • build
      • src / main / kotlin
      • build.gradle.kts
      • settings.gradle.kts

Gradle Docs — Using build-logic

settings.gradle.kts (Project)
pluginManagement {
includeBuild("build-logic")
}

Gradle Docs — Composite Build · Plugins

Convention Plugin Build Scripts
build.gradle.kts (:buildSrc / :build-logic)
plugins {
`kotlin-dsl`

Apply the Kotlin DSL plugin

  • Allows to write Convention plugins using the Gradle Kotlin DSL

Gradle Docs — Kotlin DSL

}
repositories {
gradlePluginPortal()

Required to download the Kotlin Standard Library
Kotlin Docs — kotlin-stdlib

}
main/kotlin/shared-config.gradle.kts
...
build.gradle.kts (Project)
plugins {
id("shared-config")

Apply the Convention plugin by name

}

Gradle Docs — Sharing logic via convention plugins

Plugins

Core Plugins

Gradle Docs — Core Plugins

Community Plugins

Gradle Plugin Portal

JVM Languages

JVM · Java Virtual Machine

Java

Gradle Docs — Java Plugin Java

Java Build Script

build.gradle.kts
plugins {
java

Apply the Java plugin
Gradle Docs — Java Plugin

}
java {
java {

The Java Plugin can be configured here
Gradle Docs — Java Plugin Extension

toolchain {
toolchain {

Specifies the JDK/JRE used by this project

  • This replaces legacy sourceCompatibility and targetCompatibility

Gradle Docs — JVM Toolchain Java Toolchain Spec

languageVersion = JavaLanguageVersion.of(26)
vendor = null
}
withJavadocJar()
withSourcesJar()
}
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

Gradle Docs — Jar Task

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]

Gradle Docs — Archive File Name

archiveFileName.set("executable.jar")

Alternatively, the jar can be renamed altogether
Gradle Docs — Archive File Name

manifest {
manifest {

The jar's manifest file can be configured here

  • The manifest file is located at META-INF/MANIFEST.MF
  • Alternatively, the manifest file could be copied from resources

Gradle Docs — Modifying the JAR manifest 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 {
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

Gradle Docs — Process Resources Copy

includeEmptyDirs = true
rename("manifest.txt", "META-INF/MANIFEST.MF")

files can be renamed

exclude("transient")

files can be excluded

filesMatching("config.yml") {
filesMatching("config.yml") {
expand(
expand(

files can have template properties ${...}
Gradle Docs — Expand

mapOf(
"version" to project.version,
"name" to project.name,
... //! blur
)
)
}
... //! blur
}
javadoc {
javadoc {
options {
options {
windowTitle = null
header = null
locale = null
encoding = null
memberLevel = JavadocMemberLevel.PROTECTED
outputLevel = JavadocOutputLevel.QUIET
jFlags = listOf()
doclet = null
docletpath = null

A custom docklet engine can be used

  • The default is StandardDoclet

Oracle Docs — Docklet API

}
}
test {
test {

The :test task's test framework can be configured here
Gradle Docs — Testing in Java & JVM projects Test Task

useJUnitPlatform()

Use the JUnit test framework recommended

  • useJUnit() is legacy for ≤v4
useTestNG()

Alternatively, you can use the TestNG test framework

}
}

Inherited Base Build Script
Gradle Docs — Java Plugin

Java Project Layout

  • src
    • main
      • java
      • resources
    • test
      • java
      • resources

Inherited Base Project Layout
Gradle Docs — Java Plugin · Project layout

Java Tasks

Optional Java Tasks
  • :assemble
    • :sourcesJar — assembles -sources.jar in build/libs
    • :javadocJar — assembles -javadoc.jar in build/libs
      • :javadoc ···

Inherited Base Tasks
Gradle Docs — Java Plugin · Tasks

Kotlin

Gradle Plugins — Kotlin JVM Plugin Kotlin

Kotlin Build Script

build.gradle.kts
//#collapse
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
//#endcollapse
plugins {
plugins {
kotlin("jvm") version "2.0.0"

The Gradle Kotlin DSL has built-in Kotlin support

  • kotlin(...) is a shortcut for id('org.jetbrains.kotlin.$module')

Kotlin Docs — Gradle Plugin releases

}
repositories {
mavenCentral()

Required to download the Kotlin Standard Library
Kotlin Docs — kotlin-stdlib

}
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

Gradle Docs — Compiler options in KGP

compilerOptions {
freeCompilerArgs.addAll(listOf())
}
}
}

Inherited Java Build Script
Kotlin Docs — Gradle Plugin

Kotlin Project Layout

  • src
    • main
      • kotlin
      • resources
    • test
      • kotlin
      • resources

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

Inherited Java Tasks
Kotlin Docs — Gradle Tasks

Groovy

Gradle Docs — Groovy Plugin Groovy

Scala

Gradle Docs — Scala Plugin Scala

ANTLR

Gradle Docs — ANTLR Plugin Antlr

JVM Targets

Application

Jar .jar JVM
JPackage Native · JVM bundled
GraalVM Native Native

Jar

Builds an executable .jar

build.gradle.kts
plugins {
`java`

Apply a JVM language plugin

}
tasks {
jar {
manifest {
attributes["Main-Class"] = "me.username.Main"

Reference your Main class here

}
}
}
  • Run :jar
  • Open build / libs / .jar

Oracle Docs — JAR Manifest · Main Class

JPackage

Builds a native installer or portable executable with bundeled .jar + JVM

build.gradle.kts
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 {
jpackage {

Configure JPackage here
GitHub — JPackage Gradle Plugin

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

build.gradle.kts
plugins {
`java`

Apply a JVM language plugin

id("org.graalvm.buildtools.native") version "1.1.0"

Apply the GraalVM Native plugin

}
graalvmNative {

GraalVM Native is configured here
GraalVM Docs — Native Gradle plugin

binaries {
named("main") {
mainClass = "me.username.Main"

Reference your Main class here

}
}
}
  • Download GraalVM JDK
  • Unarchive GraalVM JDK and place in ~/.jdks
  • Set Environment Variable GRAALVM_HOME to location of GraalVM JDK
  • Run :nativeCompile
  • Open build / native / nativeCompile / .exe

GraalVM Docs — Native Gradle plugin

Library

Publish Dependency

build.gradle.kts
plugins {
`java`

Apply a JVM language plugin

`maven-publish`

Apply the Maven publish plugin
Gradle Docs — Maven Publish 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}"
version = "${project.version}"

The Maven coordinates group:artifact:version can be overriden

pom {
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"
relocation {

Maven publication can indicate it was moved

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()
}
}
properties = mapOf()

properties can be defined
Maven Docs — POM · Properties

withXml {

The XML can be configured

with(asNode()) { //! blur
appendNode("name", mapOf("key" to "value"), "value") //! blur
.appendNode("child-name", mapOf("key" to "value"), "child-value")
} //! blur
}
}
from(components["java"])

Publish your Gradle project as a Maven publication

  • *.jar
  • Dependencies

Every JVM Language uses components["java"]

artifacts.removeIf { artifact ->
artifact.classifier in setOf("sources", "javadoc")
}

sources.jar and javadoc.jar can be excluded from the publication

artifact(file("build/libs/artifact.zip")) {

Alternatively, atifacts can be included

  • The published file name is [project]-[version]-[classifier].[extension]
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 {
repositories {

Maven repositories to publish to are listed here

mavenLocal()

Maven Local is the local respository on your machine

  • By default it is located at ~/.m2/repository
  • It is redundant to include it, a task for it is generated by default

Gradle Docs — Local Maven Repository Maven Local

maven {
maven {

Publish to GitHub Packages
GitHub Docs — Gradle Publish

name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/OWNER/REPOSITORY")
credentials {
username = System.getenv("GITHUB_ACTOR")
password = System.getenv("GITHUB_TOKEN")
}
}
maven {
maven {

Publish to Sonatype Nexus
Sonatype Docs — Nexus Gradle Publish

name = "Nexus"
url = uri("http://nexus.username.me/repository/repository-name")
credentials {
username = System.getenv("NEXUS_USER")
password = System.getenv("NEXUS_PASS")
}
}
maven {
maven {

Publish to JFrog Artifactory
JFrog Docs — Artifactory Gradle Publish

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

Version Catalog Config

gradle/libs.versions.toml
[versions]
[versions]

Dependency versions are listed here
Gradle Docs — Version Catalog · 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]
[bundles]

Dependency groups are listed here
Gradle Docs — Version Catalog · Bundles

processing = [ "processing-core", "processing-net" ]
...
[plugins]
[plugins]

Plugin dependencies are listed here
Gradle Docs — Version Catalog · Plugins

shadow = { id = "com.gradleup.shadow", version = "9.0.0" }
...

Referencing Version Catalog

build.gradle.kts
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.processing.core)

Dependencies can be referenced:

  • org.processing:core:4.0.0

- in .toml are replaced by . in .gradle.kts

implementation(libs.bundles.processing)

Bundles add many dependencies at once:

  • org.processing:core:4.0.0
  • org.processing:net:3.0.0
}

Gradle Docs — Version Catalog Format Version Catalogs

Properties

Centralized config for properties

Properties Layout

Properties Config

gradle.properties
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_

Gradle Docs — Project properties

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

Oracle Docs — System properties

28 collapsed lines
org.gradle.caching=false
org.gradle.caching.debug=false
org.gradle.configuration-cache=false
org.gradle.configureondemand=false
# org.gradle.console=
org.gradle.console.interactive=true
org.gradle.continue=false
org.gradle.daemon=true
org.gradle.daemon.idletimeout=10800000
org.gradle.debug=false
# org.gradle.java.home=
org.gradle.java.installations.auto-detect=true
org.gradle.java.installations.auto-download=true
org.gradle.java.installations.paths=
org.gradle.java.installations.fromEnv=
org.gradle.jvmargs=-Xmx512m "-XX:MaxMetaspaceSize=384m"
org.gradle.logging.level=lifecycle
org.gradle.parallel=false
org.gradle.priority=normal
org.gradle.projectcachedir=.gradle
org.gradle.problems.report=true
# org.gradle.tooling.parallel=
org.gradle.unsafe.isolated-projects=false
org.gradle.unsafe.isolated-projects.diagnostics=false
org.gradle.vfs.verbose=false
org.gradle.vfs.watch=true
org.gradle.warning.mode=summary
# org.gradle.workers.max=

Gradle properties with namespace org.gradle can be configured here

  • They are System properties

Gradle Docs — Gradle properties reference

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 Docsgradle.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

build.gradle.kts
val property = property("key")

Returns the Project property value or throws

val propertyOrNull = findProperty("key")

Returns the Project property value or null

Gradle Docs — Accessing a project property

System properties

build.gradle.kts
val property = System.getProperty("key")

Returns the System property value or null

Oracle Docs — Reading System Properties

Environment variables

build.gradle.kts
val variable = System.getenv("KEY")

Returns the Environment variable value or null

Gradle Docs — Environment variables

This website is currently available on Desktop only
Let @Stephcraft know on Discord you'd like a Mobile version

Join Discord