This project demonstrates how to structure a Spring Boot project with Gradle that allows for:
- Providing a baseline set of dependencies and their Spring configurations in a jackson module
- Allowing service modules to override specific dependency versions while still using the jackson module's configurations
spring-gradle-example/
├── build.gradle # Root project build file with common configurations
├── settings.gradle # Project settings file
├── jackson-module/ # Jackson module with baseline dependencies and configurations
│ ├── build.gradle # Jackson module build file with Jackson dependencies
│ └── src/
│ └── main/
│ ├── java/
│ │ └── com/example/jackson/config/
│ │ └── JacksonConfig.java # Auto-configured Jackson configuration
│ └── resources/
│ └── META-INF/
│ └── spring.factories # Auto-configuration registration
└── service-module/ # Service module that uses the jackson module
├── build.gradle # Service build file that overrides Jackson versions
└── src/
└── main/
└── java/
└── com/example/service/
└── ServiceApplication.java # Spring Boot application
The root build.gradle file sets up common configurations for all modules:
- Applies the Spring Boot dependency management plugin
- Sets up Java compatibility
- Configures repositories
- Imports the Spring Boot BOM (Bill of Materials)
The jackson module:
- Uses the
java-libraryplugin to expose dependencies to consumers - Declares Jackson dependencies with specific versions using the
apiconfiguration - Provides a Spring configuration class (
JacksonConfig) that configures Jackson - Uses Spring Boot's auto-configuration mechanism to automatically apply the configuration
The service module:
- Depends on the jackson module
- Overrides the Jackson versions with newer versions
- Uses the jackson module's Spring configurations automatically
- Can add its own dependencies and configurations
When the service module includes both the jackson module and its own Jackson dependencies:
- Gradle resolves the dependency conflict by using the newest version
- The service's Jackson version (2.14.0) takes precedence over the jackson module's version (2.13.4)
- The jackson module's Spring configurations still apply, but they use the service's Jackson version
To run the application:
./gradlew :service-module:bootRunVisit http://localhost:8080/test to see the response with the timestamp formatted according to the jackson module's configuration but using the service's Jackson version.
This approach allows you to:
- Create reusable libraries with pre-configured dependencies and Spring configurations
- Override specific dependency versions in services without duplicating configuration code
- Maintain consistent configuration across multiple services
- Upgrade dependencies independently in each service
- This example uses Spring Boot 2.7.5, but the same approach works with other versions
- The same pattern can be applied to other dependencies beyond Jackson
- For more complex scenarios, consider using a platform BOM in the jackson module