How to Replace Spring Dependency Management Gradle Plugin with a Version Catalog

Are you looking for a cleaner alternative to managing dependencies in your Spring projects? While the Spring Dependency Management Gradle Plugin offers convenience, it may sometimes lead to issues such as transitive dependency conflicts. Enter the version catalog—a more streamlined solution. Let’s dive into how you can replace the Spring Dependency Management Gradle Plugin with a version catalog for smoother dependency management in your Spring projects.

Why Replace the Spring Dependency Management Gradle Plugin?

The Spring Dependency Management Gradle Plugin simplifies dependency management by providing a BOM (Bill of Materials) file that declares dependency versions. However, it can sometimes result in transitive dependency conflicts or inadvertently using outdated versions. By leveraging a version catalog, you gain greater control and flexibility over your project dependencies.

Creating a Version Catalog

Start by creating a libs.versions.toml file in the gradle folder of your project. This file will serve as your version catalog, containing sections for versions, libraries, and plugins. Here’s how it might look:

[versions]
# libs
flyway = "10.8.1" # https://mvnrepository.com/artifact/org.flywaydb/flyway-core
# plugins
spring-boot = "3.2.3" # https://spring.io/projects/spring-boot
kotlin = "1.9.22" # https://kotlinlang.org/docs/releases.html#release-details
sonarqube = "4.3.1.3277" # https://plugins.gradle.org/plugin/org.sonarqube

[libraries]
org-flywaydb-core = { module = "org.flywaydb:flyway-core", version.ref = "flyway" }
org-flywaydb-database-postgresql = { module = "org.flywaydb:flyway-database-postgresql", version.ref = "flyway" }

[plugins]
org-springframework-boot = { id = "org.springframework.boot", version.ref = "spring-boot" }
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-spring = { id = "org.jetbrains.kotlin.plugin.spring", version.ref = "kotlin" }
kotlin-jpa = { id = "org.jetbrains.kotlin.plugin.jpa", version.ref = "kotlin" }
sonarqube = { id = "org.sonarqube", version.ref = "sonarqube" }

Integrating Version Catalog in Gradle

Now, let’s integrate the version catalog into your Gradle build using Kotlin DSL.

plugins {
    alias(libs.plugins.org.springframework.boot)
    alias(libs.plugins.kotlin.jvm)
    alias(libs.plugins.kotlin.spring)
    alias(libs.plugins.kotlin.jpa)

    alias(libs.plugins.sonarqube)
    jacoco
}

...

dependencies {
    implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-web")

    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")

    implementation(libs.org.sonarsource.sonar.ws)
    implementation(libs.swagger.annotations.v3)

    runtimeOnly("org.postgresql:postgresql")
    runtimeOnly(libs.org.flywaydb.core)
    runtimeOnly(libs.org.flywaydb.database.postgresql)

    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.springframework.boot:spring-boot-testcontainers")
    testImplementation("org.testcontainers:junit-jupiter")
    testImplementation("org.testcontainers:postgresql")
}

So all versions are either coming from the Spring Boot BOM that is imported first or from the libs.versions.toml file which is taken in account automatically.

Conclusion

Replacing the Spring Dependency Management Gradle Plugin with a version catalog provides a cleaner and more flexible approach to dependency management in your Spring projects. By centralizing your dependency versions in a separate file, you gain more control and avoid potential conflicts or outdated versions. Give it a try in your next Spring project and experience smoother dependency management firsthand. Happy coding!