is there a way to make an extension property simil...
# plugin-development
j
is there a way to make an extension property similar to
application
java
and
kotlin-dsl
etc for applying my custom plugin? i tried adding it in my plugin code, and my buildscripts were able to compile using it, but at runtime I got a classnotfound exception. i am guessing the extension property/function needs to be on the buildscript classpath. would adding it to a settings plugin, or custom wrapper work?
e
Do you mean like for a published plugin for other users to use to apply it? That's a bit of a chicken and egg problem because you couldn't add the extension without first applying the plugin.
v
If the plugin is in the classloader somehow, it will work without you needing to do anything. If you for example add the plugin to
buildSrc
dependencies, or to the settings script classpath, then it is in a parent classloader and that's already available. In this case the plugin ID automatically gets a type-safe accessors generated that you can use in the
plugins
block. Without this, you have the chicken and egg problem Eric mentioned.
j
Ok thanks. In this case, i have a companion settings plugin, so that option seems promising
What do you mean by "plugin id automatically gets typesafe accessor" though ?
v
if you would otherwise write
Copy code
plugins {
    id("my.cool.plugin")
}
you can automatically write
Copy code
plugins {
    my.cool.plugin
}
just make sure you do not have chars in the plugin id that are illegal in an identifier like
-
. I think in that case no accessor is generated. For that use-case, it imho is better to use camelCase than kebab-case.
If your project plugins are in the same jar as the settings plugin and the settings plugin is applied, you should not need to do anything further.
Except maybe when you have kebab-case for example
e
For kebab-case you have to surround it with ``` just like the bundled plugins, e.g., `my.cool.`kebab-case-plugin``.
v
Oh, really? I meant to remember that in this case the accessors are not going to be generated. Maybe confused it with some other case.
Then I probably just did it to not have to use the backticks as that imho hurts the readability more than the slight inconsistency of sometimes having kebab-case, sometimes camelCase. 🙂
e
Yep. Had to test it myself to confirm. 😄 I typically only use version catalogs now.
v
I think in my case that was not possible because in version catalog you have to have a version for the plugin and if the plugin is already in the classpath through the settings script classloader you then get the "cannot check compatibility" error
(except if you are on 8.8 of course as you can now omit the version of plugins in the version catalog, but in the build I'm currently working, 7.6.4 is still the way to go for reasons)
Besides that it is nice to have the possibilites auto-completed without having to put them to the version catalog in this case 🙂
e
8.8 ❤️
v
Like always, each option has its pros and cons 🙂
j
ok cool this is awesome. I never knew that this was a thing. so basically the the plugin needs to be in the root buildscript classpath for it to generate these accessors for you?
confirmed that this worked for me. Thanks all!
party gradlephant 1
v
In any parent class loader. For usage in subprojects, root project class loader is fine. For usage in root project, then for example settings class loader or buildSrc class loader