Extending Gradle: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 19: Line 19:


==In the Project's buildSrc Directory==
==In the Project's buildSrc Directory==
This is a good choice while developing a custom task or a plugin, because it gives a quick feedback loop and allows for in-line debugging.
This is a good choice while developing a custom task or a plugin, because it gives a quick feedback loop and allows for in-line debugging. The buildSrc has a standard Maven layout:
<syntaxhighlight lang='text'>
.
├─ build.gradle
└─ buildSrc
    └─ src
        └─ main
            └─ java
                └─ playground
                    └─ gradle
</syntaxhighlight>


Example of an [https://github.com/ovidiuf/playground/tree/master/gradle/extending-gradle/03-enhanced-task-in-buildSrc enhanced task developed in buildSrc].
Example of an [https://github.com/ovidiuf/playground/tree/master/gradle/extending-gradle/03-enhanced-task-in-buildSrc enhanced task developed in buildSrc].

Revision as of 07:03, 24 September 2020

Internal

Overview

The simplest way to extend Gradle is to write a custom task, which can be declared in-line in the project's build.gradle or in a script plugin, in the projects's buildSrc directory or in can be external to project and provided back to the Gradle runtime as a JAR. More complex behavior that goes beyond the capabilities of a custom task can be implemented as a custom object plugin, which has the same choice in terms of where the code lives.

Extensions Code Location

In-line in build.gradle

Both custom tasks and binary plugins can be fully declared in-line in build.gradle. While declaring simple custom tasks in-line is acceptable, provided that the tasks are not intended for reuse and sharing among other projects, declaring a full binary plugin in-line is, albeit possible, not recommended.

Example of a custom simple task declared in-line in build.gradle.

In-line in a script plugin

A script plugin is a regular Gradle build script that can be imported into another build script. Both custom tasks and "binary" plugin code can be declared in-line in a script plugin.

Example of a custom simple task declared in-line in a script plugin.

In the Project's buildSrc Directory

This is a good choice while developing a custom task or a plugin, because it gives a quick feedback loop and allows for in-line debugging. The buildSrc has a standard Maven layout:

.
├─ build.gradle
└─ buildSrc
    └─ src
        └─ main
            └─ java
                └─ playground
                    └─ gradle

Example of an enhanced task developed in buildSrc.

External to Project

Extension code can live in separate standalone projects that publish JARs. Those JARs can be declared as dependencies by a Gradle project that can then use the custom task or the binary plugin externally developed.

Custom Task

Extending Gradle with Custom Tasks

Custom Binary Plugin

Extending Gradle with Binary Plugins