Use variables in configuration files
Use variables to avoid hardcoding values that might not be appropriate when the configuration is reused in different environments.
Variables can be defined by setting a property in any of the following places:
- in the server configuration file, or an included file
- in bootstrap.properties
The following predefined variables can be referenced:
- directory properties, see Directory locations
- JVM system properties
- process environment variables
Variables specific to a particular server, for example port numbers, are specified in bootstrap.properties, allowing server.xml to be shared across multiple servers, while keeping those values different in each server. Variables shared across a group of servers, for example database configuration for a particular host, is better specified in an xml file included into the parent configuration file.
Variable names must begin with an alphabetic character, and must contain the following characters only: alphabetic characters, numeric characters, and the "_" and "." characters.
- Specify a variable in a configuration file.
Variables defined in the configuration files are scoped to the configuration elements by which they are used. For example, the following code fragment creates a variable called updateTrigger_var to be used in applicationMonitor configuration elements:
<applicationMonitor updateTrigger_var="mbean" />
To create a variable used in a particular configuration instance (such as an application or resource entry), also specify the instance identifier. For example: <httpEndpoint id="defaultHttpEndpoint" HTTP_default_var="8889" />
- Specify a variable in bootstrap.properties.
Variables defined in bootstrap.properties are not scoped to particular configuration elements. Enter the variables as key-value pairs. For example:
HTTP_default_var=8006
- Use a defined variable in the configuration.
The variable substitution syntax is ${variable_name}. For example, to use the HTTP_default_var variable, add the following code fragment to the configuration file:
<httpEndpoint id="defaultHttpEndpoint" httpPort="${HTTP_default_var}"> </httpEndpoint>- Use variable element in the configuration
We can use the variable element to define a variable globally in the server configuration. If the same variable is defined in an included file, it is overridden by the one in server.xml. For example, to use the variable element, add the following code fragment to the configuration file:
<variable name="HTTP_default_var" value="8889" />
- Use process environment variables in the configuration
Process environment variables are available if we use the env. configuration variable prefix, for example:
See Customize the Liberty profile environment .
Use variable expressions in configuration
For configuration variables, we can use a limited variable expression syntax with the format ${<operand><operator><operand>}. The description of the variable is as follows:
- operand
- Operands can either be long integer literals or the name of a variable containing a long integer value. Variable names must begin with an alphabetic character, and must contain the following characters only: alphabetic characters, numeric characters, and the "_" and "." characters.
- operator
- The available operators :
- + for addition
- - for subtraction
- * for multiplication
- / for division
- If the expression cannot be parsed, a non-integer value is used, or an arithmetic error occurs, then the behavior is undefined.
For example, if the HTTP_port_base variable is defined, a variable expression might be used to define multiple httpEndpoints:
<httpEndpoint id="defaultHttpEndpoint" httpPort="${HTTP_port_base+0}"/> <httpEndpoint id="httpEndpoint2" httpPort="${HTTP_port_base+1}"/>
- Override inheritable attributes in the configuration
We can override the default values of inheritable attributes in the configuration. The inheritable attributes are listed on the Configuration elements in server.xml page with an Inherits type. For example, the onError attribute is one of inheritable attributes. We can define a variable name for the onError attribute globally by either setting it in the bootstrap.properties or server.xml file with a variable element. If the same variable name is specified in both files, the value in server.xml is used. If the attribute is not explicitly set in either of two files, it uses the default value. If an invalid value is set to the inheritable attribute, the attribute value falls back to the global value defined in bootstrap.properties or server.xml file or the default value if not defined at the global level.
Another example is logging properties in the Liberty profile. See Logging and Trace.
Parent topic: Use include elements, variables, and Ref tags in configuration files