YAML Config Files
juneau-config supports YAML config files as an alternative to INI/CFG files.
See the guide: Externalize config with INI/YAML and variables
When to use it
Use YAML when the config is nested or shared with tools that already read YAML. INI remains the default format.
Example
A .yml or .yaml config name auto-selects the YAML format.
import org.apache.juneau.config.*;
import org.apache.juneau.config.store.*;
public class YamlConfigExample {
public static void main(String[] args) throws Exception {
// Put a YAML file in an in-memory store so the example is self-contained.
MemoryStore store = MemoryStore.create().build();
store.update("MyConfig.yaml", "foo:\n bar:\n baz:\n k1: v1\n");
// A .yaml name auto-selects the YAML format.
Config config = Config.create().name("MyConfig.yaml").store(store).build();
System.out.println(config.getString("foo/bar/baz/k1"));
}
}
Options
| Setting | Effect |
|---|---|
name("MyConfig.yaml") | Picks YAML from the file extension |
yaml() | Forces YAML for any name, for example MyConfig.cfg |
Config c = Config.create()
.name("MyConfig.cfg")
.yaml()
.build();
Section path mapping
INI section paths map directly to nested YAML maps:
[foo/bar/baz]
k1 = v1
foo:
bar:
baz:
k1: v1
A config lookup like config.getString("foo/bar/baz/k1") works the same for either format.
Notes
- INI remains the default format.
FileStorenow searchescfg,yml,yamlby default.- Existing value modifiers and SVL variable resolution continue to work unchanged.