Define non-dependency versions (extension properties) in a central place

I use a few static code analysis plugins like checkstyle and PMD that can be configured to use a specific version of the underlying tools. I also use convention plugins, i.e. I have a checkstyle specific plugin that I can include from my generic Java convention plugin.

plugins {

checkstyle {
    maxWarnings = 0
    toolVersion = "10.4"

I’d like to move the version constraint “10.4” to some central place. For regular dependency versions I use a platform project with dependencies.constraints. With Groovy I was able to create (and include) a simple versions convention plugin that only defines checkstyle { toolVersion = "10.4" }. However, with Kotlin this only works if I also include the “checkstyle” plugin from said version plugin. Furthermore, I don’t think (implictly?) including “checkstyle” from the versions plugin is a good idea, especially if (in the future) I don’t want to have checkstyle at all.

I tried using PluginManager.withPlugin. However, I don’t see how I can check the type of the returned AppliedPlugin instance. Before I start throwing “instanceof” and type casts at the problem, I’d like to know:

What’s a better way of defining versions that are hidden in plugin properties?

This seems to do the trick. If the checkstyle plugin isn’t enabled, it fails the build instead of silently enabling it.

import org.gradle.api.plugins.quality.CheckstyleExtension

configure<CheckstyleExtension> {
    toolVersion = "10.4"

And you could wrap it in a pluginManager.withPlugin("checkstyle") { ... } to not do anything unless the plugin is applied and also work if the plugin is applied after your convention plugin.

Besides that, I usually define all versions, including those, in a version catalog and use it from there.

1 Like