TExcelTAD
Home › Java Builds: Maven & Gradle › Version Catalogs (libs.versions.toml)

Version Catalogs (libs.versions.toml)

In a build with twenty projects, "com.google.guava:guava:33.7.2-jre" written in twelve build scripts means twelve places to update, and sooner or later two of them disagree. A version catalog is Gradle's answer: one TOML file, gradle/libs.versions.toml, that names every external dependency and plugin, and generated type-safe accessors (libs.guava) that build scripts use instead of strings.

The file

toml
[versions]                       # shared version numbers, referenced by name
jackson = "2.22.4"
junit = "5.14.4"

[libraries]                      # alias = coordinates (+ version or version.ref)
guava = "com.google.guava:guava:33.7.2-jre"
commons-lang3 = { module = "org.apache.commons:commons-lang3", version = "3.21.0" }
jackson-bom = { module = "com.fasterxml.jackson:jackson-bom", version.ref = "jackson" }
jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind" }   # version from the BOM
junit-bom = { module = "org.junit:junit-bom", version.ref = "junit" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter" }
junit-launcher = { module = "org.junit.platform:junit-platform-launcher" }

[bundles]                        # several libraries declared together
logging = ["slf4j-api", "slf4j-simple"]

[plugins]                        # plugin id + version
shadow = { id = "com.gradleup.shadow", version = "9.2.2" }

Gradle reads gradle/libs.versions.toml automatically and creates the libs extension. Aliases are split at -, _ and . into nested accessors: commons-lang3 becomes libs.commons.lang3, jackson-bom becomes libs.jackson.bom. In a build script, plugins are applied with alias(libs.plugins.shadow).

Using a catalog

bash · try it yourself
mkdir -p cat/app/src/main/java/demo cat/app/src/test/java/demo cat/gradle && cd cat
cat > gradle/libs.versions.toml <<'TOML'
[versions]
jackson = "2.22.4"
junit = "5.14.4"
slf4j = "2.0.20"

[libraries]
commons-lang3 = { module = "org.apache.commons:commons-lang3", version = "3.21.0" }
jackson-bom = { module = "com.fasterxml.jackson:jackson-bom", version.ref = "jackson" }
jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind" }
slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" }
slf4j-simple = { module = "org.slf4j:slf4j-simple", version.ref = "slf4j" }
junit-bom = { module = "org.junit:junit-bom", version.ref = "junit" }
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter" }
junit-launcher = { module = "org.junit.platform:junit-platform-launcher" }

[bundles]
logging = ["slf4j-api", "slf4j-simple"]
TOML
cat > settings.gradle.kts <<'KTS'
rootProject.name = "cat"
dependencyResolutionManagement { repositories { mavenCentral() } }
include("app")
KTS
cat > app/build.gradle.kts <<'KTS'
plugins { application }
dependencies {
    implementation(libs.commons.lang3)
    implementation(platform(libs.jackson.bom))
    implementation(libs.jackson.databind)
    implementation(libs.bundles.logging)
    testImplementation(platform(libs.junit.bom))
    testImplementation(libs.junit.jupiter)
    testRuntimeOnly(libs.junit.launcher)
}
application { mainClass = "demo.App" }
tasks.test { useJUnitPlatform() }
KTS
cat > app/src/main/java/demo/App.java <<'SRC'
package demo;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;

public class App {
    static String payload(String name) throws Exception {
        return new ObjectMapper().writeValueAsString(Map.of("name", StringUtils.capitalize(name)));
    }

    public static void main(String[] args) throws Exception {
        LoggerFactory.getLogger(App.class).info("starting");
        System.out.println(payload("catalog"));
    }
}
SRC
cat > app/src/test/java/demo/AppTest.java <<'SRC'
package demo;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

class AppTest {
    @Test
    void capitalises() throws Exception {
        assertEquals("{\"name\":\"Ada\"}", App.payload("ada"));
    }
}
SRC
gradle -q :app:run
gradle -q :app:test && echo "tests passed"
gradle -q :app:dependencies --configuration runtimeClasspath | grep -E '^[+\\]'
[main] INFO demo.App - starting
{"name":"Catalog"}
tests passed
+--- org.apache.commons:commons-lang3:3.21.0
+--- com.fasterxml.jackson:jackson-bom:2.22.4
+--- com.fasterxml.jackson.core:jackson-databind -> 2.22.4
+--- org.slf4j:slf4j-api:2.0.20
\--- org.slf4j:slf4j-simple:2.0.20

One line, implementation(libs.bundles.logging), added both SLF4J modules, and slf4j-simple printed its log line to standard error ([main] INFO demo.App - starting). Versions come from one place, and the junit-launcher entry has no version at all: the JUnit BOM supplies it. Since Gradle 9 the JUnit Platform launcher must be on the test runtime classpath explicitly, which is why every JUnit 5 setup in this course declares it.

A typo is a compile error

Because accessors are generated Kotlin, a wrong alias fails when the script compiles, with a precise message, rather than at resolution time with "could not find com.google.guava:guaav":

bash · try it yourself
mkdir -p cat/gradle && cd cat
printf '[libraries]\ncommons-lang3 = "org.apache.commons:commons-lang3:3.21.0"\n' > gradle/libs.versions.toml
printf 'rootProject.name = "cat"\ndependencyResolutionManagement { repositories { mavenCentral() } }\n' > settings.gradle.kts
cat > build.gradle.kts <<'KTS'
plugins { java }
dependencies { implementation(libs.commons.lang) }
KTS
gradle -q help 2>&1 | grep '^e: ' | sed 's#file:///home/learner/cat/##'
e: build.gradle.kts:2:44: Unresolved reference 'lang'.

How aliases become accessors

python · try it yourself
import re

def accessor(alias, kind="libraries"):
    parts = re.split(r"[-_.]", alias)
    prefix = {"libraries": "libs", "bundles": "libs.bundles", "plugins": "libs.plugins",
              "versions": "libs.versions"}[kind]
    return ".".join([prefix] + parts)

for alias, kind in [("guava", "libraries"), ("commons-lang3", "libraries"), ("jackson_databind", "libraries"),
                    ("junit.jupiter", "libraries"), ("logging", "bundles"), ("shadow", "plugins"),
                    ("jackson", "versions")]:
    print(f"{kind:9} {alias:17} -> {accessor(alias, kind)}")
libraries guava             -> libs.guava
libraries commons-lang3     -> libs.commons.lang3
libraries jackson_databind  -> libs.jackson.databind
libraries junit.jupiter     -> libs.junit.jupiter
bundles   logging           -> libs.bundles.logging
plugins   shadow            -> libs.plugins.shadow
versions  jackson           -> libs.versions.jackson

Note: A catalog declares versions; it doesn't enforce them. If a transitive dependency asks for a newer Guava, conflict resolution still picks it. To enforce, combine the catalog with a platform (platform(libs.my.platform)) or a constraint. If one alias is a prefix of another (groovy and groovy-json), the shorter one is reached with libs.groovy.asProvider(); naming both with a suffix (jackson-bom, jackson-databind) reads better.

Test yourself

What accessor does the catalog alias commons-lang3 produce?

  1. libs.commonsLang3
  2. libs.commons.lang3
  3. libs["commons-lang3"]
  4. catalog.commons_lang3

Where does Gradle look for the default version catalog?

  1. build.gradle.kts
  2. gradle/libs.versions.toml
  3. settings.gradle.kts only
  4. ~/.gradle/catalog.toml

Does a version catalog stop a transitive dependency from upgrading a library beyond the catalog's version?

  1. Yes, always
  2. No: a catalog only declares coordinates and versions; use a platform or constraint to enforce
  3. Only for plugins
  4. Only with --offline