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 {
        `kotlin-dsl`
    }
    
  4. configure repositories to include Gradle Plugin Portal

    repositories {
        gradlePluginPortal()
    }
    
  5. in buildSrc add src/main/kotlin directory

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

    plugins {
        java
    }
    
    repositories {
        mavenCentral()
    }
    
    testing {
        suites {
            named("test", JvmTestSuite::class) {
                useJUnitJupiter()
            }
        }
    }
    
  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

    id("com.tomgregory.maxirail.common-conventions")
    
  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

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

    [libraries]
    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

    [plugins]
    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 {
        implementation(project(":service"))
        implementation(libs.spring.boot.web)
        testImplementation(libs.spring.boot.test)
    }
    
  7. reference the Spring Boot plugin using the alias defined in the version catalog (IntelliJ IDEA may incorrectly show an error in the editor)

    alias(libs.plugins.spring.boot)
    
  8. in service’s build script use the type-safe accessor to reference spring-boot-starter

    dependencies {
        api(project(":model"))
        implementation(libs.spring.boot.starter)
    }
    
  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.