Extending Gradle: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 9: Line 9:
=Extensions Code Location=
=Extensions Code Location=
==In-line in build.gradle==
==In-line in build.gradle==
Both custom tasks and binary plugins can be fully declared in [[build.gradle]]. While declaring simple custom tasks in-line is acceptable, provided that the tasks are not intended to be shared, declaring a full binary plugin in-line is, albeit possible, not recommended.
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 to be shared, declaring a full binary plugin in-line is, albeit possible, not recommended.


==In-line in a script plugin==
==In-line in a script plugin==

Revision as of 05:43, 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 to be shared, declaring a full binary plugin in-line is, albeit possible, not recommended.

In-line in a script plugin

A script plugin is a regular Gradle build script that can be imported into another build script.

In the Project's buildSrc Directory

This is a good choice while developing the custom task or plugin, because it gives a quick feedback loop.

External to Project

Custom Task

https://docs.gradle.org/current/userguide/custom_tasks.html

The simplest way of extending Gradle is write a custom task. The custom task can be declared in-line in the default build script build.gradle, as a simple task. The simple task can also be declared in-line in a separate build script, which is then included from the default build script. The code of the custom task can live in a separate source file, which in turn can be declared in a special area of the Gradle project, or can be share with other projects as part of a library. Such a task is referred to as a enhanced task.

Task. DEPLETE Gradle_Task_TODEPLETE#Explicit_Task_Declaration_.28Custom_Tasks.29

Simple Task

A simple task is defined with an in-line action closure in the build script or another script imported from the build script.

Enhanced Task

An enhanced task implies writing code into a source code file (or files) and providing that bytecode to Gradle. The behavior is built into the task and the task exposes some properties that can be configured from the build script. This is appropriate if the task needs to be shared across different projects.

Custom Binary Plugin

https://docs.gradle.org/current/userguide/custom_plugins.html#custom_plugins

Gradle allows writing custom binary plugins. The source code for the plugin can be declared in-line in the build script or in a script plugin, none of these methods being recommended, in a directory structure under the project's buildSrc directory or in a separate standalone project that publishes a JAR.