home / skills / mduongvandinh / skills-java-spring / spring-maven-modular
This skill enables modular Maven builds for Spring projects by toggling components like Redis, Kafka, and RabbitMQ via profiles and properties.
npx playbooks add skill mduongvandinh/skills-java-spring --skill spring-maven-modularReview the files below or copy the command above to add this skill to your agents.
---
name: spring-maven-modular
version: 1.0.0
description: |
Maven Modular Architecture with profiles for optional components.
Enable/disable modules like Redis, Kafka, RabbitMQ dynamically.
triggers:
- "maven"
- "module"
- "profile"
- "dependency"
- "optional"
---
# Maven Modular Architecture
## Module Flags
| Module | Default | Property |
|--------|---------|----------|
| PostgreSQL | ON | `module.postgresql.enabled` |
| Redis | OFF | `module.redis.enabled` |
| Kafka | OFF | `module.kafka.enabled` |
| RabbitMQ | OFF | `module.rabbitmq.enabled` |
| OAuth2 | OFF | `module.oauth2.enabled` |
## Usage
```bash
# Minimal (PostgreSQL only)
mvn clean install -Pminimal
# With Redis
mvn clean install -Dmodule.redis.enabled=true
# With Kafka
mvn clean install -Dmodule.kafka.enabled=true
# Full stack
mvn clean install -Pfull-stack
# Custom combination
mvn clean install -Dmodule.redis.enabled=true -Dmodule.kafka.enabled=true
```
## Profiles
### Minimal Profile
- PostgreSQL (always)
- JWT Authentication
### Full Stack Profile
- PostgreSQL
- Redis
- Kafka
- OAuth2
## Spring Configuration
Use `@ConditionalOnModuleEnabled` to conditionally load beans:
```java
@Configuration
@ConditionalOnModuleEnabled("redis")
public class RedisConfig {
// Only loaded when modules.redis.enabled=true
}
```
## application.yml
```yaml
modules:
postgresql:
enabled: true
redis:
enabled: ${MODULE_REDIS_ENABLED:false}
kafka:
enabled: ${MODULE_KAFKA_ENABLED:false}
rabbitmq:
enabled: ${MODULE_RABBITMQ_ENABLED:false}
oauth2:
enabled: ${MODULE_OAUTH2_ENABLED:false}
```
This skill implements a Maven-based modular architecture for Spring Boot projects, letting you enable or disable optional components at build time. It provides profile presets (minimal, full-stack) and per-module flags for Redis, Kafka, RabbitMQ, OAuth2, and PostgreSQL. The goal is predictable builds and conditional bean loading to keep the runtime surface minimal and focused.
Modules are toggled via Maven profiles or system properties (e.g., -Dmodule.redis.enabled=true). The build includes or excludes module artifacts and resource wiring based on those flags. At runtime Spring beans use a @ConditionalOnModuleEnabled annotation to register only when the corresponding module flag is active. application.yml maps module flags to environment variables for containerized deployments.
How do I conditionally load Spring beans for a module?
Annotate configuration classes with @ConditionalOnModuleEnabled("moduleName") so they only register when the corresponding module flag is true.
Can I control modules with environment variables?
Yes. application.yml reads module flags from environment variables (e.g., MODULE_REDIS_ENABLED) so you can control modules in containers and CI.