Plugins and conventions

Gradle · Practical · 10 min

Plugins are how Gradle supports Kotlin, Android, Spring Boot, code quality and publishing.

Gradle itself is small; plugins add tasks, configurations and conventions. Apply them in the plugins { } block, which must be the first block in the script:

plugins {
    java                                        // core plugin, no version
    id("org.springframework.boot") version "3.4.4"
    kotlin("jvm") version "2.1.20"
}

Core plugins (java, java-library, application, maven-publish, jacoco) are bundled. Community plugins are resolved from the Gradle Plugin Portal by default, or from repositories listed in pluginManagement { repositories { } } in settings.gradle.kts. Versions are pinned there or in the version catalog; in a multi-project build, declare a plugin version once (often apply false in the root) and apply it without a version in subprojects.

What plugins give you:

  • java-library adds api and ABI-aware compile avoidance.
  • application adds run and installDist with start scripts.
  • org.springframework.boot adds bootRun and bootJar.
  • com.android.application turns a project into an Android app.
  • jacoco, checkstyle, spotless for coverage and style.

Each plugin has an extension for configuration (java { }, application { }, springBoot { }). Prefer plugins { } over the legacy apply(plugin = ...), which doesn't support type-safe accessors. You can write your own convention plugins in buildSrc or an included build (Advanced).

Example

// settings.gradle.kts
pluginManagement {
    repositories { gradlePluginPortal(); google(); mavenCentral() }
}
rootProject.name = "shop"

// build.gradle.kts
plugins {
    `java-library`
    application
    jacoco
    id("org.springframework.boot") version "3.4.4"
    id("io.spring.dependency-management") version "1.1.7"
}

repositories { mavenCentral() }

application { mainClass = "com.acme.ShopApplication" }

jacoco { toolVersion = "0.8.12" }
tasks.jacocoTestReport { dependsOn(tasks.test); reports { html.required = true } }

// $ ./gradlew tasks   -> now lists bootRun, bootJar, jacocoTestReport, run, installDist

Remember

  • plugins { } must come first; community plugins need a version
  • Core plugins are bundled; others come from the Plugin Portal or configured repositories
  • Each plugin adds tasks and an extension block to configure it
  • Pin plugin versions once per build

Check yourself

  1. Why does id("java") not need a version while id("org.springframework.boot") does?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: docs.gradle.org/current/userguide/plugins.html