Apache FreeMarker Generator
supports the transformation of directories
The following sample files are used
tree examples/data/template/ examples/data/template/ |-- application.properties `-- nginx `-- nginx.conf.ftl # == application.properties ================================================== server.name=${NGINX_HOSTNAME!"127.0.0.1"} server.logs=${NGINX_LOGS!"/var/log/nginx"}
# == nginx-conf ============================================================== server { listen ${NGINX_PORT!"80"}; server_name ${NGINX_HOSTNAME!"127.0.0.1"}; root ${NGINX_WEBROOT!"/usr/share/nginx/www"}; index index.htm;
The transformed templates are written to an out
directory
nginx.conf.ftl
was changed to `nginx.conf" during the transformationfreemarker-generator -t examples/data/template/ -o out; tree out; cat out/application.properties out/nginx/nginx.conf out |-- application.properties `-- nginx `-- nginx.conf 1 directory, 2 files # == application.properties ================================================== server.name=127.0.0.1 server.logs=/var/log/nginx # == nginx-conf ============================================================== server { listen 80; server_name 127.0.0.1; root /usr/share/nginx/www; index index.htm; }
A user-supplied parameter NGINX_HOSTNAME
is used to render the templates
freemarker-generator -t examples/data/template/ -P NGINX_HOSTNAME=some.host.invalid -o out; \ cat out/application.properties out/nginx/nginx.conf # == application.properties ================================================== server.name=some.host.invalid server.logs=/var/log/nginx # == nginx-conf ============================================================== server { listen 80; server_name some.host.invalid; root /usr/share/nginx/www; index index.htm; }
All environment variables can be copied to the top-level data model by providing -m env:///
-m
or -data-model
creates a data modelenv:///
is an URI referencing all environment variablesexport NGINX_PORT=8080 freemarker-generator -t examples/data/template/ -m env:/// # == application.properties ================================================== server.name=localhost server.logs=/var/log/nginx # == nginx-conf ============================================================== server { listen 8080; server_name 127.0.0.1; root /usr/share/nginx/www; index index.htm; }
Instead of environment variables an environment file (aka properties file) can be used
echo "NGINX_PORT=8080" > nginx.env freemarker-generator -t examples/data/template/ -m nginx.env # == application.properties ================================================== server.name=localhost server.logs=/var/log/nginx # == nginx-conf ============================================================== server { listen 8080; server_name 127.0.0.1; root /usr/share/nginx/www; index index.htm; }
Another option is passing the information as JSON file
echo '{"NGINX_PORT":"8443","NGINX_HOSTNAME":"some.host.invalid"}' > nginx.json freemarker-generator -t examples/data/template/ -m nginx.json # == application.properties ================================================== server.name=some.host.invalid server.logs=/var/log/nginx # == nginx-conf ============================================================== server { listen 8443; server_name some.host.invalid; root /usr/share/nginx/www; index index.htm; }
Yet another option is using a YAML file
echo -e "- NGINX_PORT": "\"8443\"\n- NGINX_HOSTNAME": "some.host.invalid" > nginx.yaml freemarker-generator -t examples/data/template/ -m nginx.yaml # == application.properties ================================================== server.name=localhost server.logs=/var/log/nginx # == nginx-conf ============================================================== server { listen 8443; server_name localhost; root /usr/share/nginx/www; index index.htm; }
In the cloud it is common to pass JSON configuration as environment variable
env:///NGINX_CONF
selects the NGINX_CONF
environment variable#mimeType=application/json
defines that JSON content is parsedexport NGINX_CONF='{"NGINX_PORT":"8443","NGINX_HOSTNAME":"some.host.invalid"}' freemarker-generator -t examples/data/template/ -m env:///NGINX_CONF#mimeType=application/json # == application.properties ================================================== server.name=some.host.invalid server.logs=/var/log/nginx # == nginx-conf ============================================================== server { listen 8443; server_name some.host.invalid; root /usr/share/nginx/www; index index.htm; }
For testing purpose it is useful to override certain settings
export NGINX_CONF='{"NGINX_PORT":"8443","NGINX_HOSTNAME":"some.host.invalid"}' freemarker-generator -t examples/data/template/ -PNGINX_HOSTNAME=www.mydomain.com -m env:///NGINX_CONF#mimeType=application/json # == application.properties ================================================== server.name=www.mydomain.com server.logs=/var/log/nginx # == nginx-conf ============================================================== server { listen 8443; server_name www.mydomain.com; root /usr/share/nginx/www; index index.htm; }
Please note that this only works for “top-level” variables, i.e. mimicking environment variables or property files.