I am trying to migrate some build logic into a plugin, and I need the plugin to be applied to Settings rather than Project. My question is: is this possible with a precompiled plugin script in Groovy? The linked comment above suggests to me that it should be possible, but I always get the following error when applying the plugin from settings.gradle:
> Unexpected plugin type
The plugin must be applied in a build script (or to the Project object), but was applied in a settings script (or to the Settings object)
I know that this is not the recommended way of doing this, and I intend to re-implement the logic in Kotlin or Java later on, but it would be really helpful if I could get it into a plugin first. Is there a way to write a settings plugin in this way? I have searched hard for any hints, even dipping into the Gradle source code, apologies if I have missed something obvious. I am using Gradle version 8.14.1.
And regarding your question, you just misnamed your file. foo.gradle is a project plugin, foo.init.gradle is an init plugin, foo.settings.gradle is a settings plugin.
The thing that is not recommended are legacy script plugins.
That is the thing you use with “apply from”.
Those have many quirks and are highly discouraged.
I was going by Implementation Options for Plugins. Row 4 of the first table (Groovy DSL/Pre-compiled script plugin) has “Recommended: OK” with note 2, which in turn says:
It is recommended to use a statically-typed language like Java or Kotlin for implementing plugins to reduce the likelihood of binary incompatibilities
which I’m completely on board with, BTW. This is why I am in the last stages of converting all my Groovy to Kotlin or Java.
I understood (misunderstood?) your comment from June 2023 to say that this only works for Kotlin, not Groovy, and I didn’t try hard enough… It turns out that I just needed to clean the plugin project after renaming the foo.gradle file and then it works as expected.
The June 2023 issue didn’t result in the change to the documentation that you suggested - I’ll see about opening a PR myself.
Thanks for your input - it will make my move away from Groovy much easier.
Or you enabled the static compilation of the Groovy files, then my comment of course would also not apply anymore, as you remove the duck-typing by that.
I’m quite sure that I haven’t done that. Maybe something changed since June 2023 as you say, but the documentation hasn’t kept up. I ought to see if there is something in the release notes that documents this: it would be good to refer to it in any suggestion to update the documentation. Is there a simple way of searching through the release notes for a bunch of releases, or do I have to go through the individual release notes one-by-one? (I’ve looked before for something like this, but without success.)