// Copyright (C) 2024 Gradle, Inc.
//
// Licensed under the Creative Commons Attribution-Noncommercial-ShareAlike 4.0 International License.;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

[[four-centralizing-dependencies]]
= 4. Centralizing dependencies

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.

[[sub:using-platforms]]
== 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:

====
include::sample[dir="snippets/java-platform/dep-man/kotlin",files="platform/build.gradle.kts[]"]
include::sample[dir="snippets/java-platform/dep-man/groovy",files="platform/build.gradle[]"]
====

Then, you can use that platform in your project:

====
include::sample[dir="snippets/java-platform/dep-man/kotlin",files="app/build.gradle.kts[]"]
include::sample[dir="snippets/java-platform/dep-man/groovy",files="app/build.gradle[]"]
====

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 link:https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies[Spring Boot Bill of Materials].
To use the BOM, you add it to the dependencies of your project:

====
include::sample[dir="snippets/dependencyManagement/managingTransitiveDependencies-constraintsFromBOM/kotlin",files="build.gradle.kts[tag=dependency-on-bom]"]
include::sample[dir="snippets/dependencyManagement/managingTransitiveDependencies-constraintsFromBOM/groovy",files="build.gradle[tag=dependency-on-bom]"]
====

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

[[sub:using-catalogs]]
== 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:

====
include::sample[dir="snippets/dependencyManagement/catalogs-toml-simple/kotlin",files="gradle/libs.versions.toml[]"]
====

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

====
include::sample[dir="snippets/dependencyManagement/catalogs-toml-simple/kotlin",files="build.gradle.kts[]"]
include::sample[dir="snippets/dependencyManagement/catalogs-toml-simple/groovy",files="build.gradle[]"]
====

[.text-right]
**Next Step:** <<dependency_constraints_conflicts.adoc#five-dependency-constraints-conflicts,Learn about Dependency Constraints and Conflict Resolution>> >>
