juniorDeveloper
juniorDeveloper

Reputation: 225

Spring boot deployed as WAR on tomcat, How to Externalize application.properties for different profiles(Dev,Test,Staging,Prod)

I am in a scenario where I am deploying by spring boot as a WAR on tomcat. Here in this application I have application.properties which has database username/password , some URL which help in consuming rest services(urls vary depending on the environment). Now I need to get the DB credentials and URL's depending on the environment it is deployed to. how to achieve it.

Upvotes: 3

Views: 6320

Answers (4)

Salvatore Napoli
Salvatore Napoli

Reputation: 444

On external container ex tomcat You can pass configuration using Jndi variable in context.xml This var override local property defined in application.properties

Or pass -Dspring.profiles.active=env in tomcat startup script ,for select environment specific application.properties if you prefer to have configuration inside the war

Upvotes: 2

juniorDeveloper
juniorDeveloper

Reputation: 225

I am able to read the properties file from tomcat/webapp location

for every environment I can ask system engineers to drop the file at the location, I dont know if its a good solution. Please suggest

@PropertySource(value={"file:C:/Users/foo/Downloads/apache-tomcat-8.5.28/webapps/application.properties"})

Upvotes: 1

slemoine
slemoine

Reputation: 367

according to the discussion with Karol, I guess using properties sources referencing file system path should be ok:

@Configuration
@PropertySource("${mywebapp.config.dir}/application.properties")
public class SpringConfig {
}

And just path JVM args at the startup of tomcat like

-Dmywebapp.config.dir=file:/etc/mywebapp

This way you can deploy one application.properties different for each environment.

As for your comment:

@PropertySource(value={"file:C:/Users/foo/apache-tomcat-8.5.28/webapps/application.properties}"})

as it's a windows system path may you have to double backslash your path: C:\\Users\\foo ...

Upvotes: 1

Karol Dowbecki
Karol Dowbecki

Reputation: 44952

As per 24. Externalized Configuration it should be enough to place a profile specific properties e.g. application-dev.properties on the classpath. The property precedence is:

...

  1. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
  2. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
  3. Application properties outside of your packaged jar (application.properties and YAML variants).
  4. Application properties packaged inside your jar (application.properties and YAML variants).

...

This can however work slight differently if you are packaging as JAR as per 24.3 Application Property Files. The property precedence is:

  1. A /config subdirectory of the current directory
  2. The current directory
  3. A classpath /config package
  4. The classpath root

Upvotes: 1

Related Questions