Testing custom Gradle plugins

pablisco
2 min readAug 17, 2017

--

Christopher Nolan. (Director). (2010). Inception.

If you are working on a Gradle plugin, you may have come across an interesting issue: “How do I do integration testing?”. The code itself is easy to test since we can use Gradle TestKit. This is great to test the actual logic of your plugin code.

However, at some point you may want to test it with an actual module before publishing it into the wild (it’s always good to test tooling with real projects). Well, the tricky thing here is that the code is inside of a Gradle module so we can’t actually use the plugin on the build graph, since it doesn’t exist until we compile the plugin code. Good old cyclical dependency.

The first idea I had when coming across this, was to deploy the plugin into a local repository and then import it into the script. However, this encounters the same issues when building the project for the first time.

Fear not, as not all is lost. There is a small trick you can use to include the plugin within the same project. This is thanks to the buildSrc folder. Anything inside this folder is compiled before the Gradle graph is evaluated. This special module comes in very handy.

“Pluginception”

Let’s say that we have our plugin module called plugin as a submodule of the root project, we can add these lines inside buildSrc/build.gradle:

sourceSets {    
main {
java.srcDirs += '../plugin/src/main/java'
resources.srcDirs += '../plugin/src/main/resources'
}
}

You will also require the same dependencies as your plugin module. I suggest using a separate script file that can be used on both build scripts.

With this configuration, you can use the plugin as if it was imported using the final product: apply plugin: 'super-awesome-plugin'

Notes

  1. It’s important to know that this will slow down your build significantly because it compiles the plugin as part of the build graph evaluation.
  2. A working example of this can be found here:
    https://github.com/pablisco/ramBuild

--

--