Hey everyone!
Is it possible to add plugin and/or task configuration if a plugin is applied without restricting it to a specific version of the plugin?
I want to get rid of some of the boilerplate we have in all our Gradle projects. That boilerplate is mainly credentials to our private registry and our standard versioning configuration. So my idea is to build a plugin, that takes over that in case the specific plugins are applied. Some examples of those are:
- pl.allegro.tech.build.axion-release for versioning
- org.springframework.boot
- com.bmuschko.docker-remote-api
- org.unbroken-dome.helm
I’m fine with specifying the explicit version of the axion-release plugin, as it’s not related to the projects itself and I’m doing the full configuration in the plugin anyways (with the option to overwrite it).
But for something like the Spring boot plugin, I just want to do something like
tasks.withType<BootBuildImage> {
builder = "..."
runImage = "..."
docker {
builderRegistry {
url = "..."
username = project.extra.get("registryUsername") as String
password = project.extra.get("registryPassword") as String
}
publishRegistry {
url = "..."
username = project.extra.get("registryUsername") as String
password = project.extra.get("registryPassword") as String
}
}
}
And since the project should still be able to decide the Spring Boot Version, I don’t want to bind this to a specific plugin version (I’m not sure if it is necessary to keep the versions in sync, tbh, but at least there is one version of the plugin for each version of the framework and we are keeping it in sync in general).
So my first idea was to just add one version of the plugin as a dependency to my config plugin and just do the configuration if this plugin is applied (if (project.plugins.hasPlugin(CLASS))
). This now leads to the issue though, that specifying a version in the downstream project results in a Gradle error, that there is already a dependency with an unknown version on the classpath, so it cannot be resolved.
I then tried to add it as a compileOnly
dependency in the plugin, but that also resulted in a (very unspecific) Gradle error.
Is it somehow possible to achieve what I’m trying to do?
I know, that having it independent from the version might result in issues, when there are breaking changes. But I’m ok with that. If we stumble upon that we would build another release supporting the new version of the plugin and release it including a drop of support for the old version of the plugin. Or, if that’s somehow possible, we could maybe find out which version of the plugin is used by the downstream project and do different configurations based on the version?
Would be great if someone could help me out here. Thanks in advance