Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Gradle Plugins

custom_plugins

QUESTION

  1. Give a Example of standalone plugin.
  2. How to create standalone plugin

A Gradle plugin packages up reusable pieces of build logic, which can be used across many different projects and builds. Gradle allows you to implement your own plugins, so you can reuse your build logic, and share it with others.

You can implement a Gradle plugin in any language you like, provided the implementation ends up compiled as JVM bytecode. In our examples, we are going to use Java as the implementation language for standalone plugin project and Groovy or Kotlin in the buildscript plugin examples. In general, a plugin implemented using Java or Kotlin, which are statically typed, will perform better than the same plugin implemented using Groovy

Writing a simple plugin
To create a Gradle plugin, you need to write a class that implements the Plugin interface. When the plugin is applied to a project, Gradle creates an instance of the plugin class and calls the instance’s Plugin.apply() method. The project object is passed as a parameter, which the plugin can use to configure the project however it needs to. The following sample contains a greeting plugin, which adds a hello task to the project.

class GreetingPlugin implements Plugin<Project> {
    void apply(Project project) {
        project.task('hello') {
            doLast {
                println 'Hello from the GreetingPlugin'
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
// Apply the plugin
apply plugin: GreetingPlugin
Enter fullscreen mode Exit fullscreen mode
Output of gradle -q hello
> gradle -q hello
Hello from the GreetingPlugin
Enter fullscreen mode Exit fullscreen mode

Making the plugin configurable
Most plugins offer some configuration options for build scripts and other plugins to use to customize how the plugin works. Plugins do this using extension objects. The Gradle Project has an associated ExtensionContainer object that contains all the settings and properties for the plugins that have been applied to the project. You can provide configuration for your plugin by adding an extension object to this container. An extension object is simply an object with Java Bean properties that represent the configuration.

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.

abstract class GreetingPluginExtension {
    abstract Property<String> getMessage()

    GreetingPluginExtension() {
        message.convention('Hello from GreetingPlugin')
    }
}

class GreetingPlugin implements Plugin<Project> {
    void apply(Project project) {
        // Add the 'greeting' extension object
        def extension = project.extensions.create('greeting', GreetingPluginExtension)
        // Add a task that uses configuration from the extension object
        project.task('hello') {
            doLast {
                println extension.message.get()
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
apply plugin: GreetingPlugin
Enter fullscreen mode Exit fullscreen mode

// Configure the extension
greeting.message = 'Hi from Gradle'

Output of gradle -q hello
> gradle -q hello
Hi from Gradle
Enter fullscreen mode Exit fullscreen mode

Working with files in custom tasks and plugins
When developing custom tasks and plugins, it’s a good idea to be very flexible when accepting input configuration for file locations. You should use Gradle’s managed properties and project.layout to select file or directory locations. By this, the actual location will only be resolved when the file is needed and can be reconfigured at any time during build configuration.

abstract class GreetingToFileTask extends DefaultTask {

    @OutputFile
    abstract RegularFileProperty getDestination()

    @TaskAction
    def greet() {
        def file = getDestination().get().asFile
        file.parentFile.mkdirs()
        file.write 'Hello!'
    }
}

def greetingFile = objects.fileProperty()

tasks.register('greet', GreetingToFileTask) {
    destination = greetingFile
}

tasks.register('sayGreeting') {
    dependsOn greet
    doLast {
        def file = greetingFile.get().asFile
        println "${file.text} (file: ${file.name})"
    }
}

greetingFile.set(layout.buildDirectory.file('hello.txt'))
Enter fullscreen mode Exit fullscreen mode
Output of gradle -q sayGreeting
> gradle -q sayGreeting
Hello! (file: hello.txt)
Enter fullscreen mode Exit fullscreen mode

In this example, we configure the greet task destination property as a closure/provider, which is evaluated with the Project.file(java.lang.Object) method to turn the return value of the closure/provider into a File object at the last minute. You will notice that in the example above we specify the greetingFile property value after we have configured to use it for the task. This kind of lazy evaluation is a key benefit of accepting any value when setting a file property, then resolving that value when reading the property.

A standalone project
Now we will move our plugin to a standalone project so that we can publish it and share it with others. This project is simply a Java project that produces a JAR containing the plugin classes. The easiest and the recommended way to package and publish a plugin is to use the Java Gradle Plugin Development Plugin. This plugin will automatically apply the Java Plugin, add the gradleApi() dependency to the api configuration, generate the required plugin descriptors in the resulting JAR file and configure the Plugin Marker Artifact to be used when publishing. Here is a simple build script for the project.

plugins {
    id 'java-gradle-plugin'
}

gradlePlugin {
    plugins {
        simplePlugin {
            id = 'org.example.greeting'
            implementationClass = 'org.example.GreetingPlugin'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Give a Example of standalone plugin

As part of enabling Google APIs or Firebase services in your Androidapplication you may have to add the google-services plugin to yourbuild.gradlefile: The google-servicesplugin has two main functions: 1. Process the google-services.json file and produce Android resources that can be used in your application's code. See Adding the JSON Filemore inf.

Image description


buildSrc/build.gradle
plugins {
    id 'groovy-gradle-plugin'
}
Enter fullscreen mode Exit fullscreen mode

** How to create standalone plugin**
how-to-write-a-standalone-plugin

Top comments (0)