Prerequisites
- Java 16+
- Gradle (install from gradle.org/install)
Step-by-step instructions
-
create new directory gradle-multi-project-masterclass
-
cd
into new directory -
run
gradle init
-
accept all defaults except question 2. Choose Kotlin.
-
open project in IDE (I recommend IntelliJ IDEA)
-
add 2 plugins to build.gradle.kts build script
plugins { java id("org.springframework.boot") version "2.6.4" }
-
add Spring Boot dependency to build script
dependencies { implementation("org.springframework.boot:spring-boot-starter-web:2.6.4") }
-
add Maven Central repository to build script
repositories { mavenCentral() }
-
click Load Gradle Changes in IntelliJ IDEA
-
create directories src/main/java
-
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); } }
-
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 :("; } }
-
run application with
./gradlew bootRun
-
access application at localhost:8080
Cool! You now have a working single-project build.
-
create app subproject directory
-
copy src and build.gradle.kts into app
-
in settings.gradle.kts add the following line to include the app subproject
include("app")
-
check project still runs with
./gradlew bootRun
-
view project structure with
./gradlew projects
-
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.