I have an init script to apply a project plugin wi...
# community-support
p
I have an init script to apply a project plugin with
lifecycle.afterProject
. But the project does not find the plugin this way, but it finds the plugin if I apply it directly in my build.gradle.kts script.
Copy code
// init.gradle.kts
settingsEvaluated {
    pluginManagement {
        repositories {
            maven(url = "<https://maven.pkg.github.com/hfhbd/extract-publications>") {
                name = "GitHubPackages"
                credentials(PasswordCredentials::class)
            }
            gradlePluginPortal()
        }
        plugins {
            id("io.github.hfhbd.extract-publications") version "0.0.3"
            id("io.github.hfhbd.extract-publications.root") version "0.0.3"
        }
    }
}

lifecycle.afterProject {
    if (isolated.buildTreePath == ":") {
        if (this == rootProject) {
            pluginManager.apply("io.github.hfhbd.extract-publications.root")
        }

        pluginManager.withPlugin("maven-publish") {
            pluginManager.apply("io.github.hfhbd.extract-publications")
        }
    }
}
If I use this initscript, it fails with
Plugin with id 'io.github.hfhbd.extract-publications.root' not found.
But when I apply it manually in the root project, it does not fail:
Copy code
// build.gradle.kts
plugins {
    id("io.github.hfhbd.extract-publications.root")
}
v
Well, you nowhere add the plugin to the classpath, so how do you expect it to be found? Having a plugin in the
plugins { ... }
block does add it to the classpath and if not using
apply false
applies it.
p
Thank you! I didn’t think about that. I thought using the plugin and pluginManagement options would be enough. Now I switched back to the buildscript block and it does work as expected.
v
The
pluginManagement { plugins { ... } }
block only defines default versions if the plugin is used in a
plugins { ... }
block later on without a version, but it does not add anything anywhere.
p
Yeah, makes sense. I normally don’t use the plugins block in pluginManagement, but I failed to use the settings plugins block in settingsEvaluated to add the plugin to the settings classpath. That’s why I use the setting’s classpath directly.
v
Yeah, you cannot use
plugins
blocks like that.
plugins
blocks are extracted before the script in question is compiled. This is then used to get these plugins for compiling the build script. Additionally the extracted block is applied to a dummy project to find out which type-safe accessors to generate and also add those to the compile classpath for the script.