Skip to content

Latest commit

 

History

History

local-script-plugin-example

Local Script Plugin Example

This Gradle project shows how the build logic can be extracted out of main build.gradle build script into local script plugin as described in the following lecture.

You would want to apply this kind of refactoring if build.gradle script contains more than one group of tasks. Even better option is to extract logic to buildSrc or if the tasks needs to be reused into external binary plugin. Still in case that you are working on a PoC, your project isn't that complex, or you just don't have time to invest into proper plugin you can extract build logic to local Gradle script files to separate build logic.

This example builds on top of previous project that was used for creating Gradle tasks.

Compared to the task assignment all the logic is extracted to gradle/filesPlugin.gradle file. Only thing being done in the main build.gradle script file is to apply local script plugin filesPlugin.

Local Script Plugin

Gradle build script invocation

Default

By default, always build.gradle in the root of the project will be processed by Gradle.

Specify build file in settings.gradle

You can define project build filename in settings.gradle file.

settings.gradle

rootProject.buildFileName = 'gradle/filesPlugin.gradle'

Specify build file over an CLI option (Deprecated)

To execute taskPlugin script file directly use following command (Will be deprecated in 8.0)

./gradlew sF -b gradle/filesPlugin.gradle

Resources

Script Plugins (Gradle Userguide)

Gradle CLI - Environment Options (Gradle Userguide)

ProjectDescriptor (Gradle API Documentation)

Examples

Public GitHub examples using local Gradle script plugins:

Project Description
kurron/open-guide-jvm-builds Main gradle.build file uses Gradle scripts inside of gradle/ directory. There are scripts that set up Spring Boot framework, Spock testing, AWS libraries for cloud computing, Cucumber and many more.
mapbox/mapbox-events-android Example of mapbox local script plugin applications. It collects data and location information from the mobile device when using the mapbox service. Main build.gradle uses Gradle scripts inside of gradle/ directory.

You can observe chained references of build:
scriptsmapbox-events-android/libcore/build.gradle --> gradle/publish.gradle --> gradle/artifact-settings.gradle

Reusing one scrip plugin from multiple scripts

line/armeria Microservice framework for multiple protocols application frameworks. Example of a very complex setup for local gradle scripts of line/ameria project. Main build.gradle applies only one local Gradle script /gradle/scripts/build-flags.gradle, which then does a heavy lifting.