How can I use a custom gradle plugin in a subproject of the same multi-project?

(Patrick) #1

I’m creating a custom gradle plugin, and I want to provide example projects that use the plugin from the same source.

My current multi-project layout is as following:

├── build.gradle
├── examples
│   ├── example1
│   │   ├── ...
│   │   └── build.gradle
│   └── example2
 ├── ...
 └── build.gradle
├── plugin
 ├── build.gradle
│   └── src
   └── main
       ├── ...
       └── resources
           └── META-INF
               └── gradle-plugins
└── settings.gradle

The ‘plugin’ project contains the custom plugin, and in the two example projects I want to use that ‘plugin’.

In the build.gradle files of the examples I tried

apply plugin: 'myplugin'


apply plugin: com.plugin.MyPlugin

But none worked.

I also tried adding the project(‘plugin’) to the example dependencies in ‘compile’ and ‘classpath’ configurations. ‘classpath’ failed because it doesn’t exist as configuration, and ‘compile’ will just fail saying it can’t find ‘myplugin’.

Any ideas?

(Peter Niederwieser) #2

A plugin cannot be used from the same build that builds it. What you can do is to use ‘GradleBuild’ tasks to kick off one or more nested builds for the examples. The example builds can then pull in the plugin using a ‘buildscript’ block and apply it.

(Patrick) #3

hmm… interesting. And the example builds pulling in the plugin using a buildscript block, can that be done directly (file/project based?) or does that need to go via a maven/ivy/… dependency?

Based on your answer I found which gives an indication on how to use the GradleBuild task.

(Peter Niederwieser) #4

If the plugin has external dependencies, it’s probably best to have the main build publish to a ‘file:///’ based maven/ivy repo, and have the example builds pull from there. Otherwise, plain file dependencies should be good enough (see regular dependency management).

(Patrick) #5

Thanks for the superbly fast answering :smiley: Will give that a try

(Patrick) #6

I did not get it to work the way I wanted. Instead I went with just having the examples as separate projects not bound to the plugin project, and have the examples depend on the jcenter dependency instead