Is there a way to get Gradle's Kotlin DSL to autog...
# gradle
c
Is there a way to get Gradle's Kotlin DSL to autogenerate type-safe accessors for items in the
gradle.properties
file like it used to for Groovy? Instead of having to bind every single one manually with
val foo by properties
.
h
Not by default. You could write such generator by yourself, but I would recommend convention plugins instead. Groovy is also not type safe, but supports dynamic „typing“.
c
How do you make convention plugins work there? Variables don't carry over between scripts, even if you
include
them
Oh I bet you can do
val Project.foo by properties
, can't you
h
Well, just don't use properties at all
c
Oh
Same question then
How do config carry over
h
What do you mean? Do you have a sample?
If you need to apply the same logic in different projects, just apply the convention plugin in each project
c
I mean this:
Copy code
// buildSrc/.../my.convention.gradle.kts
val myProperty = "foo"

// build.gradle.kts
plugins {
  id("my-convention")
}

println(myProperty) // can't reference it
I want to have constants that are used in multiple places bound in one place
h
Yeah, but what's the real use case?
c
like my project coordinates, url, other metadata
h
You can have constants in a kt file in build-logic
c
multiple convention plugins will use the information, but the constants should be in one place
You can have constants in a kt file in build-logic
Could you explain? I've never been able to get that to work.
Do you mean just a plain ol kotlin file, not .gradle.kts?
h
Yes
c
I wasn't aware those got resolved. Neat, thanks!
Do you need to do anything to import them into a script like with convention plugins? Or are they automatically brought into all buildscripts in the project?
h
It depends. Inside your build-logic src, it is a variable/function/getter, so you need to import it like regular kotlin, eg packages etc.
c
But I mean they're just on the gradle file's classpath by default?
h
If you also want to use it from your actual projects, you need to include the build-logic classes in your project classpath, and the easiest way to do this is using a settings plugin
c
I'll try it, thx
Wow yeah ok, I had no idea buildSrc allowed regular kotlin files
Thank you so much!
v
If it is in
buildSrc
it is in an ancestry classloader of all project build scripts, so like the plugins available everywhere. If it is in a proper included build, it is only available where you use it and thus does not invalidate everything unrelated, meaning, if you apply a plugin from the same project the class is available, or if you declare a buildscript classpath dependency. Or if it is in an ancestry classloader like the settings script classloader. Btw. "project coordinates" sounds like a bad idea, at least if you use it to manipulate a publication, that is seldomly a good idea and you should instead make the defaults work, or you for example need manual substitutions as soon as you want to use the build in a composite build and so on.