Quick revision
The short answer
We create an included build called build-logic that applies the kotlin-dsl plugin, add a precompiled script plugin such as src/main/kotlin/myorg.java-conventions.gradle.kts containing the Java toolchain, test and Checkstyle settings, include build-logic from the root settings file with pluginManagement { includeBuild("build-logic") }, and apply it in each module with plugins { id("myorg.java-conventions") }. Each project then opts in explicitly, which works better with the configuration cache and project isolation than one script configuring every project, and the plugin can be tested and versioned like any other code.
What to remember
In this answer, we replaced a subprojects block with a convention plugin. We created a build-logic included build with the kotlin-dsl plugin, wrote myorg.java-conventions.gradle.kts with the toolchain, test and Checkstyle settings, included build-logic through pluginManagement in the root settings, and applied the plugin by id in each module. The result makes configuration explicit per project, gives type-safe accessors, works with the configuration cache and project isolation, and can be tested and versioned like code.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We create an included build called build-logic that applies the kotlin-dsl plugin, add a precompiled script plugin such as src/main/kotlin/myorg.java-conventions.gradle.kts containing the Java toolchain, test and Checkstyle settings, include build-logic from the root settings file with pluginManagement { includeBuild("build-logic") }, and apply it in each module with plugins { id("myorg.java-conventions") }. Each project then opts in explicitly, which works better with the configuration cache and project isolation than one script configuring every project, and the plugin can be tested and versioned like any other code.
Why this question is asked
Almost every multi-module Gradle build starts with a subprojects { } block in the root script, because it is the quickest way to give every module the same settings. Over time it becomes the place where everything lands, it hides where settings come from, and it fights the direction Gradle is moving in. Interviewers ask this as a coding question because the migration is concrete and shows whether we know how Gradle loads build logic. In this answer, we will look at the block we start with and its problems, create the build-logic build, write the convention plugin, apply it in the modules, and then see why the result is better.
1The Block We Start With
Here is a typical root build.gradle.kts. It configures Java 25, JUnit 5 and Checkstyle for every module.
Notice the awkward forms: apply(plugin = ...), extensions.configure<...>, and string names like "testImplementation". They are needed because inside subprojects the Kotlin compiler cannot know which plugins each project has, so no type-safe accessors are generated. That is the first sign we are fighting the tool.
The bigger problems are structural. The root script reaches into every other project, so a module's build file no longer tells the whole story: a reader of core/build.gradle.kts cannot see that Checkstyle is on. Every module gets the same settings, including ones that should not, such as a documentation module that has no Java code. And configuring one project from another's script is exactly what the configuration cache and project isolation discourage, because each project's configuration should depend only on its own script and the plugins it applies.
2Create the build-logic Build
build-logic is a small, separate Gradle build inside our repository. It produces plugins instead of an application. Using an included build rather than the special buildSrc directory is the usual recommendation today: it behaves like any other build, can be split into several projects, and its settings are under our control. buildSrc works in a very similar way, and the plugin code below would be the same.
- app/build.gradle.ktsplugins {
id("myorg.java-conventions")} - settings.gradle.kts
pluginManagement{includeBuild("build-logic")} - build-logickotlin-dsl compiles the script into a plugin
- myorg.java-conventions.gradle.ktsthe settings, written once
4 layers, from the top down. Step through them one at a time.
The kotlin-dsl plugin is what turns a Kotlin script file into a real plugin. Each file named something.gradle.kts in src/main/kotlin becomes a precompiled script plugin whose id is the file name without the extension. The includeBuild call inside pluginManagement tells the main build to look in build-logic when a plugins block asks for an id it does not know.
3Write the Convention Plugin
Now we move the content of the subprojects block into a file. Because the plugin applies java and checkstyle in its own plugins block, all the type-safe accessors are available inside it.
Compare it with the block we started from. It is the same configuration, but written in the plain style of a normal build script: java { }, checkstyle { } and testImplementation(...) instead of extensions.configure and strings. If a module needs a library plugin later, we can add a second file, myorg.java-library-conventions.gradle.kts, that applies this one and adds java-library and publishing.
4Apply It in Each Module and Remove the Block
Each module now asks for the conventions explicitly, and the root script no longer configures anyone.
Notice that the docs module simply does not apply the plugin. With subprojects, we would have needed a condition in the root script to exclude it. Now the decision is visible in each module's own file, which is the main readability win.
5Why the Convention Plugin Is Better
First, configuration is explicit and local. Anyone reading a module's build file sees which conventions apply, and an IDE can jump straight into the plugin source. Second, each project is configured only by its own script and the plugins it applies. That is the model the configuration cache and project isolation are built on, so the build can use them as they mature, and Gradle can configure projects in parallel when isolation is enabled.
Third, the plugin is code like any other. It is reviewed, it can be tested with Gradle TestKit by running a small sample build against it, and if it moves to its own repository it can be published with a version, so 40 repositories can share the same conventions and upgrade on purpose. A change in a subprojects block, by contrast, is untested and takes effect everywhere at once.
| Aspect | subprojects { } | Convention plugin |
|---|---|---|
| Where settings come from | Hidden in the root script | Named in each module's plugins block |
| Type-safe accessors | No, string names | Yes |
| Opting out a module | Conditions in the root script | Do not apply the plugin |
| Configuration cache, isolation | Cross-project configuration | Each project configures itself |
| Testing and versioning | Not practical | TestKit tests, publishable |
6Follow-up questions
Why does a change in buildSrc or build-logic slow the next build?
The plugin is on the classpath of every build script that uses it, so changing it means recompiling the plugin and treating those scripts as changed, which invalidates their cached configuration. With an included build, we can split conventions into several small projects so that a change only affects the builds that apply that part. This is one reason teams prefer build-logic over a single large buildSrc.
How do I use the version catalog inside a precompiled script plugin?
The generated libs accessors of the main build are not available inside build-logic by default. A common approach is to look the catalog up through the VersionCatalogsExtension, for example extensions.getByType<VersionCatalogsExtension>().named("libs"), and read versions from it, after making the catalog available to build-logic in its settings file. Some teams use a small community workaround to get type-safe accessors; check what your Gradle version supports.
When would you write the plugin as a class instead of a script?
When the logic gets complex: options exposed through a custom extension, conditional behaviour, or code we want to unit test in detail. A class implementing Plugin<Project> registered with the java-gradle-plugin plugin gives full control. For plain shared configuration, a precompiled script plugin is shorter and easier to read.
7Conclusion
In this answer, we replaced a subprojects block with a convention plugin. We created a build-logic included build with the kotlin-dsl plugin, wrote myorg.java-conventions.gradle.kts with the toolchain, test and Checkstyle settings, included build-logic through pluginManagement in the root settings, and applied the plugin by id in each module. The result makes configuration explicit per project, gives type-safe accessors, works with the configuration cache and project isolation, and can be tested and versioned like code.
Open with
I would move the settings out of the root script into a convention plugin. First I create an included build, build-logic, with its own settings file and a build file that applies the kotlin-dsl plugin and uses the Gradle plugin portal as a repository.
Then, if they want more
Then in build-logic/src/main/kotlin I add a precompiled script plugin called myorg.java-conventions.gradle.kts. Its id comes from the file name.
Inside it, I apply java and checkstyle in a plugins block and write exactly what the subprojects block did: the Java toolchain set to 25, checkstyle with a tool version and max warnings zero, tasks.withType Test configureEach with useJUnitPlatform, and the JUnit and platform launcher test dependencies.
Because the plugins are applied in the script's own plugins block, I get type-safe accessors instead of string names. In the root settings file I add pluginManagement includeBuild build-logic, delete the subprojects block, and in each module write plugins id myorg.java-conventions. Modules like docs simply do not apply it.
The benefits are that each module shows which conventions it uses, each project configures only itself, which is what the configuration cache and project isolation need, and the plugin is real code: reviewed, testable with TestKit, and publishable with a version if other repositories want it.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- JUnit 5, Java 25
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Gradle user manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Show how you would replace a 'subprojects { ... }' block that configures Java version...": https://scrivio-interview-library.netlify.app/java/gradle/9189cbb297/