Set Environment Variable in Tomcat tomcat logo

Set Environment Variable in Tomcat

In this post, we will learn to set environment variables in the tomcat configuration file so that when the application loads then it will pick a value automatically from there.

Example:

Let’s say, we have a Spring Boot application packaged as a war file and we want to deploy it to an external Tomcat server.

In this case, whatever the profile we’ve defined in our application.properties file it will automatically pick from the tomcat configuration file.

We might have different configurations for different environments. But, once we package our code to a war file then it is unlikely to repackage again and again when we want to deploy to other environments like test, staging, and production.

Hence, to use the same packaged war file in multiple environments we have to set up an environment variable in tomcat’s context.xml file.

Solution:

The context.xml file can be found inside a conf directory of our external tomcat.

Open the context.xml file and define an environment like the following:

<Context>
<!-- Write following line at the end of this file before closing the Context tag.                                   -->
    <Environment name="spring.profiles.active" type="java.lang.String" value="staging" />

</Context>

Now, we can save this file.

After saving this, it is equivalent to the following configuration of application.properties file in our spring boot application:

spring.profiles.active=staging

Conclusion:

In this post, we learned to set the environment name and value in our external tomcat server. By setting this, we can load the value of the environment name from the context.xml file.


Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments