Hey, I have issues applying plugin to a project fr...
# plugin-development
r
Hey, I have issues applying plugin to a project from init script - more details in the thread 🧵
The minimal example is available on GitHub, but let me explain it first 🙂 I have the following init script:
Copy code
rootProject {
    buildscript {
        dependencies {
            // just some random plugin, doesn't matter
            classpath("com.coditory.gradle:integration-test-plugin:1.5.0")
        }
    }
    apply(plugin = "com.coditory.integration-test")
}
but it fails with:
Copy code
* What went wrong:
Plugin with id 'com.coditory.integration-test' not found.
It works perfectly OK if I move
apply(plugin = ...)
line from init script to
build.gradle.kts
. It also works if I wrap the line into `afterEvaluate`:
Copy code
afterEvaluate {
    apply(plugin = "com.coditory.integration-test")
}
but it's important for me to apply the plugin before buildscript evaluation. Also, I don't want to add the plugin to init script classpath and apply it like that:
Copy code
import com.coditory.gradle.integration.IntegrationTestPlugin

initscript {
    dependencies {
        classpath("com.coditory.gradle:integration-test-plugin:1.5.0")
    }
}

rootProject {
    apply<IntegrationTestPlugin>()
}
because I need to configure other plugins from my plugin. Given the above config, extensions of the other plugins will be loaded by different classloader and the only way to configure them is to use reflection. Is it expected behavior that my first example fails? If so - is there any way to apply a plugin from init script, before the buildscript evaluation?
Hey, just bumping up my question 😉 It would be awesome if someone could explain me why the following init script:
Copy code
rootProject {
    buildscript {
        dependencies {
            // just some random plugin, doesn't matter
            classpath("com.coditory.gradle:integration-test-plugin:1.5.0")
        }
    }
    apply(plugin = "com.coditory.integration-test")
}
fails with:
Copy code
* What went wrong:
Plugin with id 'com.coditory.integration-test' not found.
Thanks in advance!
t
My reading: you're configuring the buildscript "for when the project will be evaluated", and immediately try to apply a plugin, but the project buildscript's classloader hasn't been initialized yet! (or possibly your buildscript dependency is added too late and the project has already been evaluated without that buildscript dependency)
Try using lifecycle callbacks like
projectLoaded
(to setup the buidscript dependency),
beforeProject
and/or
afterProject
(to apply the plugin)
r
Unfortunately,
projectLoaded
for buildscript dependency and `beforeProject`/`afterProject` for applying the plugin doesn't work 😞 I think the latter hypothesis (about adding buildscript dependency too late) is not the case, because everything it works after moving plugin application to
build.gradle.kts
The only way I've found to make it working is something like that:
Copy code
rootProject {
    buildscript {
        dependencies {
            classpath("com.coditory.gradle:integration-test-plugin:1.5.0")
        }
    }

    pluginManager.withPlugin("base") {
        apply(plugin = "pl.allegro.tech.build.init")
    }
}
but I need at least one other plugin (
base
in the example above) to be applied explicitly by the
build.gradle.kts
😕 my requirement is to apply the plugin even if no other plugins are applied
All I need is a callback, something like
buildscriptClasspathAvailable { ... }
, that can be used to apply imperative plugins from init script. I've submitted a feature request for that: #30891. If this kind of change makes sense for the maintainers, I can offer a pull request 🙂