Environment/system variables in server.xml
Asked Answered
F

2

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.

Fairyfairyland answered 12/8, 2012 at 21:57 Comment(1)
@DagR, unfortunately noFairyfairyland
L
20

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)

Lysenko answered 1/10, 2019 at 13:5 Comment(6)
Watch out, because in the near future the $ in the class name will change to a . See github.com/apache/tomcat/commit/…Showbread
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.Richburg
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? @RichburgKoosis
@JonathanHagen Yes.Richburg
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: tomcat.apache.org/tomcat-9.0-doc/api/org/apache/tomcat/util/…Jorgan
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"Loony
F
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
Fluxmeter answered 30/5, 2014 at 15:10 Comment(2)
Can you please reference the official documentation for this? thanks!Wizened
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?Adaxial

© 2022 - 2024 — McMap. All rights reserved.