Plugins - pford68/gradle-examples GitHub Wiki

Contents

Description

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)
  • Apply specific configuration (e.g. add organizational repositories or enforce standards)
  • By applying plugins, rather than adding logic to the project build script, we can reap a number of benefits.

Applying plugins:

  • Promotes reuse and reduces the overhead of maintaining similar logic across multiple projects
  • Allows a higher degree of modularization, enhancing comprehensibility and organization
  • Encapsulates imperative logic and allows build scripts to be as declarative as possible

Legacy DSL

apply plugin

This is legacy. There is a new DSL in Gradle 3.

Plugin DSL

Gradle 3 DSL

plugins {
  id '<the id>'
} 

plugins {
    id «plugin id» version «plugin version» [apply «false»]
}

Writing Plugins

To create a custom plugin, you need to write an implementation of Plugin. Gradle instantiates the plugin and calls The project object is passed as a parameter to the plugin's apply method. The plugin then usea the project parameter to configure the project.

apply plugin: GreetingPlugin

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

Making The Plugin Configurable

In short, create an extension object. And add it to the project parameter's list of extensions, with the Plugin's apply method.

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

class GreetingPluginExtension {
    String message
    String greeter
}

Using the plugin:

apply plugin: GreetingPlugin

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

The name of the closure block in the build script (greeting) needs to match the extension object name. (In the example above the extension name is "greeting.") Then, when the closure is executed, the fields on the extension object will be mapped to the variables within the closure based on the standard Groovy closure delegate feature.

Testing

Unit Testing

You can use the ProjectBuilder class to create Project instances to use when you test your plugin implementation.

class GreetingPluginTest {
    @Test
    public void greeterPluginAddsGreetingTaskToProject() {
        Project project = ProjectBuilder.builder().build()
        project.pluginManager.apply 'org.samples.greeting'

        assertTrue(project.tasks.hello instanceof GreetingTask)
    }
}

Functional Testing

TBD

References

Testing

⚠️ **GitHub.com Fallback** ⚠️