Spring Boot: Building a Multi-Module Application with Gradle (Java)
2 min readJan 27, 2023
What is multi module project in Spring boot
- Spring boot project with nested gradle projects with parent gradle configuration where you can manage all gradle build for the all child projects.
- Multi module project is built from parent gradle config which manages group of submodules.
Why we need multi-module project
- This can help developers to split the project into multiple modules which is easy to maintain.
- All sub projects share common build file.
Project Structure In Intellij IDEA
- Parent project Name: spring-examples
- Sub projects: entity-common, entity-modules, entity-service
Parent gradle config
- Define plugins with version.
plugins {
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'org.springframework.boot' version '2.7.0' apply false
}
2. Dependency Management
User can declare, resolve and define dependencies required by projects. Please find more details here Dependency management in Gradle
allprojects {
apply plugin: 'io.spring.dependency-management'
group 'com.spring.example'
version = getProjectVersion
dependencyManagement {
imports {
mavenBom("org.springframework.boot:spring-boot-dependencies:${versions.springBoot}")
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${versions.springCloud}")
}
dependencies {
dependency group: 'io.micrometer', name: 'micrometer-registry-prometheus', version: versions.micrometer
dependency group: 'commons-logging', name: 'commons-logging', version: versions.commonLogging
}
}
}
How to use sub project in another sub project
For demo purpose, we will consume entity-models project in entity-service.Update project’s build.gradle file and module presented in following block.
dependencies {
implementation(project(':entity-models'))
}
Now you will be able to consume all classes declared inside entity-models
Project Details
Please visit this GitHub repo to check for full example