Multi-project build and custom plugin under buildSrc - plugin not found


I have a simple plugin class in groovy in ROOT/buildSrc/src/main/groovy/org/example/Foo.groovy and a build file located at ROOT/sub1/test.gradle, which contains the following:

apply plugin: org.example.Foo

Now if I do a

$ ./gradlew -b test.gradle

in folder sub1, I get this error:

> Could not find property 'org' on project 'sub1'.

If I add an import statement at the top of test.gradle

import org.example.Foo

I get a ClassNotFoundException. I also tried assigning a short name to my plugin (ROOT/buildSrc/src/main/ressources/META-INF/gradle-plugins/ and using this in my apply plugin: statement, but no success either.

What am I missing here? Is it possible at all to provide plugin code in a multi-project build? The same project has custom tasks located in_ ROOT/buildSrc_ and my sub project build files eat them without complaining…

EDIT: I verified file ROOT/buildSrc/build/libs/buildSrc.jar; it contains my plugin class files and my META-INF/gradle-plugins/ folder.

EDIT: Ok I have some progress here - if I do a apply from: 'test.gradle' in file sub1/build.gradle it seems that my plugin is consumed. However, I would like to avoid having to tightly couple test.gradle to build.gradle. How can I achieve this?


If you use the -b option, then the subproject will be evaluated as a standalone project, not as part of the multi-project build. That’s why it doesn’t see anything from buildSrc.

Why do you have build.gradle and test.gradle? You should only have one build file per project, possibly applying some script plugins, e.g. apply from: 'test.gradle'.

Hi Stefan,

thanks for your answer. My original purpose for a separate file was to “abuse” gradle for scripted startup of a test environment, and I did not want to have this in the build file because I don’t see it as part of the build - test.gradle contains user specific configuration and logic which I do not want to commit to the version control. However, I meanwhile have also adopted that apply from: clause and I am on my way tackling the issue with the user-specific parts.

Hey Felix,

going with apply from: would be the right way to go for user-specific scripting, too. You can just have an if-statement in your build like this:

if (file('test-script.gradle')) {
  apply from: 'test-script.gradle'
1 Like