Version catalogs
A version written in five build scripts is five versions to update. Gradle 9 finds a TOML file by its path, gradle/libs.versions.toml, and turns every entry into an accessor of the libs object, so a build script names a library and the file names the version.
The catalog file
gradle/libs.versions.toml
[versions]
junit = "5.14.3"
[libraries]
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit" }
junit-launcher = { module = "org.junit.platform:junit-platform-launcher", version = "1.14.3" }
Three tables matter: [versions] holds the numbers, [libraries] the modules, and [bundles] a named group of modules. A library can name its version directly, as the launcher does, or refer to an entry of [versions], as the aggregate does. There is a fourth table, [plugins], for plugin ids and their versions.
The build script that reads it
settings.gradle
rootProject.name = 'shop'
build.gradle
plugins {
id 'java'
}
repositories {
mavenLocal()
}
dependencies {
testImplementation libs.junit.jupiter
testRuntimeOnly libs.junit.launcher
}
tasks.named('test') {
useJUnitPlatform()
}
The dash of junit-jupiter becomes a dot: libs.junit.jupiter. A library called commons-lang3 would be libs.commons.lang3, and a name that collides with a keyword gets a suffix: libs.junit.jupiter.versions for the version of the same entry.
The same graph
An accessor is not a shortcut that changes resolution: it expands to the same coordinate. The graph below is the one the previous lesson printed with the coordinates spelled out.
gradle -q dependencies --configuration testCompileClasspath --console=plain --offline --no-daemon
------------------------------------------------------------
Root project 'shop'
------------------------------------------------------------
testCompileClasspath - Compile classpath for source set 'test'.
\--- org.junit.jupiter:junit-jupiter:5.14.3
+--- org.junit.jupiter:junit-jupiter-api:5.14.3
| +--- org.opentest4j:opentest4j:1.3.0
| +--- org.junit.platform:junit-platform-commons:1.14.3
| | \--- org.apiguardian:apiguardian-api:1.1.2
| \--- org.apiguardian:apiguardian-api:1.1.2
\--- org.junit.jupiter:junit-jupiter-params:5.14.3
+--- org.junit.jupiter:junit-jupiter-api:5.14.3 (*)
\--- org.apiguardian:apiguardian-api:1.1.2
(*) - Indicates repeated occurrences of a transitive dependency subtree. Gradle expands transitive dependency subtrees only once per project; repeat occurrences only display the root of the subtree, followed by this annotation.
A web-based, searchable dependency report is available by adding the --scan option.
What the accessors are
- A catalog is for the whole build. One file at the root serves every project, and a subproject reads the same accessors.
- An alias must be unique. Gradle fails the build when two entries produce the same accessor, which is the one mistake a catalog catches early.
- The file is a build input. Changing a version in the TOML file invalidates the configuration cache, as a change in a build script would.
- It is not a lock file. The catalog names versions; it does not stop a transitive dependency from raising one. The conflict lesson is the other half.
Related
- Platforms and constraints: the BOM that keeps a family of modules on the numbers the catalog picks.
- The files of a Gradle project: where the catalog sits in a folder.
- dependencyManagement in Maven: the same idea as an import of a BOM.
Article author: Arthur Isaev