# 4. Centralizing dependencies

version 9.2.0

Central dependencies can be managed in Gradle using various techniques such as platforms and version catalogs. Each approach offers its own advantages and helps in centralizing and managing dependencies efficiently.

## Using platforms

A platform is a set of dependency constraints designed to manage the transitive dependencies of a library or application.

When you define a platform in Gradle, you’re essentially specifying a set of dependencies that are meant to be used together, ensuring compatibility and simplifying dependency management:

platform/build.gradle.kts

    plugins {
        id("java-platform")
    }

    dependencies {
        constraints {
            api("org.apache.commons:commons-lang3:3.12.0")
            api("com.google.guava:guava:30.1.1-jre")
            api("org.slf4j:slf4j-api:1.7.30")
        }
    }

platform/build.gradle

    plugins {
        id("java-platform")
    }

    dependencies {
        constraints {
            api("org.apache.commons:commons-lang3:3.12.0")
            api("com.google.guava:guava:30.1.1-jre")
            api("org.slf4j:slf4j-api:1.7.30")
        }
    }

Then, you can use that platform in your project:

app/build.gradle.kts

    plugins {
        id("java-library")
    }

    dependencies {
        implementation(platform(project(":platform")))
    }

app/build.gradle

    plugins {
        id("java-library")
    }

    dependencies {
        implementation(platform(":platform"))
    }

Here, `platform` defines versions for `commons-lang3`, `guava`, and `slf4j-api`, ensuring they are compatible.

Maven’s BOM (Bill of Materials) is a popular type of platform that Gradle supports. A BOM file lists dependencies with specific versions, allowing you to manage these versions in a centralized way.

A popular platform is the Spring Boot Bill of Materials. To use the BOM, you add it to the dependencies of your project:

build.gradle.kts

    dependencies {
        // import a BOM
        implementation(platform("org.springframework.boot:spring-boot-dependencies:1.5.8.RELEASE"))
        // define dependencies without versions
        implementation("com.google.code.gson:gson")
        implementation("dom4j:dom4j")
    }

build.gradle

    dependencies {
        // import a BOM
        implementation platform('org.springframework.boot:spring-boot-dependencies:1.5.8.RELEASE')
        // define dependencies without versions
        implementation 'com.google.code.gson:gson'
        implementation 'dom4j:dom4j'
    }

By including the `spring-boot-dependencies` platform dependency, you ensure that all Spring components use the versions defined in the BOM file.

## Using a Version catalog

A version catalog is a centralized list of dependency coordinates that can be referenced in multiple projects. You can reference this catalog in your build scripts to ensure each project depends on a common set of well-known dependencies.

First, create a `libs.versions.toml` file in the `gradle` directory of your project. This file will define the versions of your dependencies and plugins:

gradle/libs.versions.toml

    [versions]
    groovy = "3.0.5"
    checkstyle = "8.37"

    [libraries]
    groovy-core = { module = "org.codehaus.groovy:groovy", version.ref = "groovy" }
    groovy-json = { module = "org.codehaus.groovy:groovy-json", version.ref = "groovy" }
    groovy-nio = { module = "org.codehaus.groovy:groovy-nio", version.ref = "groovy" }
    commons-lang3 = { group = "org.apache.commons", name = "commons-lang3", version = { strictly = "[3.8, 4.0[", prefer="3.9" } }

    [bundles]
    groovy = ["groovy-core", "groovy-json", "groovy-nio"]

    [plugins]
    versions = { id = "com.github.ben-manes.versions", version = "0.45.0" }

Then, you can use the version catalog in you build file:

build.gradle.kts

    plugins {
        `java-library`
        alias(libs.plugins.versions)
    }

    dependencies {
        api(libs.bundles.groovy)
    }

build.gradle

    plugins {
        id 'java-library'
        alias(libs.plugins.versions)
    }

    dependencies {
        api libs.bundles.groovy
    }

Next Step: Learn about Dependency Constraints and Conflict Resolution >>