Search code examples
dockerkubernetestomcatwar

Set classpath in a container for a war file depoyed with tomcat


I have a war file that I deployed with Tomcat.
I have a Dockerfile and at the end I build a kubernetes pod.

The problem is that if my property files from my app do exist in the path: /usr/local/tomcat/webapps/myapp/WEB-INF/classes/config/ and not in path /usr/local/tomcat/webapps/myapp/WEB-INF/classes/, so the application does not start.

Is it possible to set a classpath in Tomcat to point to a specific folder?
For example, I want to set classpath like: /usr/local/tomcat/webapps/myapp/WEB-INF/classes/config/.
I don't want to have duplicate property files.


Solution

  • As mentioned here:

    foo.properties is supposed to be placed in one of the roots which are covered by the default classpath of a webapp, e.g. webapp's /WEB-INF/lib and /WEB-INF/classes, server's /lib, or JDK/JRE's /lib.

    • If the properties file is webapp-specific, best is to place it in /WEB-INF/classes.
    • If you're developing a standard WAR project in an IDE, drop it in src folder (the project's source folder).
    • If you're using a Maven project, drop it in /main/resources folder.

    You can alternatively also put it somewhere outside the default classpath and add its path to the classpath of the appserver.
    In for example Tomcat you can configure it as shared.loader property of Tomcat/conf/catalina.properties.

    Example:

    FROM tomcat:8.5-jre8
     
    # $CATALINA_HOME is defined in tomcat image
    ADD target/my-webapp*.war $CATALINA_HOME/webapps/my-webapp.war
     
    # Application config
    RUN mkdir $CATALINA_HOME/app_conf/
    ADD src/main/config/test.properties $CATALINA_HOME/app_conf/
     
    # Modify property 'shared.loader' in catalina.properties
    RUN sed -i -e 's/^shared.loader=$/shared.loader="${catalina.base} \/ app_conf"/' $CATALINA_HOME/conf/catalina.properties