简体   繁体   中英

Customize WAR deployment for Tomcat

I'm using the Spring Framework (Spring Boot) with Maven. I'm making .war deployed (manually) on Tomcat 8.5. My application is sold to multiple customers.

I have 2 config files. The main application.yml which contains options common to all installations, and a application-custom.yml unique per installation (database credentials, mail etc.). This custom config file must not be erased when I update my war package (Tomcat erases the config file and replaces it by the newest of the war). But I don't want the custom file replaced by a new at every update.

To fix it, my Spring config file is actually (manually) copied in the webapps path of Tomcat (not erased when update like that) :

C:\\Program Files\\Apache Software Foundation\\Tomcat 8.5\\webapps\\application-custom.yml

But this is not the best solution I guess. Because at every new install, I must create the custom config file by myself. Is there a better solution ?

On every start you can move config file to safe place like for example /webapps/configs and after Tomcat extracts .war file move your config back. To do this you need to modify catalina.sh in bin folder. After this line:

elif [ "$1" = "start" ] ; then

add this to move your config to safe place

cp $CATALINA_HOME/webapps/myApp/WEB-INF/classes/your_config.yml $CATALINA_HOME/webapps/configs

After this:

  if [ ! -z "$CATALINA_PID" ]; then
    echo $! > "$CATALINA_PID"
  fi

  echo "Tomcat started."

add below to move your config back to the place :

cp $CATALINA_HOME/webapps/configs/your_config.yml $CATALINA_HOME/webapps/myApp/WEB-INF/classes

After that start your Tomcat using catalina.sh start

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM