Convention plugins versus subprojects
Every Gradle build starts with a root script that configures its children, and every such build eventually has a project that does not want what the root says. The subprojects {} block is the first form and the convention plugin is the second; both are below, on the same three projects, and both really run.
The subprojects form
settings.gradle
rootProject.name = 'shop'
include 'core', 'app', 'cli'
build.gradle
subprojects {
apply plugin: 'java'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
tasks.withType(JavaCompile).configureEach {
options.encoding = 'UTF-8'
options.compilerArgs += ['-Xlint:all']
}
}
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);
}
}
core/build.gradle
// No plugins: the root applies java to every subproject.
app/build.gradle
dependencies {
implementation project(':core')
}
app/src/main/java/com/example/App.java
package com.example;
public final class App {
public static void main(String[] args) {
System.out.println("Total: " + Price.format(1999));
}
}
cli/build.gradle
// Nothing to build here yet.
The cli project has no sources and no plugin of its own, and it is still a Java project now. Its tasks say so:
gradle :cli:tasks --group build --console=plain --offline --no-daemon
> Task :cli:tasks
------------------------------------------------------------
Tasks runnable from project ':cli'
------------------------------------------------------------
Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the classes of the 'main' feature.
testClasses - Assembles test classes.
To see all tasks and more detail, run gradle tasks --all
To see more detail about a task, run gradle help --task <task>
BUILD SUCCESSFUL in 18s
1 actionable task: 1 executed
What is wrong with it
- It cannot be opted out of. cli is a folder of scripts today and gets a Java plugin anyway. A project that needs something else has to subtract from the block, which is harder than adding.
- It configures everything at every build. The block runs for each project, even when the command touches none of them, and a big build pays for it on every invocation.
- It is invisible from the project. A reader of core/build.gradle sees a comment and no build. The knowledge of how the project is built lives one folder up, in a file that also holds the root project.
- It is not testable. There is no class to unit-test, no plugin id to apply somewhere else, and no way to publish it.
The convention plugin form
The same settings become a plugin in buildSrc, and every project that wants them applies it by id. The root build script disappears:
buildSrc/build.gradle
plugins {
id 'groovy-gradle-plugin'
}
repositories {
mavenLocal()
}
dependencies {
implementation localGroovy()
}
buildSrc/src/main/groovy/shop.java-conventions.gradle
plugins {
id 'java'
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
tasks.withType(JavaCompile).configureEach {
options.encoding = 'UTF-8'
options.compilerArgs += ['-Xlint:all']
}
build.gradle
// The root project has no build logic of its own.
core/build.gradle
plugins {
id 'shop.java-conventions'
}
app/build.gradle
plugins {
id 'shop.java-conventions'
}
dependencies {
implementation project(':core')
}
cli/build.gradle keeps its comment and gets no Java tasks at all.
The second build
Same projects, same sources, one file of shared logic that a project chooses. The build logic is compiled once, before the build:
gradle :app:build --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
> Task :core:processResources NO-SOURCE
> Task :core:classes
> Task :core:jar
> Task :app:compileJava
> Task :app:processResources NO-SOURCE
> Task :app:classes
> Task :app:jar
> Task :app:assemble
> Task :app:compileTestJava NO-SOURCE
> Task :app:processTestResources NO-SOURCE
> Task :app:testClasses UP-TO-DATE
> Task :app:test NO-SOURCE
> Task :app:check UP-TO-DATE
> Task :app:build
BUILD SUCCESSFUL in 19s
11 actionable tasks: 11 executed
The :buildSrc: tasks at the top are the price of the form: one compile of the build logic, cached until a file of it changes. Everything below is the build of the two projects that applied the plugin.
Related
- buildSrc: the folder the plugin lives in.
- A plugin you write: the same idea as a class, with a task and a test.
- Build and configuration caching: what a block that configures every project costs the second build.
Article author: Arthur Isaev