Can Get Plugin Classes in Classpath When Using "apply from"?


I’m developing a Gradle Plugin. I have some Groovy classes defined in my plugin. I built the jar for my plugin and am using it in a different project. In my other project, my build.gradle file looks like:

buildscript {
  repositories {
    // point to my jar file
  dependencies {
    classpath group: 'myGroupName', name: 'myPluginJarName', version: '1.0.0'
  apply plugin: 'myPlugin'
apply from: 'otherFile.gradle'

Now in my plugin, I have the class Foo defined in Groovy. If I try to use class Foo at the end of my build.gradle file, it works fine. However, if I try to use foo in otherFile.gradle, I get “unable to resolve class Foo”. It seems that the buildscript classpath isn’t being applied when you use “apply from:”. Is there a workaround for this?

In otherFile.gradle, I have:

def bar = new Foo(); // You get "unable to resolve class Foo". If you put this at the end of build.gradle (and remove the apply from line), it works.

Thanks, Alex

Hi Alex,

For the most part, ‘buildscript {}’ defines the classpath just for that particular script. The exception here is that ‘buildscript {}’ in project build scripts (e.g. ‘build.gradle’) is propagated to child projects. Script plugins (i.e. 'apply from: “«»”) do not participate in this inheritance.

It depends on exactly what you need to do, but the simplest solution in your case might be to move the ‘buildscript {}’ section to ‘otherFile.gradle’.

Yes, that worked. I have apply plugin: ‘myPlugin’ only in my build.gradle file, but I have buildscript { } blocks in both files, so that the classpath is set correctly in both files. Thanks!