Step-by-step instructions

These instructions follow on from what was achieved in the previous lesson.

Add a convention plugin

  1. create a buildSrc directory

  2. in there add a build.gradle.kts build script

  3. add a single plugin

    plugins {
  4. configure repositories to include Gradle Plugin Portal

    repositories {
  5. in buildSrc add src/main/kotlin directory

  6. add convention plugin script com.tomgregory.maxirail.common-conventions.gradle.kts

    plugins {
    repositories {
    testing {
        suites {
            named("test", JvmTestSuite::class) {
  7. in IntelliJ IDEA’s Gradle Tool Window click Reload All Gradle Projects for IntelliJ IDEA to validate this file

  8. in app’s build script apply the convention plugin by id

  9. remove old build script configuration which was extracted into the convention plugin (i.e. java plugin, repositories, and testing blocks)

  10. repeat for service subproject, but leave the java-library plugin

  11. repeat for model subproject, but leave the java-library plugin

  12. run ./gradlew cleanTest test to ensure tests are still passing

Add a version catalog

  1. under gradle directory create file libs.versions.toml

  2. define Spring Boot version using TOML syntax

    spring-boot = "2.6.4"
  3. define 3 x Spring Boot dependencies, referencing version defined above

    spring-boot-web = { module = "org.springframework.boot:spring-boot-starter-web", version.ref = "spring-boot" }
    spring-boot-test = { module = "org.springframework.boot:spring-boot-starter-test", version.ref = "spring-boot" }
    spring-boot-starter = { module = "org.springframework.boot:spring-boot-starter", version.ref = "spring-boot" }
  4. define Spring Boot plugin, referencing version defined above

    spring-boot = { id = "org.springframework.boot", version.ref = "spring-boot" }
  5. in IntelliJ IDEA’s Gradle Tool Window click Reload All Gradle Projects to make this information available to build scripts

  6. in the build script for app, update dependencies by using the type-safe accessor

    dependencies {
  7. reference the Spring Boot plugin using the alias defined in the version catalog (IntelliJ IDEA may incorrectly show an error in the editor)

  8. in service’s build script use the type-safe accessor to reference spring-boot-starter

    dependencies {
  9. run tests again with ./gradlew test and see that everything is still working

Great! You just used the latest Gradle features to share build logic between multiple subprojects and improve maintainability.

GitHub repository

The gradle-multi-project-masterclass repository contains the final code solution for lessons 2-5.