Gradle - Plugins


Advertisements

Plugin is nothing but set of all useful tasks, such as compiling tasks, setting domain objects, setting up source files, etc. are handled by plugins. Applying a plugin to a project means that it allows the plugin to extend the project’s capabilities.

The plugins can do the things such as −

  • Extend the basic Gradle model (e.g. add new DSL elements that can be configured).

  • Configure the project, according to conversions (e.g. add new tasks or configure sensible defaults).

  • Apply specific configuration (e.g. add organisational repositories or enforce standards).

Types of Plugins

There are two types of plugins in Gradle, which are as follows −

  • Script plugins − Script plugins is an additional build script that gives a declarative approach to manipulating the build. This is typically used within a build.

  • Binary plugins − Binary plugins are the classes, that implements the plugin interface and adopt a programmatic approach to manipulating the build. Binary plugins can reside with a build script, with the project hierarchy or externally in a plugin JAR.

Applying Plugins

Project.apply() API method is used to apply the particular plugin. You can use the same plugin for multiple times.There are two types of plugins one is script plugin and second is binary plugin.

Script Plugins

Script plugins can be applied from a script on the local filesystem or at a remote location. Filesystem locations are relative to the project directory, while remote script locations specify HTTP URL.

Take a look at the following code snippet. It is used to apply the other.gradle plugin to the build script. Use this code in build.gradle file.

apply from: 'other.gradle'

Binary Plugins

Each plugin is identified by plugin id. Some core plugins use short names to apply the plugin id and some community plugins use fully qualified name for plugin id. Sometimes, it allows to specify the class of plugin.

Take a look at the following code snippet. It shows how to apply java plugin by using its type. Use the below code in build.gradle file.

apply plugin: JavaPlugin

Take a look into the following code for applying core plugin using short name. Use this code in build.gradle file.

plugins {
   id 'java'
}

Take a look into the following code for applying community plugin using short name. Use this code in build.gradle file.

plugins {
   id "com.jfrog.bintray" version "0.4.1"
}

Writing Custom Plugins

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. Take a look into the following code and use this code in build.gradlebuild.gradle file.

apply plugin: GreetingPlugin

class GreetingPlugin implements Plugin<Project> {
   void apply(Project project) {
      project.task('hello') << {
         println "Hello from the GreetingPlugin"
      }
   }
}

Use the following code to execute the above script.

C:\> gradle -q hello

Output

This produces the following output −

Hello from the GreetingPlugin

Getting Input from the Build

Most of the plugins need the configuration support from the build script. The Gradle project has an associated ExtensionContainer object that helps to track all the setting and properties being passed to plugins.

Let's add a simple extension object to the project. Here, we add a greeting extension object to the project, which allows you to configure the greeting. Use this code in build.gradlefile.

apply plugin: GreetingPlugin

greeting.message = 'Hi from Gradle'

class GreetingPlugin implements Plugin<Project> {
   void apply(Project project) {
      // Add the 'greeting' extension object
      project.extensions.create("greeting", GreetingPluginExtension)
		
      // Add a task that uses the configuration
      project.task('hello') << {
         println project.greeting.message
      }
   }
}

class GreetingPluginExtension {
   def String message = 'Hello from GreetingPlugin'
}

Use the following code to execute the above script −

C:\> gradle -q hello

Output

When you run the code, you will see the following output −

Hi from Gradle

In this example, GreetingPlugin is a simple, old Groovy object with a field called message. The extension object is added to the plugin list with the name greeting. This object, then becomes available as a project property with the same name as the extension object.

Gradle adds a configuration closure for each extension object, so you can group the settings together. Take a look at the following code. Use this code in build.gradle file.

apply plugin: GreetingPlugin

greeting {
   message = 'Hi'
   greeter = 'Gradle'
}

class GreetingPlugin implements Plugin<Project> {
   void apply(Project project) {
      project.extensions.create("greeting", GreetingPluginExtension)
		
      project.task('hello') << {
         println "${project.greeting.message} from ${project.greeting.greeter}"
      }
   }
}

class GreetingPluginExtension {
   String message
   String greeter
}

Use the following code to execute the above script.

C:\> gradle -q hello

Output

The output is mentioned below −

Hello from Gradle

Standard Gradle Plugins

There are different plugins which are included in the Gradle distribution.

Language Plugins

These plugins add support for various languages which can be compiled and executed in the JVM.

Plugin Id Automatically Applies Description
java java-base Adds Java compilation, testing, and bundling capabilities to a project. It serves as the basis for many of other Gradle plugins.
groovy java,groovy-base Adds support for building Groovy projects.
scala java,scala-base Adds support for building Scala projects.
antlr Java Adds support for generating parsers using Antlr.

Incubating Language Plugins

These plugins add support for various languages.

Plugin Id Automatically Applies Description
assembler - Adds native assembly language capabilities to a project.
c - Adds C source compilation capabilities to a project.
cpp - Adds C++ source compilation capabilities to a project.
objective-c - Adds Objective-C source compilation capabilities to a project.
objective-cpp - Adds Objective-C++ source compilation capabilities to a project.
windows-resources - Adds support for including Windows resources in native binaries.
Advertisements