Subproject Dependencies on a Custom Plugin in the Root Project

I’m writing a custom plugin and have a few test projects that verify the custom plugin works correctly. The custom plugin is located in the root of a multi-project build where the test projects are subprojects.

My problem is that the build fails because the subprojects can’t find the custom plugin. The subprojects are trying to apply the new plugin before the root project is built.

My feeling is that the solution is something like adding the following to the root’s build.gradle:

subprojects {
 buildscript {
  dependencies {
   classpath project(rootProject.name)
  }
 }
}

The problem here is that the subprojects (located in a directory called "testProjects) don’t recognize the root project (named “gradle-plugins”):

Project with path ‘gradle-plugins’ could not be found in project ‘:testProjects’.

Can someone point me in the right direction?

Hi Mark, I had this same exact issue a while back. I have a plugin which dynamically creates a gradle multi project build by reading a third party project structure and mirroring that in gradle by creating a number of on-the-fly gradle projects corresponding to the third party projects.

I asked this question in IRC and got a reply from one of the gurus (Hans, think it might have been you, but not sure). One way to solve this is to stick a ‘settings.gradle’ file in the root directory of your project structure and then add something like the following code to it:

buildscript {
  repositories {
    mavenRepo name: 'my-plugin-repository',
urls: uri('<repourl>')
  }
    dependencies {
    classpath('<group>:<name>:<version>')
  }
    //This adds the required classes to the settings classloader...which in turn makes the
  //classes accessible by every subproject in this multi project build
  configurations.classpath.each { file -> settings.classLoader.addURL(file.toURI().toURL()) }
}

You should then be able to do a:

subprojects {
     apply plugin: 'myplugin'
   }

in your root (or any other) project without defining any repositories or dependencies for the plugin. They should just be available for all projects including the root one. Let me know if that answers your question and how it works out.

Also I’m not 100% on this but I believe that if you want to use the ‘project(…)’ pattern the way you intended in your code you should stick a colon in front of the root project name, i.e.:

subprojects {
    buildscript {
        dependencies {
            classpath project(":${rootProject.name}")
        }
    }
}

without the colon I believe the project reference will be relative. The colon is kind of like a slash on a *nix file system. Then again, haven’t tested this so might be off here.