Apply plugin with version catalog

when I use a version catalog to define plugins what would be the correct way to apply the plugin to all subprojects?

currently, it looks like this:
libs.versions.toml

[version]
gradle-plugin-forbiddenapis = "3.3"

[plugins]
forbiddenapis = { id = "de.thetaphi.forbiddenapis", version.ref = "gradle-plugin-forbiddenapis" }

build.gradle

plugins {
    alias(libs.plugins.forbiddenapis) apply false
}

ext {
    subprojectsWithoutPlatform = subprojects.findAll {
        it.name != 'platform' && new File(it.projectDir, "build.gradle").exists()
    }
}

configure(subprojectsWithoutPlatform) {
    apply plugin: libs.plugins.forbiddenapis.get().pluginId
    forbiddenApis {
        ignoreFailures = false
    }
}

so the question is related to apply plugin: libs.plugins.forbiddenapis.get().pluginId, is this the way it should be done or should it be done differently.

Actually practically any subprojects { ... }, allprojects { ... }, project(...) { ... }, configure(listOfProjects) { ... } and similar should be avoided due to various reasons, reaching from being non-idiomatic in current Gradle, over hindering more advanced features like parallel execution or configuration cache by introducing coupling between projects, to decreasing build maintainability.

The way to go is to have convention plugins where you define the common logic and then apply these convention plugins in the concrete build script where you want it to be applied.

2 Likes