TL;DR:
A project plugin can be configured by the config block introduced by creating an extension for it (project.extensions.create(...)
). How can this be done in a “Settings” plugin?
Long story:
I have a Settings plugin, that should apply a different “Project” plugin, if configured to do so. To do that I have the following code:
MySettingsPluginExtension.kt
open class MySettingsPluginExtension
@Inject constructor(
private val settings: Settings,
objects: ObjectFactory
) {
val applyProjectPlugin: Property<Boolean> =
objects.property<Boolean>()
.convention(
settings.providers.provider {
return@provider settings.extensions.extraProperties.let {
if (it.has("mySettings.applyProjectPlugin")) it.get("mySettings.applyProjectPlugin").toString().toBoolean() else true
}
}
)
}
MySettingsPlugin.kt
open class MySettingsPlugin : Plugin<Settings> {
override fun apply(settings: Settings) {
val mySettingsPluginExtension = settings.extensions.create("mySettings", MySettingsPluginExtension::class.java, settings)
// automatically apply the project plugin in the root project, if configured
if (mySettingsPluginExtension.applyProjectPlugin.get()) {
settings.gradle.rootProject {
it.plugins.apply(MyProjectPlugin::class.java)
}
}
}
}
My first assumption was, that I just get a block mySettings { ... }
in my settings.gradle.kt
where I could do applyProjectPlugin.set(false)
to disable it. That does not exist though.
It works to set mySettings.applyProjectPlugin=false
in my gradle.properties
file.
I found two ways of having some way to configure my plugin, but both don’t seem to work:
I added the following block to my Settings Plugin:
fun Settings.mySettings(action: Action<MySettingsPluginExtension>): Unit =
(this as ExtensionAware).extensions.configure("mySettings", action)
Now I can import this method in my settings.gradle.kt
:
import com.example.gradle.plugins.settings.mySettings
...
mySettings {
applyProjectPlugin.set(false)
}
But changing values there does not seem to have any effect.
The same goes for the following block in the project I applied the Settings plugin to:
configure<MySettingsPluginExtension> {
applyProjectPlugin.set(false)
}
After both of them I would expect, that the project plugin is not applied anymore. But it still is.
So my question: How do I configure my Settings plugin from my actual project.
Thanks in advance!