Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot and configuration profile

I have a Spring Boot application with some configuration. I want to specify different configuration values, for dev and production. My application.properties file look like this:

spring.datasource.url=jdbc:mysql://devhost:devport/devschema
spring.datasource.username=devuser
spring.datasource.password=mypwd
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

This file is in my git repo on Openshift. Everytime I commit, Openshift start to deploy the application, so I need the application.properties with the correct values. Is it possible to use something like profiles to change the configuration?

like image 728
Antonio Avatar asked Mar 05 '16 14:03

Antonio


2 Answers

You can use the spring.profiles.active Spring property to pick whichapplication-[environment].properties file to use. Then create multiple files, one for each environment. To set the environment just pass this --spring.profiles.active=development in your Java arguments.

like image 77
Jose Martinez Avatar answered Oct 19 '22 05:10

Jose Martinez


Create different property files for different profiles using application-{profile}.properties format, e.g. application-dev.properties for dev, and put your profile specific configurations in them. Then when you're running the spring boot application, activate your intended profile using the SPRING_PROFILES_ACTIVE environment variable or spring.profiles.active system property.

Since you're using Openshift, you can export the envirnemnt variable in the build file in the .openshift/action_hooks directory. Something like this:

export JAVA_OPTS_EXT=-Dspring.profiles.active="dev"

The better approach is to use rhc env set command for enabling one specific profile:

rhc env set SPRING_PROFILES_ACTIVE=dev -a App_Name

You can read more about using environment variables in openshift here.

like image 4
Ali Dehghani Avatar answered Oct 19 '22 04:10

Ali Dehghani