# Reference version from catalog in plugin

**URL:** <https://discuss.gradle.org/t/reference-version-from-catalog-in-plugin/44706>\
**Category:** Help/Discuss\
**Created:** [January 21, 2023, 1:08pm UTC](https://discuss.gradle.org/t/reference-version-from-catalog-in-plugin/44706 "2023-01-21T13:08:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![C-Otto](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/c-otto/32/8680_2.png) [@C-Otto](https://discuss.gradle.org/u/C-Otto)\
**Post date:** [January 21, 2023, 1:08pm UTC](https://discuss.gradle.org/t/reference-version-from-catalog-in-plugin/44706/1 "2023-01-21T13:08:42Z")

</div>

I’m writing a convention plugin that basically just provides convenient defaults for another plugin, namely `de.aaschmid.cpd`. As part of this, I’d like to provide a value for `cpd.toolVersion` (defined in `CodeQualityExtension`, a superclass of `CpdExtension`).

Instead of hardcoding this value, I’d like to use a version that I get from a separate version catalog plugin. A naive approach would be:

```gradle
cpd {
    minimumTokenCount = 123
    toolVersion = libs.versions.cpd.get() // or something like this
}

```

However, while I’m able to use my version catalog to define the version of the `de.aaschmid.cpd` plugin, this catalog is not available inside the plugin that I’d like to export.

I’ve seen [Give access to catalogs from binary plugins · Issue #15382 · gradle/gradle · GitHub](https://github.com/gradle/gradle/issues/15382) and [Make version catalogs accessible from precompiled script plugins · Issue #15383 · gradle/gradle · GitHub](https://github.com/gradle/gradle/issues/15383) but, to be honest, I don’t see how this helps.

I managed to compile my plugin with unsafe/untyped access to my catalog (`val libs = extensions.getByType<VersionCatalogsExtension>().named("libs")` etc.), but haven’t managed to include this plugin in my project so that the `libs` catalog is known (even though I included it in that project, too):

```gradle
An exception occurred applying plugin request [id: 'de.c-otto.java-conventions']
> Failed to apply plugin 'de.c-otto.java-conventions.cpd'.
   > Extension of type 'VersionCatalogsExtension' does not exist. Currently registered extension types: [ExtraPropertiesExtension, BasePluginExtension, DefaultArtifactPublicationSet, SourceSetContainer, ReportingExtension, JavaToolchainService, JavaPluginExtension, TestingExtension, CpdExtension, NullAwayExtension, CheckstyleExtension]

```

**How can I reference a version defined in an imported version catalog inside a plugin so that this version is inlined and used when I publish the plugin?**

(Thanks to @Vampire who’s very helpful, both here and in the linked GitHub issues)

---

<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:** [January 21, 2023, 5:12pm UTC](https://discuss.gradle.org/t/reference-version-from-catalog-in-plugin/44706/2 "2023-01-21T17:12:57Z")

</div>

Can you show how you “included it in that project”?  
Because when the catalog is there, it should work with that string-y API or with my hack-around from the issue you mentioned.

---

<div class="post-metadata">

**Author:** ![C-Otto](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/c-otto/32/8680_2.png) [@C-Otto](https://discuss.gradle.org/u/C-Otto)\
**Post date:** [January 21, 2023, 5:43pm UTC](https://discuss.gradle.org/t/reference-version-from-catalog-in-plugin/44706/3 "2023-01-21T17:43:02Z")

</div>

The version catalog is published to my local Maven repository (`de.c-otto:version-catalog:2023.01.21`).

# Conventions plugin project
published to my local maven repository (de.c-otto:java-conventions:2021.01.21)  

settings.gradle.kts:

```gradle
dependencyResolutionManagement {
    repositories {
        mavenLocal()
    }
    versionCatalogs {
        create("libs") {
            from("de.c-otto:version-catalog:2023.01.21")
        }
    }
}

```

build.gradle.kts:

```gradle
repositories {
    mavenCentral()
    mavenLocal()
    gradlePluginPortal()
}

val platformVersion = libs.versions.platform.get()

dependencies {
    implementation(platform("de.c-otto:java-platform:$platformVersion")) // unrelated, I think
    implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
    implementation(libs.gradleplugins.cpd) // this resolves to de.aaschmid.cpd:gradle-cpd-plugin:3.3 and works as expected
}

```

src/main/kotlin/de.c-otto.de.java-conventions.gradle.kts

```gradle
plugins {
    java
    `java-test-fixtures`
    id("de.c-otto.java-conventions.cpd")
}

java {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
    consistentResolution {
        useCompileClasspathVersions()
    }
}

val libs = extensions.getByType<VersionCatalogsExtension>().named("libs")
val platformVersion = libs.findVersion("platform").get()

dependencies {
    implementation(platform("de.c-otto:java-platform:$platformVersion")) // unrelated?
}

repositories {
    mavenCentral()
    mavenLocal()
}

```

src/main/kotlin/de.c-otto.java-conventions.cpd.gradle.kts

```gradle
import de.aaschmid.gradle.plugins.cpd.Cpd

val libs = extensions.getByType<VersionCatalogsExtension>().named("libs")

plugins {
    id("de.aaschmid.cpd")
}

cpd {
    toolVersion = libs.findVersion("pmdCpd").get().strictVersion // `pmdCpd = "6.53.0"` is defined in the `[versions]` block in my TOML file
}

```

# Consuming Java project

both settings.gradle.kts and buildSrc/settings.gradle.kts include:

```gradle
dependencyResolutionManagement {
    repositories {
        mavenCentral()
        mavenLocal()
    }
    versionCatalogs {
        create("libs") {
            from("de.c-otto:version-catalog:2023.01.21")
        }
    }
}

```

buildSrc/build.gradle.kts:

```gradle
plugins {
    `kotlin-dsl`
}

repositories {
    gradlePluginPortal()
    mavenLocal()
}

val platformVersion = libs.versions.platform.get()

dependencies {
    implementation("de.c-otto:java-conventions:$platformVersion")
    implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
}

```

buildSrc/src/main/kotlin/de.c-otto.playground.java-conventions.gradle.kts:

```gradle
plugins {
    java
    id("de.c-otto.java-conventions")
}

group = "de.c-otto.playground"

repositories {
    mavenLocal()
}

```

Failure:

```gradle
An exception occurred applying plugin request [id: 'de.c-otto.java-conventions']
> Failed to apply plugin 'de.c-otto.java-conventions.cpd'.
   > Extension of type 'VersionCatalogsExtension' does not exist. Currently registered extension types: [ExtraPropertiesExtension, BasePluginExtension, DefaultArtifactPublicationSet, SourceSetContainer, ReportingExtension, JavaToolchainService, JavaPluginExtension, TestingExtension]

```

---

<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:** [January 21, 2023, 8:33pm UTC](https://discuss.gradle.org/t/reference-version-from-catalog-in-plugin/44706/4 "2023-01-21T20:33:38Z")

</div>

I think I know the problem, can you please show more of the previous output?

---

<div class="post-metadata">

**Author:** ![C-Otto](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/c-otto/32/8680_2.png) [@C-Otto](https://discuss.gradle.org/u/C-Otto)\
**Post date:** [January 21, 2023, 9:30pm UTC](https://discuss.gradle.org/t/reference-version-from-catalog-in-plugin/44706/5 "2023-01-21T21:30:39Z")

</div>

Here’s the output with `-i --stacktrace`:  
[https://c-otto.de/gradle-out.txt](https://c-otto.de/gradle-out.txt)

---

<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:** [January 22, 2023, 2:55am UTC](https://discuss.gradle.org/t/reference-version-from-catalog-in-plugin/44706/6 "2023-01-22T02:55:09Z")

</div>

Yeah, as I thought.  
It happens during `:buildSrc:generatePrecompiledScriptPluginAccessors`.  
In that step the `plugins { ... }` blocks of the precompiled Kotlin DSL script plugins are extracted, copied over to a dummy project and then executed to find out which type-safe accessors to generate.  
In that dummy project the version catalog is not available and thus this step fails.

I guess you should report this as issue if there is none yet, but remove the traces of my hack-around to show that it has nothing to do with the problem. 🙂

As a work-around, do not use the `plugins { ... }` block, but the legacy `apply` way to apply the plugin, then it should work I think.

Sidenote: Why do you only take the version for the platform from the version catalog and not the whole platform like `libs.platform`?

---

<div class="post-metadata">

**Author:** ![C-Otto](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/c-otto/32/8680_2.png) [@C-Otto](https://discuss.gradle.org/u/C-Otto)\
**Post date:** [January 22, 2023, 9:19am UTC](https://discuss.gradle.org/t/reference-version-from-catalog-in-plugin/44706/7 "2023-01-22T09:19:54Z")

</div>

Will do, thanks a lot!

I haven’t used `libs.platform` because I noticed the same thing _after_ I published and tagged the platform/catalog project. I’ll change that soon, though - thanks for the hint 🙂

---

<div class="post-metadata">

**Author:** ![C-Otto](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/c-otto/32/8680_2.png) [@C-Otto](https://discuss.gradle.org/u/C-Otto)\
**Post date:** [January 22, 2023, 10:39am UTC](https://discuss.gradle.org/t/reference-version-from-catalog-in-plugin/44706/8 "2023-01-22T10:39:54Z")

</div>

Looks like someone beat me to it: [`VersionCatalogExtension` not added to the `gradle-kotlin-dsl-accessors` during `generatePrecompiledScriptPluginAccessors` · Issue #22468 · gradle/gradle · GitHub](https://github.com/gradle/gradle/issues/22468)
