We have defined our plugin in the project source directory, but we cannot re-use it in another project. We will learn how we can distribute our plugin logic, using a standalone project. Also, we will see how we can use the plugin in other projects.
By placing the plugin code in the
buildSrc
directory, we have separated the definition of the plugin and the usage. The plugin still cannot be used by other projects. To make the plugin reusable, we create a standalone project and create an artifact with the plugin code and publish the artifact to a repository. Other projects can then get the plugin from the repository and use the build logic from the plugin in the project.
We already have the code for the plugin and the test code in the buildSrc
directory (from the previous section). We can copy this code to a new directory with the project for the plugin. In this new directory, we must also create a build.gradle
file. The implicit dependencies and plugin added to a project in the buildSrc
directory must be made explicit in a standalone project.
Let's create a new Gradle project in the directory plugin,
and create the file build.gradle
with the following content:
apply plugin: 'groovy' apply plugin: 'maven' version = 1.0 group = 'sample.infoplugin' archivesBaseName = 'infoplugin' repositories.mavenCentral() dependencies { compile gradleApi() groovy localGroovy() testCompile group: 'junit', name: 'junit', version: '4.10' } uploadArchives { repositories.mavenDeployer { repository(url: 'file:../lib') } }
Next, we create the plugin/src/main/groovy/sample
and plugin/src/test/groovy/sample
directories. We copy the InfoPlugin.groovy
and InfoPluginExtension.groovy
files to the src/main/groovy/sample
directory, and the InfoPluginTest.groovy file
to the plugin/src/test/groovy/sample
directory.
So far we have all the ingredients to create an artifact JAR file with the plugin code. The artifact is deployed to the local ../lib
directory. We can, of course, define any Maven or Ivy repository to deploy the plugin artifact into.
To make sure Gradle can find the plugin, we must provide a properties file in the plugin/src/main/resources/META-INF/gradle-plugins
directory with the name of our plugin. The properties file has a property key implementation-class
with the full class name of the Plugin
class.
We want to name our plugin as info
, so in the plugin/src/main/resources/META-INF/gradle-plugins
directory we create the info.properties
file with the following code:
implementation-class = sample.InfoPlugin
We are ready to create the artifact with the plugin and upload it to our repository. We invoke the
uploadArchives
task, and we get the following output:
$ gradle uploadArchives :compileJava UP-TO-DATE :compileGroovy UP-TO-DATE :processResources :classes :jar :uploadArchives Uploading: sample/infoplugin/infoplugin/1.0/infoplugin-1.0.jar to repository remote at file:../lib Transferring 8K from remote Uploaded 8K BUILD SUCCESSFUL Total time: 3.076 secs
The plugin is now in the repository. To use the plugin, we must create a new Gradle project. We must extend the classpath of this new project, and include the plugin as a dependency. We use the buildscript{}
script block, where we can configure the repository location and a classpath dependency. For our sample we reference the local ../lib
directory. In the dependencies section we set the classpath
configuration to the InfoPlugin
artifact.
The following sample build file contains the definitions:
buildscript { repositories { maven { url 'file:../lib' } } dependencies { classpath group: 'sample.infoplugin', name: 'infoplugin', version: '1.0' } } apply plugin: 'info' info.prefix = 'Gradle version'
Our project now has the info
task from the plugin. We can configure the plugin extension through the info
object or the configuration closure.
If we run the info
task, we get the following output:
18.117.158.165