How to use a common `Versions` object with `buildSrc` plugins?

The sample project is available here: https://github.com/TheBestPessimist/gradle-buildSrc-example/tree/8177332319743c9e3c0e49c29815666167977585 and is a kotlin + spring boot project.

I want to have a “common gradle project” that i apply to my subprojects.
It is found at: buildSrc/src/main/kotlin/land/tbp/wwsc/tralala.gradle.kts and it applies multiple plugins, one of which is id("org.springframework.boot").

In order to set the versions of these plugins, i need to add the plugins as dependencies inside the file buildSrc/build.gradle.kts.

I have buildSrc/src/main/kotlin/land/tbp/wwsc/Versions.kt where i want to store all my versions. this also includes plugins.

I want to do this because the dependency implementation(platform("org.springframework.boot:spring-boot-dependencies:$springBootVersion")) (here) must have the same version as the one of the plugin that is applied.

The problem is that i cannot access import land.tbp.wwsc.Versions.springBootVersion from buildSrc/build.gradle.kts, so what i need to do is duplicate the properties which store the versions inside buildSrc/build.gradle.kts as well, and whenever i have to update a version, i need to so that in 2 places.

  • buildSrc/build.gradle.kts -> springBootVersion
  • buildSrc/src/main/kotlin/land/tbp/wwsc/Versions.kt -> Versions.springBootVersion

Please see here: https://github.com/TheBestPessimist/gradle-buildSrc-example/blob/8177332319743c9e3c0e49c29815666167977585/buildSrc/build.gradle.kts

My question is: is there a way to have the cake and eat it too?
Is there a way to use the object buildSrc/src/main/kotlin/land/tbp/wwsc/Versions.kt everywhere? Or is there an alternative which does not lead to code duplication?

Bump.

Does anyone have advice or experience with common versioning in buildSrc?

I just put a version.txt file in my root directory then reference via

buildscript {
	def pluginVersion = file('../version.txt').text.trim()
	dependencies {
		classpath "com.foo:myplugin:$pluginVersion"
	}
}

See here and here

Hey @Lance , thanks for the example.

I checked it out, but the way I see it working doesn’t quite align with what I want.
In your example, if i were to add multiple dependency versions there, i would have to parse that file into a map, just like a normal .properties file. therefore there is no variable names/type checking/autocomplete.

In my example, the file Versions.kt contains an Object, therefore i get the benefit of having autocomplete and all the other nice things.

So, afaics your example won’t work in my case.

Do you maybe have another idea on how to deal with my problem?