How can I apply a custom plugin, that resides in the buildSrc folder of the root project to all subprojects?

I have a multiproject build. In the buildSrc folder, there is a plugin A, which needs to be applied to all subprojects. I tried applying the plugin like this in the root buildscript:

subprojects {
         apply plugin:'A'

However I get an error, that the plugin with id ‘A’ is not found. So how can I apply the plugin in all subprojects?

It should just find it, but using buildSrc can be a little quirky when it comes to already compiled scripts. That is, if your subproject scripts have already been compiled and cached before buildSrc was created, it may not be on their classpath. Try running it with the “–recompile-scripts” option (which will force the scripts to be recompiled and will ensure that buildSrc is on their classpath) and see if that fixes it.

I tried “–recompile-scripts” without success. When I apply the plugin in a specific project, it works. Here is the project layout (we use ‘includeFlat’ in our settings.gradle file).:

    | - rootProject
     | - buildSrc/src/main/groovy/A.groovy
    | - subProjectA
    | - subProjectB

Do you have a META-INF/gradle-plugins/ I notice in your example you are applying a plugin id (String) rather than A.class…

Thank you very much. Using A.class instead of A did the trick and the plugin is applied to all subprojects. Can you maybe explain, why this is? We also do nto have a META-INF/gradle-plugins/ file. What is this file for?

It’s what creates the linkage between a plugin id and the actual plugin implementation class. Using ‘apply plugin: ‘id’’ requires gradle to determine which class ‘id’ is associated with. On the other hand, using ‘apply plugin: A.class’, you’re telling it specifically what plugin class to use, so it doesn’t ahve to do this resolution. So, for instance, if I wanted to use the plugin id ‘foo’ to apply the class, I would create a file in my jar (or buildSrc) at META-INF/gradle-plugins/

Note that the name of the properties file must match the id that you want to use (ie ‘foo’ ->

1 Like

That works great. Having properties file with the implementation class let’s us use the id instead of the class name