Skip to main content

VarResolver.DEFAULT

VarResolver.DEFAULT is a reusable, thread-safe VarResolver instance pre-configured with the most commonly-needed source variables. Because it is immutable and shared, you can reference it directly from anywhere without constructing your own resolver.

Built-in variables​

VariableClassResolves against
$S{key[,default]}SystemPropertiesVarJava system properties (System.getProperty)
$E{key[,default]}EnvVariablesVarOS environment variables (System.getenv)
$EF{key[,default]}EnvFileVarA .env-style env file on the filesystem
$DE{key[,default]}DotenvVarA .env dotenv file
$A{key[,default]}ArgsVarJVM command-line arguments
$MF{key[,default]}ManifestFileVarClasspath META-INF/MANIFEST.MF entries

Each variable accepts an optional comma-separated default that is returned when the key cannot be resolved.

Usage​

// Resolve a single system-property variable.
String s = VarResolver.DEFAULT.resolve("The Java home directory is $S{java.home}");

Variables can be arbitrarily nested — the resolved value of one variable can itself contain variables, and a variable's default can be another variable:

// Look up a value in priority order:
// 1) the MYPROPERTY environment variable,
// 2) the 'my.property' system property if the env var is not set,
// 3) the literal 'not found' if neither is set.
String s = VarResolver.DEFAULT.resolve("$E{MYPROPERTY,$S{my.property,not found}}");

Resolving a string that contains no variables is a no-op that returns the input unchanged, so it is safe (and cheap) to run arbitrary text through the resolver.

Extending the default resolver​

VarResolver.DEFAULT is a starting point, not a dead end. Use copy() to derive a new resolver that inherits its variables and adds your own:

// Derive a resolver that also supports a custom $MY{...} variable.
VarResolver vr = VarResolver.DEFAULT.copy()
.vars(MyCustomVar.class)
.build();

See also​