Skip to content

Instantly share code, notes, and snippets.

@atrolla
Last active October 31, 2022 15:52
Show Gist options
  • Save atrolla/81f96635522d68f4edab4250e373ff57 to your computer and use it in GitHub Desktop.
Save atrolla/81f96635522d68f4edab4250e373ff57 to your computer and use it in GitHub Desktop.
Deploy multiple Spring boot app on a single tomcat with different configuration for each app

Multiple Spring boot deployment on one tomcat

Configure app so it can be deployed on a tomcat

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html

Configure tomcat to use a properties file per app deployed

The mechanism is based on Spring boot properties

and on tomcat 7/8 configuration :

Warning !

The solution uses the $CATALINA_BASE/conf/[engine_name]/[host_name]/app.xml file which is DELETED when app.war is undeployed (so also when you update the war, redeploy, or restart the tomcat...)

make sure that you keep a copy of app.xml, or maybe in META-INF/context.xml

http://tomcat.apache.org/tomcat-6.0-doc/config/host.html#Automatic%20Application%20Deployment


Example

demo.war on Tomcat 8

Spring boot controller :

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @Value("${ma.variable.valeur}")
    private String value;

    @RequestMapping("/*")
    public String getAll() {
        return value;
    }
}

with application.properties

ma.variable.valeur = LOL

This is the basic configuration for deploying a Spring boot app with Tomcat 8.


Tomcat 8 JVM options containing -Dspring.profiles.active=dev

/conf/Catalina/localhost/demo.xml:

<?xml version="1.0" encoding="UTF-8"?>

<Context antiJARLocking="true" path="/demo">
<Resources>
    <PostResources className="org.apache.catalina.webresources.DirResourceSet"
                   webAppMount="/WEB-INF/classes" base="/tmp/demo" />
</Resources>
</Context>

Now in /tmp/demo, let's create application-dev.properties :

ma.variable.valeur=OVERRIDE

The rest request should now return : OVERRIDE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment