TExcelTAD
Home › Java Builds: Maven & Gradle › Convention Plugins: buildSrc & Included Builds

Convention Plugins: buildSrc & Included Builds

Every library in a build wants the same things: Java 21, JUnit Platform for tests, the same compiler flags, JaCoCo, perhaps publishing. Copying that block into every build.gradle.kts drifts; putting it in allprojects {} couples everything. The idiomatic answer is a convention plugin: your own small plugin, written as an ordinary Kotlin DSL script, that each project applies by id, exactly like a core plugin:

kotlin
// libs/model/build.gradle.kts
plugins { id("shop.java-library") }

A reader sees immediately which conventions apply, projects stay independent, and changing the convention is one edit.

Precompiled script plugins

A file src/main/kotlin/shop.java-library.gradle.kts in a build-logic project becomes a plugin with the id shop.java-library (the file name minus .gradle.kts). The kotlin-dsl plugin compiles it. Inside, you write what you would have written in a build script, using the string-based APIs or accessors for the plugins it applies itself.

There are two places to put that build-logic project.

Option 1: buildSrc

A folder named buildSrc at the root is built automatically before the main build, and its plugins are on the classpath of every build script:

bash · try it yourself
mkdir -p shop/buildSrc/src/main/kotlin shop/lib/src/main/java/shop/lib shop/lib/src/test/java/shop/lib && cd shop
cat > buildSrc/build.gradle.kts <<'KTS'
plugins { `kotlin-dsl` }
repositories { gradlePluginPortal() }
KTS
cat > buildSrc/src/main/kotlin/shop.java-library.gradle.kts <<'KTS'
plugins {
    `java-library`
}

java {
    toolchain { languageVersion = JavaLanguageVersion.of(21) }
}

repositories { mavenCentral() }

