Best way to pass an environment variable to several config files

436 views Asked by At

When deploying an application (in Java with Spring framework on a tomcat server), I have to manually modify several config files before packaging the .war depending on the target environment (dev, prod, qualif, ...)

These modifications are on for now three files (a database config file, a new relic config file and another custom config file). But with the app growing it may affect more files.

I wonder if there is a better way to automatically modify these files when starting the app. Maybe with a custom parameter in command line ? I'm not sure what is the proper way to do that.

2

There are 2 answers

1
Stefan On

Use Maven profiles and filter your text resources as described here:

<profiles>
 <profile>
  <activation>
   <property>
    <name>environment</name>
    <value>test</value>
   </property>
  </activation>
 </profile>
</profiles>
0
questionaire On

I don't know what's the "best way". But I know two options.

Either you could externalize the configuration files and locate them sepperated from your application. This way you can modify your properties the way you want, without the need to redeploy the application after that.

You only have to add the folder to your classpath.

In addition to that, in Spring there is the possibility to use Profiles.

This way you can define your properties and start with the fitting profile in the specific environment.