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:
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 and Make version catalogs accessible from precompiled script plugins · Issue #15383 · gradle/gradle · GitHub 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):
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)