WebSphere Tutorial · WebSphere Liberty · Lesson 6 of 23
Configuration Files and Variables
Splitting Liberty configuration with includes and configDropins, and supplying environment-specific values through variables.
Variables
Values that differ by environment are variables, written ${name}:
<variable name="db.host" defaultValue="localhost"/>
<dataSource jndiName="jdbc/orders">
<properties.postgresql serverName="${db.host}" databaseName="orders" user="${env.DB_USER}" password="${env.DB_PASSWORD}"/>
</dataSource>
Variables can come from <variable> elements, bootstrap.properties, server.env, environment variables
(${env.NAME}), or files in a variables directory (handy for Kubernetes Secrets mounted as files).
Splitting configuration
<include location="datasources.xml"/>pulls in another file.- Files in
configDropins/defaults/load first; files inconfigDropins/overrides/load last and win, a clean way to adjust an image's configuration per environment without editingserver.xml. - Encode passwords with
securityUtility encodeif they must live in files, or better, keep them out of files.
import re
sources = [ # later sources override earlier ones in this simulation
("server.xml <variable> defaults", {"db.host": "localhost", "http.port": "9080"}),
("bootstrap.properties", {"http.port": "9081"}),
("environment variables", {"db.host": "orders-db.prod.internal"}),
]
values = {}
for name, vals in sources:
values.update(vals)
template = 'serverName="${db.host}" httpPort="${http.port}"'
print(re.sub(r"\$\{([^}]+)\}", lambda m: values[m.group(1)], template))
Output
serverName="orders-db.prod.internal" httpPort="9081"
Test yourself
Where would you put a file that overrides an image's server.xml for one environment?
dropins/configDropins/overrides/logs/apps/
Show answer
Answer: configDropins/overrides/
Override files load last, after server.xml.