Step-by-step instructions
These instructions follow on from what was achieved in the previous lesson.
Add model subproject
-
in settings.gradle.kts add model to list of subprojects to include
include("app", "model")
-
see new subproject by running
./gradlew projects
-
add model directory
-
add build.gradle.kts in model directory
-
in build script apply java-library plugin
plugins { `java-library` }
-
in model add directories src/main/java
-
add com.tomgregory.maxirail.model package
-
add TrainTime.java class
package com.tomgregory.maxirail.model; import java.time.LocalTime; public record TrainTime(String departureStation, String destinationStation, LocalTime departureTime, LocalTime arrivalTime) { }
-
Run
./gradlew build
and see build directory is created in model
Add service subproject
-
in settings.gradle.kts add service to list of subprojects to include
include("app", "model", "service")
-
add service directory
-
add build.gradle.kts in service directory
-
in build script apply java-library plugin
plugins { `java-library` }
-
in build script add spring-boot-starter dependency
dependencies { implementation("org.springframework.boot:spring-boot-starter:2.6.4") }
-
in build script add Maven Central repository
repositories { mavenCentral() }
-
create a src/main/java directory
-
add com.tomgregory.maxirail.service package
-
add a TrainTimeService.java class
package com.tomgregory.maxirail.service; import com.tomgregory.maxirail.model.TrainTime; import org.springframework.stereotype.Service; import java.time.LocalTime; import java.util.Arrays; import java.util.List; @Service public class TrainTimeService { public List<TrainTime> getTrainTimes() { return Arrays.asList( new TrainTime("London", "Brighton", LocalTime.of(9, 35), LocalTime.of(10, 30)), new TrainTime("London", "Brighton", LocalTime.of(15, 5), LocalTime.of(16, 0)), new TrainTime("Brighton", "London", LocalTime.of(11, 0), LocalTime.of(12, 0)), new TrainTime("Brighton", "London", LocalTime.of(16, 30), LocalTime.of(17, 30)) ); } }
-
Notice how TrainTime isn’t available though? Add this project dependency to service’s build script.
api(project(":model"))
Update MaxiRailController
-
in app subproject’s build script add a dependency on service subproject
implementation(project(":service"))
-
modify
MaxiRailController
class to injectTrainTimeService
and call itsgetTrainTimes
methodpackage com.tomgregory.maxirail.controller; import com.tomgregory.maxirail.model.TrainTime; import com.tomgregory.maxirail.service.TrainTimeService; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class MaxiRailController { private final TrainTimeService trainTimeService; public MaxiRailController(TrainTimeService trainTimeService) { this.trainTimeService = trainTimeService; } @GetMapping(path = "/") public List<TrainTime> getTimes() { return trainTimeService.getTrainTimes(); } }
-
run
./gradlew bootRun
-
see updated API response at localhost:8080
Well done! You just created a layered multi-project Java application.
GitHub repository
The gradle-multi-project-masterclass repository contains the final code solution for lessons 2-5.