Applying plugin to specific subprojects doesn't work with custom plugin

I’m working on converting a large set of Maven-built projects to Gradle. I wrote a custom Gradle plugin to replace a custom Maven plugin.

I’m now converting the Maven build of a “sample” multi-project build to Gradle. I got it working with redundant information in each subproject’s build.gradle file. I then proceeded to refactor the build, putting repeated configuration into the root build.gradle file. For instance, I was able to apply the java and maven plugins in all the subprojects, and remove those lines from each of the subprojects.

This was all working fine until I tried to add a “configuration” block to apply my custom plugin to only a specific set of subprojects. This is failing, saying that it can’t find this plugin. I then tried to duplicate my required “buildscript” block in the “configuration” block, but that made no difference.

The following is my root “build.gradle”:

subprojects {
  buildscript {
repositories {
  jcenter()
  mavenCentral()
  mavenLocal()
}

dependencies {
  classpath "com.att.opnfv.yang:GradleYangPlugin:1.0.0-SNAPSHOT"
}
  }

  apply plugin: 'maven'
  apply plugin: 'java'

  repositories {
mavenLocal()
mavenCentral()
jcenter()
maven { url "http://oss.sonatype.org/content/repositories/releases/" }
maven {
  url "http://nexus.opendaylight.org/content/repositories/opendaylight.snapshot"
}
maven {
  url "http://nexus.opendaylight.org/content/repositories/opendaylight.release"
}
  }

  configurations {
yangfiles
  }

  ext {
yangGenDir    = "${project.buildDir}/yang-gen-sal"
  }

  dependencies {
testCompile 'junit:junit:4.11'
yangfiles "org.opendaylight.controller:config-api:0.3.0-SNAPSHOT"
yangfiles "org.opendaylight.controller:sal-binding-config:1.2.0-SNAPSHOT"
  }
}

configure([project(":sample-toaster"), project(":toaster-consumer"),
       project(":toaster-provider")]) {
  apply plugin: 'com.att.opnfv.yang.gradle'
  yang {
inspectDependencies true
  }
}

When I attempt to run any task (started with “clean”), it fails on the last “apply” line, saying “Plugin with id ‘com.att.opnfv.yang.gradle’ not found.”.

This “apply” call is working fine in the subprojects’ build.gradle files, even without the redundant “buildscript” block that you can see near the top of this.

Again, I also tried duplicating the “buildscript” block inside that “configuration” block, but that made no difference.

Try to declare the buildscript dependencies only once (on the root project). that should do the trick

Curious. That worked. The “buildscript” block worked fine in the"subprojects" block, but it didn’t work in the “configuration” block. What’s the difference?

I think it is a classloading issue as you declare the buildscript block for subprojects, but use it later in the configure method. The configure block lives in the root project and does not see classes added to subprojects classpath.