buildSrc
A folder named buildSrc at the root of a build is a build of its own. Gradle compiles it before it reads any build script, puts its classes on the class path of every script, and from then on a script can apply what it finds there by id. It is the shortest way to share build logic between projects.
What buildSrc is
Three facts make it work, and all three are conventions, not configuration. The folder is at the root and is named buildSrc. It has a build.gradle of its own, which applies the plugin that compiles the build logic. And its output is on the class path of every build script of the main build, so a script says id 'shop.java-conventions' without a version and without a repository.
buildSrc/build.gradle
plugins {
id 'groovy-gradle-plugin'
}
repositories {
mavenLocal()
}
dependencies {
implementation localGroovy()
}
A build script with an id
The groovy-gradle-plugin plugin reads every .gradle file under src/main/groovy and turns it into a plugin: the file name becomes the id, with the folder path in front of it. This one is called shop.java-conventions and it is a build script like any other:
buildSrc/src/main/groovy/shop.java-conventions.gradle
plugins {
id 'java-library'
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
tasks.withType(JavaCompile).configureEach {
options.encoding = 'UTF-8'
options.compilerArgs += ['-Xlint:all', '-Werror']
}
tasks.named('test') {
useJUnitPlatform()
testLogging {
events 'failed'
}
}
Applying it
settings.gradle
rootProject.name = 'shop'
include 'core', 'app', 'cli'
core/build.gradle
plugins {
id 'shop.java-conventions'
}
app/build.gradle
plugins {
id 'shop.java-conventions'
}
dependencies {
implementation project(':core')
}
cli/build.gradle
plugins {
id 'shop.java-conventions'
}
dependencies {
implementation project(':app')
}
core/src/main/java/com/example/Price.java
package com.example;
public final class Price {
public static String format(long cents) {
return String.format("%d.%02d", cents / 100, cents % 100);
}
}
Three build scripts of four lines each. Every setting of the three is in one file, and a change there reaches all of them at the next build.
The first build
The first build is slow, because the build logic has to be compiled first. The tasks prefixed with :buildSrc: are that compile, and they appear before the tasks of the projects:
gradle :core:compileJava --console=plain --offline --no-daemon
> Task :buildSrc:extractPluginRequests
> Task :buildSrc:generatePluginAdapters
> Task :buildSrc:compileJava
> Task :buildSrc:compileGroovy NO-SOURCE
> Task :buildSrc:compileGroovyPlugins
> Task :buildSrc:pluginDescriptors
> Task :buildSrc:processResources
> Task :buildSrc:classes
> Task :buildSrc:jar
> Task :core:compileJava
BUILD SUCCESSFUL in 1m 28s
8 actionable tasks: 8 executed
The second build does not compile the build logic again: Gradle keeps it in buildSrc/build and rebuilds it only when a file of it changes.
Why not subprojects
A root build script can configure its children directly with subprojects { ... }, and it is the shorter text. It is also the slower and more fragile one: the block runs for every project whether it has Java sources or not, it runs in the same file that holds the root project’s own tasks, and a subproject cannot opt out. A precompiled script plugin is a file a project applies, and it says what it is. The next lesson runs both forms on the same build.
Related
- Convention plugins versus subprojects: the same logic as a plugin class, and the comparison.
- A plugin you write: a plugin class with a task and a test of its own.
- The parent pom: the same sharing in Maven, through a file in a repository.
Article author: Arthur Isaev