dependencies {
    testImplementation(platform("org.junit:junit-bom:5.14.4"))
    testImplementation("org.junit.jupiter:junit-jupiter")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

tasks.withType<JavaCompile>().configureEach {
    options.compilerArgs.addAll(listOf("-Xlint:all", "-Werror"))
}

tasks.withType<Test>().configureEach {
    useJUnitPlatform()
}
KTS
cat > settings.gradle.kts <<'KTS'
rootProject.name = "shop"
include("lib")
KTS
cat > lib/build.gradle.kts <<'KTS'
plugins { id("shop.java-library") }
KTS
cat > lib/src/main/java/shop/lib/Money.java <<'SRC'
package shop.lib;

public record Money(long cents) {
    public Money plus(Money other) { return new Money(cents + other.cents); }
}
SRC
cat > lib/src/test/java/shop/lib/MoneyTest.java <<'SRC'
package shop.lib;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

class MoneyTest {
    @Test
    void adds() { assertEquals(new Money(350), new Money(100).plus(new Money(250))); }
}
SRC
gradle :lib:build | grep -E '^> Task|BUILD|actionable'
> Task :buildSrc:checkKotlinGradlePluginConfigurationErrors SKIPPED
> Task :buildSrc:generateExternalPluginSpecBuilders
> Task :buildSrc:extractPrecompiledScriptPluginPlugins
> Task :buildSrc:compilePluginsBlocks
> Task :buildSrc:generatePrecompiledScriptPluginAccessors
> Task :buildSrc:generateScriptPluginAdapters
> Task :buildSrc:compileKotlin
> Task :buildSrc:compileJava NO-SOURCE
> Task :buildSrc:compileGroovy NO-SOURCE
> Task :buildSrc:pluginDescriptors
> Task :buildSrc:processResources
> Task :buildSrc:classes
> Task :buildSrc:jar
> Task :lib:compileJava
> Task :lib:processResources NO-SOURCE
> Task :lib:classes
> Task :lib:jar
> Task :lib:assemble
> Task :lib:compileTestJava
> Task :lib:processTestResources NO-SOURCE
> Task :lib:testClasses
> Task :lib:test
> Task :lib:check
> Task :lib:build
BUILD SUCCESSFUL in 23s
13 actionable tasks: 13 executed

The first lines are buildSrc's own build (:buildSrc:compileKotlin, :buildSrc:jar…), then the real build runs with the convention applied: -Xlint:all -Werror turns every warning into an error, and the tests run on the JUnit Platform, without a single line about either in lib/build.gradle.kts.

buildSrc is simple, but it has a cost: any change in it invalidates the configuration of the whole build, because its classes are on every script's classpath.

Option 2: an included build (build-logic)

The recommended layout for larger builds is a separate build, conventionally called build-logic, included as a plugin source in settings.gradle.kts. It behaves like buildSrc but is an ordinary, independent build: it can have several projects, and Gradle only rebuilds what changed.

bash · try it yourself
mkdir -p shop/build-logic/src/main/kotlin shop/app/src/main/java/shop/app && cd shop
cat > build-logic/settings.gradle.kts <<'KTS'
rootProject.name = "build-logic"
dependencyResolutionManagement { repositories { gradlePluginPortal() } }
KTS
cat > build-logic/build.gradle.kts <<'KTS'
plugins { `kotlin-dsl` }
KTS
cat > build-logic/src/main/kotlin/shop.java-app.gradle.kts <<'KTS'
plugins { application }

java { toolchain { languageVersion = JavaLanguageVersion.of(21) } }

tasks.named<JavaExec>("run") {
    jvmArgs("-Dshop.env=dev")
}

val printConventions = tasks.register("printConventions") {
    val release = java.toolchain.languageVersion.map { it.asInt() }
    val main = application.mainClass
    doLast { println("java ${release.get()}, main class ${main.get()}") }
}
KTS
cat > settings.gradle.kts <<'KTS'
pluginManagement { includeBuild("build-logic") }
rootProject.name = "shop"
dependencyResolutionManagement { repositories { mavenCentral() } }
include("app")
KTS
cat > app/build.gradle.kts <<'KTS'
plugins { id("shop.java-app") }
application { mainClass = "shop.app.Main" }
KTS
cat > app/src/main/java/shop/app/Main.java <<'SRC'
package shop.app;

public class Main {
    public static void main(String[] args) {
        System.out.println("env=" + System.getProperty("shop.env"));
    }
}
SRC
gradle -q :app:printConventions :app:run
java 21, main class shop.app.Main
env=dev

app set only its main class; the toolchain, the JVM argument and an extra task all came from the convention. Note that printConventions reads java.toolchain.languageVersion and application.mainClass as providers in the configuration phase and calls .get() only inside doLast, so the value set later in app/build.gradle.kts is the one printed. That is lazy configuration, the topic of the next section.

What belongs in a convention plugin

  • Toolchain, compiler flags, test framework setup, coverage, static analysis, publishing metadata.
  • Not dependencies of the application itself; those stay in each project's build script (or the version catalog).
  • Layer them: shop.java-common ← shop.java-library and shop.java-app, each applying the previous one in its plugins {} block.

Tip: A convention plugin can use the version catalog too, but the generated libs accessor isn't available in precompiled plugins; use the<VersionCatalogsExtension>().named("libs").findLibrary("guava") or, more simply, keep the few versions a convention needs inside it.

Test yourself

What plugin id does src/main/kotlin/acme.java-conventions.gradle.kts in build-logic provide?

  1. java-conventions
  2. acme.java-conventions
  3. kotlin-dsl
  4. build-logic

Why do larger builds prefer an included build-logic build over buildSrc?

  1. buildSrc can't contain Kotlin
  2. It's an independent build: changes don't invalidate the whole build's configuration the way any buildSrc change does
  3. Included builds don't need compilation
  4. buildSrc was removed in Gradle 9

Why are convention plugins preferred to subprojects {} in the root script?

  1. They run faster on the first build only
  2. Each project applies them explicitly, so projects stay decoupled and readers see what applies
  3. subprojects is a Maven feature
  4. They download fewer dependencies