Gradle7 Version Catalog: How to use it with buildSrc?

Viewed 2155

I am very excited about the incubating Gradle's version catalogs and have been experimenting with it. I’ve found that the information in my gradle/libs.versions.toml is accessible in the build.gradle.kts scripts for my app and utility-lib projects.

However, I am unable to use the content of the toml file for buildSrc/build.gradle.kts or the convention files.

The only way that I could build was to hard-code the dependencies into those files, as I did before the version catalog feature.

In the buildSrc folder, I created a settings.gradle.kts file and inserted the dependencyResolutionManagement code for versionCatalogs, which is pointing to the same file as for my app and utility-lib projects.

Based on the Gradle7 docs, it seems that sharing a version catalog with buildSrc and modules is possible… I’d appreciate a nudge into getting it to work with buildSrc, if possible.

Here is a simple sample project, which I created via gradle init: my-version-catalog

Thank you for your time and help,

Mike

2 Answers

With Gradle 7.3.3, it is possible.

buildSrc/settings.gradle.kts

enableFeaturePreview("VERSION_CATALOGS")

dependencyResolutionManagement {
    versionCatalogs {
        create("libs") {
            from(files("../gradle/libs.versions.toml"))
        }
    }
}

buildSrc/build.gradle.kts


dependencies {
    implementation(libs.gradleplugin.intellij) // <- the lib reference
}

You can even use the version catalog for plugins

gradle/libs.versions.toml

...

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
jetbrains-changelog = { id = "org.jetbrains.changelog", version.ref = "changelog-plugin" }
jetbrains-intellij = { id = "org.jetbrains.intellij", version.ref = "intellij-plugin" }
hierynomus-license = { id = "com.github.hierynomus.license", version.ref = "license-plugin" }
nebula-integtest = { id = "nebula.integtest", version.ref = "nebula-integtest-plugin" }

build.gradle.kts

plugins {
    id("java")
    alias(libs.plugins.kotlin.jvm)
    alias(libs.plugins.nebula.integtest)
    alias(libs.plugins.jetbrains.intellij)
    alias(libs.plugins.jetbrains.changelog)
    alias(libs.plugins.hierynomus.license)
}

Quick note that IntelliJ IDEA 2021.3.2 RC may report libs.gradleplugin as an error, although the dependencies are correctly resolved.

Brice, it looks like a can of worms to go down that path, particularly for my situation, where I'm trying to use a libs.version.toml file from an android project, but the custom plugin is of course from a java/kotlin project. I tried creating the libs file by hardwiring the path to the toml file in the custom plugin. It might work if both were java projects, but I never tried that since that's not what I'm after. The ideal solution would be for the plugin to use the libs file from the project it is applied to, but it looks like the version catalog needs to be created in the settings file, before you even have access to "Project", so that's why you would have to hardwire the path.

Short answer. No, but there are other techniques for a custom plugin to get project version data from the project it is applied to.

Related