In a gradle-plugin-project, how can I apply the plugin itself in the build?


(Tobias Schulte) #1

I want to apply the plugin I am developing in the build script of the plugin. Currently I can imagine doing one of the following:

  • Release version 1.0, than apply this released version to version 1.1-SNAPSHOT (I have seen this in some plugins in github) - develop the plugin in buildSrc, put src/ in .gitignore, copy everything from buildSrc/src to src as first step of the build (This seems to be very ugly)

Are there any other options? What is the preferred and suggested way?


(René Groeschke) #2

I would recommend to publish an initial version of your plugin and then apply that plugin in your buildscript block of your gradle build script (your 1st described approach I guess)


(Tobias Schulte) #3

I just tried another approach. I created a buildSrc folder with nothing but following build.gradle

apply plugin: "groovy"
  dependencies {
 compile gradleApi()
 compile localGroovy()
}
  sourceSets {
 main {
  groovy {
   srcDir '../src/main/groovy'
  }
 }
}

Now I can apply my plugin in the main build.gradle. OK, now my classes get compiled twice (just as with my previous 2nd approach above), but at least now I always get my latest plugin version applied.


(René Groeschke) #4

Hey tobias, thanks for sharing. Just a little note on your code snippet:

When reconfiguring the source directory of a project, I often see this kind of snippet:

sourceSets {
    main {
        groovy {
            srcDir '../src/main/groovy'
        }
    }
}

But what this does is not changing the location of your sources from src/main/groovy to …/src/main/groovy but adding the directory to the preconfigured ones. Often that doesn’t matter, as the former directory (src/main/groovy) in this case is empty. But sometimes this causes side effects in your build.gradle scripts or your custom plugins. usually the snippet above, can be replaced by

sourceSets {
    main {
        groovy {
            srcDirs = ['../src/main/groovy']
        }
    }
}

For more details have a look at the documentation of SourceDirectorySet: http://www.gradle.org/docs/current/javadoc/org/gradle/api/file/SourceDirectorySet.html