Class ConfigMigrator
High-level API for migrating configuration files.
ConfigMigrator combines RawYamlLoader, MigrationRegistry, and MigrationExecutor
into a simple, easy-to-use API for migrating VersionAwareConfiguration files.
Basic Usage
// Create a migrator with your migrations
ConfigMigrator migrator = ConfigMigrator.builder()
.register(Migration.of(2, "Add timeout field", ctx -> {
ctx.data().putIfAbsent("timeout", 30);
}))
.register(Migration.of(3, "Rename host to hostname", ctx -> {
ctx.rename("host", "hostname");
}))
.build();
// Migrate and load config
MyConfig config = migrator.migrateAndLoad(configPath, MyConfig.class);
Backup Support
The migrator can create backups before applying migrations:
ConfigMigrator migrator = ConfigMigrator.builder()
.createBackups(true) // Enable backups
.backupSuffix(".backup") // Customize suffix (default: .bak)
.register(...)
.build();
Migration Callbacks
Monitor migration progress with callbacks:
ConfigMigrator migrator = ConfigMigrator.builder()
.beforeMigration(m -> logger.info("Applying: " + m.description()))
.afterMigration((m, ctx) -> logger.info("Applied migration to v" + m.targetVersion()))
.onError(e -> logger.error("Migration failed", e))
.register(...)
.build();
Complete Example
@Configuration
public class ServerConfig extends VersionAwareConfiguration {
private static final int CURRENT_VERSION = 3;
private String hostname = "localhost";
private int port = 25565;
private int timeout = 30;
public ServerConfig() {
super(CURRENT_VERSION);
}
}
// Setup migrator
ConfigMigrator migrator = ConfigMigrator.builder()
.createBackups(true)
.register(Migration.of(2, "Add timeout", ctx -> {
ctx.setDefault("timeout", 30);
}))
.register(Migration.of(3, "Rename host to hostname", ctx -> {
ctx.rename("host", "hostname");
}))
.build();
// Load with automatic migration
Path configPath = dataFolder.resolve("config.yml");
ServerConfig config = migrator.migrateAndLoad(configPath, ServerConfig.class, 3);
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for creatingConfigMigratorinstances. -
Method Summary
Modifier and TypeMethodDescriptionstatic ConfigMigrator.Builderbuilder()Creates a new migrator builder.booleancanMigrate(int fromVersion, int toVersion) Checks if all migrations are available for the given version range.getVersion(Path path) Gets the current version of a configuration file.Migrates a configuration file to the target version.<T extends VersionAwareConfiguration>
TmigrateAndLoad(Path path, Class<T> configClass, int targetVersion) Migrates a configuration file to the target version and loads it.<T extends VersionAwareConfiguration>
TmigrateAndLoad(Path path, Class<T> configClass, int targetVersion, de.exlll.configlib.YamlConfigurationProperties properties) Migrates a configuration file to the target version and loads it with custom properties.migrateData(Map<String, Object> data, int fromVersion, int toVersion) Migrates in-memory data without file I/O.booleanneedsMigration(Path path, int targetVersion) Checks if migrations are needed for a configuration file.static ConfigMigratorCreates a simple migrator with the given migrations.registry()Returns the migration registry.
-
Method Details
-
builder
Creates a new migrator builder.- Returns:
- a new builder
-
of
Creates a simple migrator with the given migrations.- Parameters:
migrations- the migrations to register- Returns:
- a new migrator
-
migrateAndLoad
public <T extends VersionAwareConfiguration> T migrateAndLoad(Path path, Class<T> configClass, int targetVersion) Migrates a configuration file to the target version and loads it.
This is the primary method for loading versioned configurations. It:
- Loads the raw YAML data
- Reads the current version
- Applies necessary migrations
- Saves the migrated data
- Syncs the configuration (adds new fields, removes obsolete ones)
- Loads the configuration using ConfigLib
- If an env prefix is set, environment variables with that prefix override config values
The sync step ensures that even if a migration is imperfect, any new fields added to the Java class will be written to the file with their default values.
- Type Parameters:
T- the configuration type- Parameters:
path- the path to the configuration fileconfigClass- the configuration class (must extendVersionAwareConfiguration)targetVersion- the target version to migrate to- Returns:
- the loaded and migrated configuration
- Throws:
MigrationException- if migration or loading failsNullPointerException- if any parameter is null
-
migrateAndLoad
public <T extends VersionAwareConfiguration> T migrateAndLoad(Path path, Class<T> configClass, int targetVersion, de.exlll.configlib.YamlConfigurationProperties properties) Migrates a configuration file to the target version and loads it with custom properties.- Type Parameters:
T- the configuration type- Parameters:
path- the path to the configuration fileconfigClass- the configuration classtargetVersion- the target version to migrate toproperties- custom ConfigLib properties- Returns:
- the loaded and migrated configuration
- Throws:
MigrationException- if migration or loading fails
-
migrate
Migrates a configuration file to the target version.
This method only performs migration without loading the result into a typed class. Use this when you need more control over the process or want to inspect the migration result before loading.
- Parameters:
path- the path to the configuration filetargetVersion- the target version to migrate to- Returns:
- the migration result
- Throws:
NullPointerException- if path is null
-
migrateData
Migrates in-memory data without file I/O.- Parameters:
data- the raw YAML data to migratefromVersion- the current versiontoVersion- the target version- Returns:
- the migration result
- Throws:
NullPointerException- if data is null
-
needsMigration
Checks if migrations are needed for a configuration file.- Parameters:
path- the path to the configuration filetargetVersion- the target version- Returns:
- true if the file needs migration
- Throws:
NullPointerException- if path is null
-
getVersion
Gets the current version of a configuration file.- Parameters:
path- the path to the configuration file- Returns:
- an Optional containing the version, or empty if file doesn't exist
- Throws:
NullPointerException- if path is null
-
canMigrate
public boolean canMigrate(int fromVersion, int toVersion) Checks if all migrations are available for the given version range.- Parameters:
fromVersion- the starting versiontoVersion- the target version- Returns:
- true if all migrations are available
-
registry
-