Version Catalog: use versions only?

Hi,

my project has a single module, but i have some dependencies with multiple artifacts for which i need to align the versions.

Some examples:

Here there are 3 different artifacts, for which i want to use the same version:

  runtimeOnly("com.github.gotson.nightmonkeys:imageio-jxl:1.0.0")
  runtimeOnly("com.github.gotson.nightmonkeys:imageio-heif:1.0.0")
  runtimeOnly("com.github.gotson.nightmonkeys:imageio-webp:1.0.0")

Here i want to use the same version for different configurations:

  implementation("org.xerial:sqlite-jdbc:3.50.2.0")
  jooqGenerator("org.xerial:sqlite-jdbc:3.50.2.0")

I have read the documentation about the Version Catalog, however i don’t necessarily want to have to define aliases, this would add unnecessary complexity. I like to see the different artifacts used in my build.gradle, but i want to centralize versions so i don’t write them multiple times.

I tried the following, but it doesn’t work:

in .gradle/libs.versions.toml:

[versions]
sqlite-jdbc = "3.50.2.0"
nightmonkeys = "1.0.0"

In build.gradle:

  runtimeOnly("com.github.gotson.nightmonkeys:imageio-jxl:${libs.versions.nightmonkeys}")
  runtimeOnly("com.github.gotson.nightmonkeys:imageio-heif:${libs.versions.nightmonkeys}")
  runtimeOnly("com.github.gotson.nightmonkeys:imageio-webp:${libs.versions.nightmonkeys}")

  implementation("org.xerial:sqlite-jdbc:${libs.versions.sqlite.jdbc}")
  jooqGenerator("org.xerial:sqlite-jdbc:${libs.versions.sqlite.jdbc}")

Why can’t this work ?

And if it cannot work, is there another way to do that in Gradle ?

Currently i am doing this in Kotlin Script:

  run {
    val luceneVersion = "9.9.1"
    implementation("org.apache.lucene:lucene-core:$luceneVersion")
    implementation("org.apache.lucene:lucene-analysis-common:$luceneVersion")
    implementation("org.apache.lucene:lucene-queryparser:$luceneVersion")
    implementation("org.apache.lucene:lucene-backward-codecs:$luceneVersion")
  }

But it is a bit limited, for instance i cannot align a plugin version with a dependency version.

In addition, i would like to use the centralized version inside some plugin configuration, for example the gradle-jooq-plugin can be configured with the desired jooq version, which i could centralize, but the version has to be used inside the plugin configuration:

jooq {
  version = "3.19.24"
}

Is that something the Version Catalog can solve ?

Here there are 3 different artifacts, for which i want to use the same version

Be aware that (be it via version catalog or directly) you only request the same version, you do not necessarily get the same versions if for example some other library depends on one of the artifacts in a newer version unless those artifacts have Gradle Module Metadata with version alignment information configured properly. If you really need the same version, you might additionally need to configure version alignment manually if it was not done by that library.

Here i want to use the same version for different configurations:

Here even the alignment (configured by the library or manually by you) would not guarantee it is the same version as it is in different configurations.

I tried the following, but it doesn’t work:

libs.versions.nightmonkeys and libs.versions.sqlite.jdbc give you Provider<String>s, you need to add a .get() call to get the actual version as String, not just an implicit toString() like you have.

Thanks @Vampire , the .get() call was what i was missing!

1 Like