How to write a precompiled setting script plugin in Groovy?

Continuing the discussion from Sharing settings.gradle.kts code, gradle.properties, and possibly more between projects:

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.

Why is it not recommended?

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.

Thanks for the response

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.

1 Like

Ah, hm, maybe either things changed, or I was simply telling bullshit, could both be the case. :smiley:

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.)

I guess the best and easiest is to use Google like searching for

site:gradle.org "release notes" zinc

I decided against trying to make a PR for the documentation for now, I am sure that there are other people who would do a far better job than me. I have opened a new issue here: Missing info about precompiled script plugins for Settings and Gradle targets · Issue #33762 · gradle/gradle · GitHub. If the issue doesn’t progress after a while, I’ll have a go at a re-write and PR myself.

Thanks again.

1 Like