title: How to use external configuration files with Spring boot deployed in Tomcat 8.5
body: <p>My Spring Boot application (2.1.1.RELEASE) is deployed as a WAR in a Tomcat 8.5 server under a Debian 9 system. It uses, among others, the following files to configure the application :</p>
  
  <ul>
  <li>myApplication.properties (main configuration file for Spring)</li>
  <li>log4j2.xml</li>
  </ul>
  
  <p>Both are under <code>src/main/resources</code>. </p>
  
  <p>My question is about how to configure Tomcat and Spring Boot in a way that allow me to have a directory <code>/home/oliver/conf</code> which contains both of these files, in order to override the defaults defined under <code>src/main/resources</code> (which are then in <code>WEB-INF/classes</code> in the exploded WAR). </p>
  
  <p><em>Below are the steps I've taken.</em></p>
  
  <p>First, and as a requirement for the project I'm working on, I changed the default Tomcat base directory to point to another place by editing <code>/etc/init.d/tomcat8</code> :</p>
  
  <pre><code>CATALINA_HOME=/usr/share/tomcat8
  CATALINA_BASE=/home/oliver
  </code></pre>
  
  <p>My <code>/home/oliver/conf</code> folder, which holds the Tomcat and Spring configs, looks like this :</p>
  
  <pre><code>- Catalina/
  - context.xml
  - web.xml
  - server.xml
  - ...
  - myApplication.properties
  - log4j2.xml
  - otherAppConfFile.properties
  - ...
  </code></pre>
  
  <p>Because Spring looks for <code>application.properties</code> by default, I'm using the <strong>@PropertySource</strong> annotation to specify another file :</p>
  
  <pre><code>@SpringBootApplication
  @PropertySource({classpath: myApplication.properties})
  public class MyApp extends SpringBootServletInitializer {...}
  </code></pre>
  
  <p>I've tried to add <code>-Dspring.config.location=file:/home/oliver/conf/myApplication.properties</code> to <strong>JAVA_OPTS</strong> defined in <code>/etc/default/tomcat8</code>, and it works (overrides the embedded file correctly), but for instance if I try to add <code>file:/etc/oliver/conf/log4j2.xml</code> to the previous JVM parameter, it doesn't work. </p>
  
  <p>I read a bit about Spring "environment profiles" but don't wish to use them if possible.</p>
  
  <p>When I launch Tomcat and issue a <code>ps aux | grep tomcat</code> command, I see all the JAVA_OPTS parameters defined as expected, and I also see the following :</p>
  
  <pre><code>-classpath :/home/oliver/conf:/usr/share/tomcat8/bin/.... -Dcatalina.base=/home/oliver -Dcatalina.home=/usr/share/tomcat8
  </code></pre>
  
  <p>I'm a bit confused about the way Tomcat's classpath and Spring's are related, and how I should solve this issue. </p>
  
  <p>If the classpath I see at launch includes the <code>/home/oliver/conf</code> directory, why are the files inside not overriding the embedded property files (myApplication.properties, log4j2.xml ...) ? Is the folder seen and added to Spring's classpath ?</p>
  
  <p>EDIT :</p>
  
  <p>As a side note, there might be a variety of files under <code>/home/oliver/conf</code> which would need to be taken into account, for instance <strong>log4j2.xml</strong> + <strong>myApplication.properties</strong> + <strong>keystore.jks</strong> so I'm not sure I can rely on <code>-Dspring.config.location</code> and <code>-Dlogging.config</code> entirely.</p>