Plugins are used to extend build capabilities and customize Gradle.

gradle basic 6

Most features, like the ability to compile Java code, are added by plugins.

Using plugins is also the primary mechanism for organizing build logic.

Plugins can provide useful tasks with capabilities such as running code, creating documentation, setting up source files, publishing archives, etc.

Applying a plugin to a project allows the plugin to extend the project’s and Gradle’s capabilities.

For example:

  • The Spring Boot Gradle Plugin, org.springframework.boot, provides Spring Boot support.

  • The Google Services Gradle Plugin, com.google.gms:google-services, enables Google APIs and Firebase services in your Android application.

  • The Gradle Shadow Plugin, com.github.johnrengelman.shadow, is a plugin that generates fat/uber JARs with support for package relocation.

Plugin distribution

Plugins are distributed in three ways:

  1. Core plugins - Gradle develops and maintains a set of Core Plugins.

  2. Community plugins - Gradle’s community shares plugins via the Gradle Plugin Portal.

  3. Local plugins - Gradle enables users to create custom plugins using APIs.

You apply plugins in the build script using their plugin id, a globally unique identifier, or a name:

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

Core plugins

Core Gradle plugins are unique in that they provide short names, such as java for the core JavaPlugin.

To apply the java plugin to a project:

plugins {
    id("java")
}

The Java plugin adds Java compilation along with testing and bundling capabilities to a project.

There are many more Gradle core plugins users can take advantage of.

Community plugins

Gradle has a vibrant community of plugin developers who contribute plugins for various capabilities, such as the Spring Boot Gradle plugin.

To apply the org.springframework.boot plugin to a project:

plugins {
    id("org.springframework.boot") version "3.1.5"
}

The Spring Boot plugin provides Spring Boot support in Gradle. It packages executable JAR or WAR archives and runs Spring Boot Java applications.

The Gradle plugin portal provides an interface for searching and exploring community plugins.

Plugin tasks

Jib is a Gradle plugin for building Docker and OCI images of Java applications.

The Google Jib Gradle plugin is applied as follows:

plugins {
  id("com.google.cloud.tools.jib") version "3.4.0"
}

Since plugins add tasks to your project when applied, you can view the tasks added by the Jib plugin by running ./gradlew tasks:

$ ./gradlew tasks

> Task :tasks

------------------------------------------------------------
Tasks runnable from root project 'plugins'
------------------------------------------------------------

Jib tasks
---------
jib - Builds a container image to a registry.
jibBuildTar - Builds a container image to a tarball.
jibDockerBuild - Builds a container image to a Docker daemon.

As we can see, the jib, jibBuildTar, and jibDockerBuild tasks from the Jib plugin have been added to the project and may be executed.

Consult the Plugin development chapter to learn more.