Skip to content

Instantly share code, notes, and snippets.

@ksaua
Last active October 15, 2018 20:28
Show Gist options
  • Save ksaua/74d75901458235f48da1 to your computer and use it in GitHub Desktop.
Save ksaua/74d75901458235f48da1 to your computer and use it in GitHub Desktop.
Gradle plugin which uses itself
// Let's say we have a gradle plugin. Now we want that gradle plugin project to actually use the exact plugin.
// There are probably multiple ways we can bootstrap this. One way is to build the jar, and use that, but that's boring.
// Here we show how we can have the plugin-project use the actual plugin we are building.
// :: We load the groovy script on runtime, only then are we able to apply the plugin
// Setup the groovy classpath. Start with our own files
def classpaths = [file('src/main/groovy').absolutePath, file('src/main/resources').absolutePath]
// The groovy script engine wants a string array
String[] classpathArray = classpaths.toArray(new String[classpaths.size()]);
// Start a groovy script engine with our classpaths
def engine = new GroovyScriptEngine(classpathArray, this.getClass().getClassLoader())
// Now we can load the plugin script
def pluginClass = engine.loadScriptByName('org/example/MyPlugin.groovy')
apply plugin: pluginClass
class MyPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
// Here you do whatever you normally want in your plugin
}
}
@ysb33r
Copy link

ysb33r commented Nov 25, 2014

Can you provide a good use case for doing this, rather than actually building the jar?

The only possibility I can possibly see is actually using the plugin to build the plugin.

@ksaua
Copy link
Author

ksaua commented Nov 26, 2014

The use case is simple: When the plugin uses itself.

At work we have a plugin which sets up a bunch of default configuration.
For example:

  • For eclipse we use 'build-eclipse' for its output path (instead of 'bin').
  • We force javac to use UTF-8 when compiling on windows (default is something else, can't quite remember)
  • We automatically setup pmd/checkstyle/findbugs with a common configuration

The plugin which houses these configurations is itself a gradle project, so it should also use the same default configuration.

Say I update the checkstyle configuration to be a bit more strict and that I in the same commit I actually violated the new checkstyle configuration.

If I depend on a jar file it will use an old version of the checkstyle configuration and thus it will not fail the build until I depend on the a new jar.

If I use the approach specified above it will fail the build immediately!

@ysb33r
Copy link

ysb33r commented Nov 26, 2014

Pretty awesome - just modified unofficial bintray plugin to publish itself! https://github.com/ysb33r/bintray

@jwermuth
Copy link

Awesome. I have spend hours figuring out how to do this. My use case is for testing the plugins i write. Thanks for sharing.

@tomerd
Copy link

tomerd commented Apr 12, 2018

<3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment