Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding local plugin to a Gradle project

Tags:

java

gradle

I have a Gradle plugin that compiles and works as expected. I would like to distribute with the source code an example application using the Gradle plugin which would also allow me to test changes to the plugin easily. Now to do this I must add a classpath dependency to the buildScript block. Is it possible to add a dependent local plugin that will be compiled with the example project? The main issue I'm having now is that the plugin does not exist when trying to sync the example project resulting in a failure.

like image 521
Bobbake4 Avatar asked Feb 09 '16 21:02

Bobbake4


People also ask

How do I add plugins to build Gradle?

While creating a custom plugin, you need to write an implementation of plugin. Gradle instantiates the plugin and calls the plugin instance using Plugin. apply() method. The following example contains a greeting plugin, which adds a hello task to the project.

When Gradle plugins are applied to a project?

Applying a plugin to a project allows the plugin to extend the project's capabilities. It can do things such as: Extend the Gradle model (e.g. add new DSL elements that can be configured) Configure the project according to conventions (e.g. add new tasks or configure sensible defaults)

Which are the two types of plugins in Gradle?

There are two general types of plugins in Gradle, script plugins and binary plugins.


2 Answers

If writing integration tests is your eventual goal, I still recommend using ProjectBuilder. You can see an example of this in Section 39.5.4 here. There are a lot more real-world test implementation examples in gradle source. See here for instance.


That being said, I got curious about the literal question you posted and tried a few things. What did work for me is this:

buildscript{     repositories{         ...     }      dependencies{         classpath files('relative/path/to/plugin.jar')     } }  apply plugin: fully.qualified.package.PluginClassName 

Note that the class name is not enclosed in 'quotes' to make it a string.

This is not very useful since this declaration requires the plugin.jar to be built and available before the project consuming the plugin is built - but at this point, you might as well be depending on an external repo dependency.

A second problem with this is that the transitive dependencies of the plugin project are not added to your plugin-consumer project automatically.

I couldn't get a project dependency like classpath project(':plugin-project') to work inside buildscript.


One (extremely hacky) way to ensure that the plugin project is always build and available while building the plugin-consumer project is to have a "parent" build.gradle that always builds the plugin first before building the consumer project. I uploaded an example here.

like image 118
RaGe Avatar answered Sep 25 '22 05:09

RaGe


I encounter when i want to debug my gradle plugin in Android Studio, I solve it finally.

First, upload your gradle plugin to local maven.

apply plugin: 'java' apply plugin: 'groovy' apply plugin: 'maven'  dependencies {     compile fileTree(dir: 'libs', include: ['*.jar'])     compile gradleApi() }  sourceCompatibility = "1.7" targetCompatibility = "1.7"   group='com.egos.gradle.plugins' name='plugintest' version='0.0.1'  uploadArchives {     repositories {         mavenDeployer {             repository(url: uri('../repo'))         }     } } 

execute gradle uploadArchives . Then use local plugin.

buildscript {     repositories {         maven {             url uri('../repo')         }     }     dependencies {         classpath group: 'com.egos.gradle.plugins',                   name: 'plugintest',                   version: '0.0.1'     } }  apply plugin: 'com.egos' 
like image 36
Egos Zhang Avatar answered Sep 24 '22 05:09

Egos Zhang