Extending Gradle: Difference between revisions
Line 49: | Line 49: | ||
{{External|[https://github.com/ovidiuf/playground/tree/master/gradle/extending-gradle/03-enhanced-task-in-buildSrc Custom enhanced task developed in buildSrc]}} | {{External|[https://github.com/ovidiuf/playground/tree/master/gradle/extending-gradle/03-enhanced-task-in-buildSrc Custom enhanced task developed in buildSrc]}} | ||
===Debugging a <tt>buildSrc</tt> Project=== | ===Debugging a <tt>buildSrc</tt> Project=== | ||
<font color=darkgray>TODO</font> | |||
==<span id='External_to_Project'></span>External Standalone Project== | ==<span id='External_to_Project'></span>External Standalone Project== |
Revision as of 23:51, 17 October 2020
External
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
Gradle does not suggest a specific way of writing extensions, such as tasks and plugins. Tasks can be declared in-line using the DSL keywords, or they can be programming using a JVM-based programming language such as Java or Groovy.
In-line in build.gradle
Both custom simple or enhanced tasks and binary plugins can be fully declared in-line in build.gradle. Placing the extension code here has the benefit that it is automatically compiled and included in the classpath of the build script. While declaring simple tasks in-line is acceptable, provided that the task or the plugin are not intended for reuse and sharing among other projects, declaring enhanced tasks or even full binary plugin in-line is, albeit possible, not recommended. The extension code declared in build.gradle is not visible outside the build script and it cannot be reused outside the build script.
Examples:
- Custom simple task declared in-line in build.gradle
- Custom ehanced Java 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 simple or enhanced tasks and "binary" plugin code can be declared in-line in a script plugin.
Examples:
- Custom simple task declared in-line in a script plugin.
- Custom enhanced task declared in-line in a script plugin.
In the Project's buildSrc Directory
The source code for the custom enhanced task or the binary plugin can be placed in the 'buildSrc/src/main/java|groovy|kotlin' subdirectory of the project directory. Gradle will compile and test the extension code and it will automatically make it available on the classpath of the build script. 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' directory is itself a Gradle project and has a standard Maven layout. It could have a build.gradle, but it works without it for simple Java compilation cases that does not require external dependencies. 'buildSrc/build.gradle' starts to become useful when the custom code task being developed requires external dependencies. In that case, the dependencies must be declared in 'buildSrc/build.gradle'.
The extension code placed here is not visible outside the build and it cannot be reused outside the build it defines it.
.
├── build.gradle
├── buildSrc
│ ├── build.gradle
│ └── src
│ └── main
│ └── java
│ └── playground
│ └── gradle
│ └── CustomEnhancedTask.java
└── ...
buildSrc Project Example
Debugging a buildSrc Project
TODO
External Standalone Project
Extension code can live in an external 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.
Examples:
- Custom enhanced task developed in standalone project. The project publishes the JAR in the local Maven repository.
- Custom enhanced task imported from external project. The project gets the JAR with the task from the local Maven repository.
Custom Tasks
Custom Binary Plugin
Extension
A plugin is the typical use case for an extension.