Creating a plugin in a standalone project

We have defined our plugin in the project source directory, but we cannot reuse it in another project. We will discuss how to distribute our plugin logic, using a standalone project. Also, we will see how to 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 will 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 plugin directory and also create the build.gradle file with the following content:

apply plugin: 'groovy' 
apply plugin: 'maven' 
 
version = '1.0' 
group = 'sample.infoplugin' 
 
// Set name of archive containing the plugin. 
archivesBaseName = 'infoplugin' 
 
repositories { 
    jcenter() 
} 
 
dependencies { 
    // Define dependency on the Gradle API. 
    compile gradleApi() 
 
    // Define dependency on the Groovy version 
    // bundled with Gradle. 
    compile localGroovy() 
 
    testCompile 'junit:junit:4.12' 
} 
 
uploadArchives { 
    repositories { 
        mavenDeployer { 
            // Define a local directory as the 
            // Maven repository where the plugin 
            // is deployed to. 
            repository(url: 'file:../lib') 
        } 
    } 
} 

Next, we will 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.

To make sure that 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 implementation-class property key 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 will 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 will invoke the uploadArchives task and get the following output:

$ gradle uploadArchives
:compileJava UP-TO-DATE
:compileGroovy
:processResources
:classes
:jar
:uploadArchives
BUILD SUCCESSFUL
Total time: 4.991 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 will reference the local ../lib directory. In the dependencies section, we will set the classpath configuration to the InfoPlugin artifact.

The following sample build file contains the definitions:

buildscript { 
    repositories { 
        maven { 
            // Define local directory lib 
            // as Maven repository. This directory 
            // contains the plugin we build ourselves. 
            url = 'file:../lib' 
        } 
    } 
 
    dependencies { 
        classpath group: 'sample.infoplugin', 
            name: 'infoplugin', 
            version: '1.0' 
    } 
} 
 
// Apply plugin. We can use the value 'info', 
// because we packaged our plugin with the file 
// info.properties. 
apply plugin: 'info' 
 
info { 
    // Set prefix property for InfoPlugin. 
    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 will get the following output:

$ gradle info
:info
Gradle version: 2.10
BUILD SUCCESSFUL
Total time: 0.739 secs
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.47.218