taken from <https://docs.gradle.org/current/usergu...
# gradle
e
as the docs say, either receive it via
@Inject
or access it through
project.objects
v
Actually with a recent enough version of Gradle you can even do easier. Just make the class abstract and the property too. When Gradle instantiates it, it will implement the abstract properties automatically.
e
yep, that's sufficient 90% of the time. I do have a few usages like
Copy code
val foo = objects.property<String>().apply { set("default") }
that still need
ObjectFactory
though
v
Shouldn't that be
objects.property<String>().convention("default")
? Defaults could also be set by a plugin or in the constructor / init function. But yes, that is a valid use-case of course 🙂
s
My issue is that I cant find the function
objects.property<String>()
but
objects.property(String::class.java).apply { set("") }
is available.
Also,
convention
is Incubating feature right now. Should I use it it production code?
v
I tend to use incubating features, at least if I develop for a specific build. If I would develop something reusable by many builds I would maybe be more conservative. Incubating things in Gradle are most often just not finalized in their api so upgrading the Gradle version might break the code and need a little adjustment. But that's a matter of PoV of course. :-)
Doesn't your IDE find the correct method to import? Iirc it should be something like
org.gradle.dsl.property
or something like that.
v
Ah, damn brain, forgot a
.kotlin
m
Are you writing a Gradle plugin with Kotlin @Sourabh Rawat? I had the same issue like you. My issue was to use
kotlin("jvm")
instead of
kotlin-dsl
plugin. You definitively need the
kotlin-dsl
plugin for this: Kotlin DSL Plugin This will add Kotlin, the Gradle Java Plugin, and many more needed configuration.
s
@mazorius I think that's what I was looking for.
👍🏼 1
@Vampire how do I provide default values for abstract class workflow for extension?
m
I use in my plugins most of the time convention to set a default. But if you have concerns by using incubating maybe
.value()
is fine for you. Don’t know if this is incubating as well.
v
Either in the constructor / init-function, or from the plugin that adds the extension or task.
Or you don't make it abstract but use the object factory yourself, then you can use convention or apply/set on the declaration as shown