Get version from version catalogs in a Gradle plugin and add dependency

I am trying two things with a Gradle plugin that worked on Gradle 8, but does not work on Gradle 9:

  1. Get a version from the version catalogs
  2. Add a dependency to a project (with the version from the catalog)

So far, I don’t know how to get the version catalogs extension without using afterEvaluate. But in Gradle 9, if I use afterEvaluate, I can’t add dependencies because they are immutable after resolution. If I remove afterEvaluate, the extension is not present.

private fun Project.configureDependencies() {
    afterEvaluate {
        val catalog = extensions.getByType(VersionCatalogsExtension::class.java).named("libs")

        dependencies.add(
            "implementation",
            "some:" + catalog.findVersion("versionName").orElseThrow {
                IllegalArgumentException("Version with alias $versionAlias not found in version catalog")
            },
        )
    }
}

What can I do to achieve 1 & 2

Where do you call this?
Can you show an MCVE?
The version catalog extension should be added quite soon.
Can you show a build --scan URL?

I think I narrowed the issue down here: Cannot apply plugins to subprojects from within a settings plugin - #3 by oSumAtrIX

If it is unrelated, I’ll continue here.

1 Like