Skip to content

Add documentation for Module Self Registration feature and removed incompatible file name with Windows #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
138 changes: 138 additions & 0 deletions site/v5/features/features-multi-module-support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
---
title: 'Module Self Registration'
date: 2014-04-18 11:30:00
permalink: /v5/features/multi-module-support/index.html
toc: true
eleventyNavigation:
version: v5
order: 30
parent: features
key: features module self registration
title: 'Module Self Registration'
---

# Module Self Registration

[[TOC]]

## Introduction

Mongock now supports a **modular approach** for managing migration packages.
This allows each module in a multi-module project to be responsible for registering its own migration scan packages, enabling a decentralized configuration.

This feature works for both:

- **Annotation-based configuration** (in Spring Boot applications)
- **Builder-based configuration** (in Standalone or non-Spring environments)

---

## Why modular support?

In traditional setups, the migration scan packages are usually registered centrally, in a single configuration. This creates a coupling between modules and forces central coordination.

With this feature:

- Modules become more **autonomous**
- Migration logic can be kept **encapsulated** in each module
- Applications become more **scalable and maintainable**

---

## Usage

### 🟢 Spring Boot (with Annotations)

Mongock introduces the ability to define migration packages directly within a Spring `@Configuration` class using the annotations `@MongockScanPackage` or `@MongockScanPackages`.

#### Example:

```java
@Configuration
@MongockScanPackage({
"com.package.example.migrations",
"com.package.example2.migrations"
})
public class ModuleConfig {
}
```

To integrate this with the application, you can just let Spring know ModuleConfig or you can create a custom enabling annotation:
```java
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(ModuleConfig.class)
public @interface EnableModuleA {
}
```

Then use it in your application:
```java
@EnableModuleA
@EnableMongock
@SpringBootApplication
public class SomeApp {
public static void main(String[] args) {
SpringApplication.run(SomeApp.class, args);
}
}
```
This allows each module to define its own migration paths independently.

### 🟡 Standalone (with Builder)
In standalone mode, Mongock introduces a new interface: ``MongockModuleConfig``.

Each module can implement this interface and register its migration packages through code.

Interface:

```java
public interface MongockModuleConfig {

void configure(MongockScanPackage scan);
}
```

Example usage:

```java
public class ModuleAConfig implements MongockModuleConfig {

@Override
public void configure(MongockScanPackage scan) {
scan.add("com.example.packageone.migrations");
// or
List<String> packages = Arrays.asList(Array.asList(
"com.example.packageone.migrations",
"com.example.packagetwo.migrations"
));
scan.addAll(packages);
}
};
```

Register the module configuration:

```java
MongockStandalone.builder()
.setDriver(driver)
.addModuleConfig(new ModuleAConfig())
.build();
```
This allows the migration setup to grow with your system without editing central files.

## Benefits

✅ Decoupled and clean modular architecture

✅ Easier to maintain and extend

✅ Enables plugin-like architectures

✅ Works with both Spring and standalone setups

## Notes

* This feature is fully backward compatible.
* Centralized configuration is still supported.
* Multiple module configs can be loaded in parallel.