24

How can I use environment/system variables in tomcat server.xml, context.xml, etc configuration files?

I tried to use ${ENV_VAR_NAME} (both for environment and system variable), ${env.ENV_VAR_NAME} (for environment variables). And nothing seems to work.

bradrn
  • 8,337
  • 2
  • 22
  • 51
michael nesterenko
  • 14,222
  • 25
  • 114
  • 182

2 Answers2

19

How it's realized in my box.

Bash-script for startup:

#!/bin/sh

SMEMORY=1G
XMEMORY=1G

if [ $ENV == DEV ]; then
  port_shutdown="8005"
  port_http="8080"
  port_https="8443"
elif
  [ $ENV == SIT ]; then
  port_shutdown="8006"
  port_http="8081"
  port_https="8444"
elif
  [ $ENV == UAT ]; then
  port_shutdown="8007"
  port_http="8082"
  port_https="8445"
else
  echo "Unknown ENV"
  exit 1
fi

export CATALINA_OPTS=" ${SYSTEM_PROPS} -d64 -server -Xms$SMEMORY -Xmx$XMEMORY \
 -XX:+UseCodeCacheFlushing -XX:ReservedCodeCacheSize=64M \
 -XX:+HeapDumpOnOutOfMemoryError -XX:MaxPermSize=1024M \
 -Dport.http=${port_http} -Dport.https=${port_https} -Dport.shutdown=${port_shutdown}"

exec $CATALINA_HOME/bin/startup.sh

In server.xml:

<Connector
  port="${port.http}"
  protocol="HTTP/1.1"
  connectionTimeout="20000"
  redirectPort="${port.https}"
/>

Take a look at process:

$ ps ux | grep tomcat
... -Xms1G -Xmx1G ... -Denv=KIEV_DEV... -Dport.http=8084 -Dport.https=8446 -Dport.shutdown=8008...

Check ports:

$ netstat -anp | grep java
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 :::8084                     :::*                        LISTEN      23343/java
tcp        0      0 :::8446                     :::*                        LISTEN      23343/java
setevoy
  • 4,374
  • 11
  • 50
  • 87
  • 1
    Can you please reference the official documentation for this? thanks! – galusben Dec 24 '18 at 14:13
  • 1
    So in a nutshell, you're modifying the tomcat startup environment with custom environment variables that then become accessible from the environment with which the tomcat xml's are parsed (the application memory vs the base linux env). So in other words, Tomcat cannot access normal BASH env variables but you can inject them into the Tomcat environment in a way that makes them accessible to reference inside of the tomcat xmls, correct? – thepip3r Mar 19 '21 at 16:28
19

Environment variables can be referenced in the server.xml etc by setting the system property org.apache.tomcat.util.digester.PROPERTY_SOURCE to the value org.apache.tomcat.util.digester.Digester$EnvironmentPropertySource.

That system property has been available since 7.0, but EnvironmentPropertySource was not mentioned in the doc until 8.5.

https://tomcat.apache.org/tomcat-9.0-doc/config/systemprops.html

Update (April 2020):

The latest tomcat releases (9.0.34, 8.5.54) now support property replacement in most configuration files: https://tomcat.apache.org/tomcat-9.0-doc/changelog.html#Tomcat_9.0.34_(markt)

Brett Okken
  • 6,210
  • 1
  • 19
  • 25
  • 2
    Watch out, because in the near future the `$` in the class name will change to a `.` See https://github.com/apache/tomcat/commit/ffba0c6c97f03e0a32c8a40a1589f93419458ef1 – Tobia Mar 17 '20 at 17:40
  • 2
    Just `echo 'org.apache.tomcat.util.digester.PROPERTY_SOURCE=org.apache.tomcat.util.digester.Digester$EnvironmentPropertySource' >> conf/catalina.properties` if you find it hard to play with dollar sign in shell script. – youfu Apr 14 '20 at 09:19
  • so basically adding `'org.apache.tomcat.util.digester.PROPERTY_SOURCE=org.apache.tomcat.util.digester.Digester$EnvironmentPropertySource'` this line to `/tomcat/conf/catalina.properties`? When tomcat is starting, it will resolve tokens in `server.xml` with environment variables? @youfu – Jonathan Hagen Apr 22 '21 at 13:39
  • 1
    @JonathanHagen Yes. – youfu Apr 23 '21 at 07:13
  • 4
    Just to note `org.apache.tomcat.util.digester.Digester$EnvironmentPropertySource` is now deprecated, at least with Tomcat 9.0.55. The alternative is `org.apache.tomcat.util.digester.PROPERTY_SOURCE=org.apache.tomcat.util.digester.EnvironmentPropertySource` in catalina.properties: https://tomcat.apache.org/tomcat-9.0-doc/api/org/apache/tomcat/util/digester/EnvironmentPropertySource.html – df778899 Jan 25 '22 at 08:36
  • Confirmed this works with Tomcat 8.5.90 + just add "CATALINA_OPTS=-Dorg.apache.tomcat.util.digester.PROPERTY_SOURCE=org.apache.tomcat.util.digester.EnvironmentPropertySource" to your setenv.bat and you can then do things like truststoreFile="${JRE_HOME}\\lib\\security\\cacerts" and keystoreFile="${USERPROFILE}\\.keystore" – Greg Jul 04 '23 at 22:16