Prerequisites

Step-by-step instructions

  1. create new directory gradle-multi-project-masterclass

  2. cd into new directory

  3. run gradle init

  4. accept all defaults except question 2. Choose Kotlin.

  5. open project in IDE (I recommend IntelliJ IDEA)

  6. add 2 plugins to build.gradle.kts build script

    plugins {
        java
        id("org.springframework.boot") version "2.6.4"
    }
    
  7. add Spring Boot dependency to build script

    dependencies {
        implementation("org.springframework.boot:spring-boot-starter-web:2.6.4")
    }
    
  8. add Maven Central repository to build script

    repositories {
        mavenCentral()
    }
    
  9. click Load Gradle Changes in IntelliJ IDEA

  10. create directories src/main/java

  11. create MaxiRailApplication.java in src/main/java/com/tomgregory/maxirail

    package com.tomgregory.maxirail;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class MaxiRailApplication {
        public static void main(String[] args) {
            SpringApplication.run(MaxiRailApplication.class);
        }
    }
    
  12. create MaxiRailController.java in src/main/java/com/tomgregory/maxirail/controller

    package com.tomgregory.maxirail.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class MaxiRailController {
        @GetMapping(path = "/")
        public String getTimes() {
            return "No train times available right now :(";
        }
    }
    
  13. run application with ./gradlew bootRun

  14. access application at localhost:8080

    Cool! You now have a working single-project build.

  15. create app subproject directory

  16. copy src and build.gradle.kts into app

  17. in settings.gradle.kts add the following line to include the app subproject

    include("app")
    
  18. check project still runs with ./gradlew bootRun

  19. view project structure with ./gradlew projects

  20. click Load Gradle Changes again for IntelliJ IDEA to pick up new multi-project structure

Congrats! You now have a multi-project build.

GitHub repository

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