2

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 :

  • myApplication.properties (main configuration file for Spring)
  • log4j2.xml

Both are under src/main/resources.

My question is about how to configure Tomcat and Spring Boot in a way that allow me to have a directory /home/oliver/conf which contains both of these files, in order to override the defaults defined under src/main/resources (which are then in WEB-INF/classes in the exploded WAR).

Below are the steps I've taken.

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 /etc/init.d/tomcat8 :

CATALINA_HOME=/usr/share/tomcat8
CATALINA_BASE=/home/oliver

My /home/oliver/conf folder, which holds the Tomcat and Spring configs, looks like this :

- Catalina/
- context.xml
- web.xml
- server.xml
- ...
- myApplication.properties
- log4j2.xml
- otherAppConfFile.properties
- ...

Because Spring looks for application.properties by default, I'm using the @PropertySource annotation to specify another file :

@SpringBootApplication
@PropertySource({classpath: myApplication.properties})
public class MyApp extends SpringBootServletInitializer {...}

I've tried to add -Dspring.config.location=file:/home/oliver/conf/myApplication.properties to JAVA_OPTS defined in /etc/default/tomcat8, and it works (overrides the embedded file correctly), but for instance if I try to add file:/etc/oliver/conf/log4j2.xml to the previous JVM parameter, it doesn't work.

I read a bit about Spring "environment profiles" but don't wish to use them if possible.

When I launch Tomcat and issue a ps aux | grep tomcat command, I see all the JAVA_OPTS parameters defined as expected, and I also see the following :

-classpath :/home/oliver/conf:/usr/share/tomcat8/bin/.... -Dcatalina.base=/home/oliver -Dcatalina.home=/usr/share/tomcat8

I'm a bit confused about the way Tomcat's classpath and Spring's are related, and how I should solve this issue.

If the classpath I see at launch includes the /home/oliver/conf 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 ?

EDIT :

As a side note, there might be a variety of files under /home/oliver/conf which would need to be taken into account, for instance log4j2.xml + myApplication.properties + keystore.jks so I'm not sure I can rely on -Dspring.config.location and -Dlogging.config entirely.

  • may be you look for Spring Cloud Config ? http://spring.io/projects/spring-cloud-config – Abder KRIMA Dec 19 '18 at 18:36
  • @TinyOS Thank you for your comment I will take a look, seems interesting but unfortunately I have very little leeway regarding this project, so adding another spring server just to manage configuration might not be possible. – Olivier Nappert Dec 19 '18 at 18:40
  • https://stackoverflow.com/a/27776123/4373948 or https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html – Abder KRIMA Dec 19 '18 at 18:43
  • Well i also checked that before asking but tbh it doesn’t really make it clearer.. – Olivier Nappert Dec 19 '18 at 19:19

1 Answers1

1

What I understand from your question is that you are trying to do something like this.

-Dspring.config.location=file:/etc/oliver/conf/log4j2.xml

I think the property spring.config.location is to provide the location of a properties file for configuration and not log4j2.xml. You can set the location of the log file in myApplication.properties by setting the value for logging.config like

logging.config=file:/etc/oliver/conf/log4j2.xml

Or else you can try

-Dlogging.config=file:/etc/oliver/conf/log4j2.xml

UPDATE

This is what I do in my production systems. Create a file setenv.sh and enter below command.

export JAVA_OPTS="$JAVA_OPTS -Djavax.net.ssl.trustStore=/path/to/keystore/keystore.jks -Djavax.net.ssl.trustStorePassword=changeit -Dspring.profiles.active=qa -Dspring.config.location=/path/to/config/ -Dfws_log=/path/to/logfile/location -Xms512m -Xmx1024m -Dsecret.key=somesecretkey"

You can add any number of key value mappings in this file and all of them will be loaded when your tomcat starts.

Sachin Kumar
  • 808
  • 3
  • 11
  • 29
  • Hi @Sachin Kumar thank you for your answer, it would work but the problem is that I may have more configuration files (for instance, log4j2.xml + myApplication.properties + keystore.jks). I don't think that adding the .jks with `-Dspring.config.location` would work, so I need to find another way of doing it .. – Olivier Nappert Dec 20 '18 at 14:58
  • @OlivierNappert I updated my answer to specify multiple properties. Hope this helps – Sachin Kumar Dec 21 '18 at 02:24