I am trying to write a new Gradle plugin which requires the application of two plugins: either java or java-library, or maven-publish. I am writing the plugin in Kotlin.
I can easily, following the Gradle guide, make the plugin react to a single plugin application:
project.plugins.withType(MavenPublishPlugin::class.java) {
// My stuff
}
But I can't do the same for requiring two plugin applications:
project.plugins.withType(JavaPlugin::class.java) {
project.plugins.withType(MavenPublishPlugin::class.java) {
// My stuff
}
}
In the second case, launching with:
plugins {
id("java")
id("maven-publish")
id("org.danilopianini.publish-on-central")
}
runs the plugin as expected
using:
plugins {
id("java")
id("org.danilopianini.publish-on-central")
}
does not apply the plugin, as expected
but, finally:
plugins {
id("maven-publish")
id("org.danilopianini.publish-on-central")
}
tries to apply the plugin and fails with:
FAILURE: Build failed with an exception.
* Where:
Build file '/tmp/junit876726632157783854/build.gradle.kts' line: 1
* What went wrong:
An exception occurred applying plugin request [id: 'org.danilopianini.publish-on-central']
> Failed to apply plugin [id 'org.danilopianini.publish-on-central']
> SoftwareComponentInternal with name 'java' not found.
* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Exception is:
org.gradle.api.plugins.InvalidPluginException: An exception occurred applying plugin request [id: 'org.danilopianini.publish-on-central']
[cut]
Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin [id 'org.danilopianini.publish-on-central']
... 128 more
Caused by: org.gradle.api.UnknownDomainObjectException: SoftwareComponentInternal with name 'java' not found.
at org.gradle.api.internal.DefaultNamedDomainObjectCollection.createNotFoundException(DefaultNamedDomainObjectCollection.java:489)
at org.gradle.api.internal.DefaultNamedDomainObjectCollection.getByName(DefaultNamedDomainObjectCollection.java:323)
Which looks to me as an attempt to apply the plugin regardless the fact that the Java plugin was not applied.
Do you know any way to configure a plugin to react to multiple plugin applications?