Idiomatic way to use platforms with version catalogs

Hello,

I’m trying out the new version catalog feature, and I was looking for an example on how to correctly use a version catalog with one or more platforms. Specifically, I have a spring boot application which uses a platform derived from the spring boot BOM, but I also have some dependencies in a version catalog that aren’t included in the bom. Right now, my build.gradle looks like this:

dependencies {
    implementation platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
    implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation libs.apache.velocity
    implementation libs.apache.commons.csv
    implementation 'commons-codec:commons-codec'
}

Is there a better way of handling the dependencies whose versions are derived from the platform? I tried to define type safe accessors in libs.versions.toml for the versionless dependencies, but I get an error saying that the version is required (which I guess makes sense).

The way I have it in the example does work, but it seems strange to mix and match hard coded strings and type safe accessors in that way.

1 Like

I had the same question. In 2021 this might not have been possible, but today (at least in Gradle 9.3-RC1, maybe before). One can write the following in the version catalog:

[versions]
testcontainers = "2.0.3"

[libraries]
testcontainers-bom = { module = "org.testcontainers:testcontainers-bom", version.ref = "testcontainers" }
testcontainers = { module = "org.testcontainers:testcontainers" }
testcontainers-junit-jupiter = { module = "org.testcontainers:testcontainers-junit-jupiter" }

And use it in the build file

dependencies {
    testImplementation(platform(libs.testcontainers.bom))
    testImplementation(libs.testcontainers)
    testImplementation(libs.testcontainers.junit.jupiter)
}

Yeah, versions for dependencies should be optional veeery long, I think it was only in the initial experimental version where they were mandatory.

For plugins the version became optional way later but is now too since quite some time.

1 Like

Thanks @Vampire for confirming :slight_smile:

1 Like