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

**URL:** <https://discuss.gradle.org/t/get-version-from-version-catalogs-in-a-gradle-plugin-and-add-dependency/51782>\
**Category:** Help/Discuss\
**Created:** [November 24, 2025, 10:58am UTC](https://discuss.gradle.org/t/get-version-from-version-catalogs-in-a-gradle-plugin-and-add-dependency/51782 "2025-11-24T10:58:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oSumAtrIX](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/osumatrix/32/11092_2.png) [@oSumAtrIX](https://discuss.gradle.org/u/oSumAtrIX)\
**Post date:** [November 24, 2025, 10:58am UTC](https://discuss.gradle.org/t/get-version-from-version-catalogs-in-a-gradle-plugin-and-add-dependency/51782/1 "2025-11-24T10:58:26Z")

</div>

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.

```gradle
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

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [November 25, 2025, 6:48pm UTC](https://discuss.gradle.org/t/get-version-from-version-catalogs-in-a-gradle-plugin-and-add-dependency/51782/2 "2025-11-25T18:48:23Z")

</div>

Where do you call this?  
Can you show an [MCVE](https://stackoverflow.com/help/mcve)?  
The version catalog extension should be added quite soon.  
Can you show a build `--scan` URL?

---

<div class="post-metadata">

**Author:** ![oSumAtrIX](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/osumatrix/32/11092_2.png) [@oSumAtrIX](https://discuss.gradle.org/u/oSumAtrIX)\
**Post date:** [November 25, 2025, 8:24pm UTC](https://discuss.gradle.org/t/get-version-from-version-catalogs-in-a-gradle-plugin-and-add-dependency/51782/3 "2025-11-25T20:24:00Z")

</div>

I think I narrowed the issue down here: [Cannot apply plugins to subprojects from within a settings plugin - #3 by oSumAtrIX](https://discuss.gradle.org/t/cannot-apply-plugins-to-subprojects-from-within-a-settings-plugin/51783/3)

If it is unrelated, I’ll continue here.
