Using env variable in Spring Boot's application.properties Using env variable in Spring Boot's application.properties java java

Using env variable in Spring Boot's application.properties


You don't need to use java variables. To include system env variables add the following to your application.properties file:

spring.datasource.url = ${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT}/"nameofDB"spring.datasource.username = ${OPENSHIFT_MYSQL_DB_USERNAME}spring.datasource.password = ${OPENSHIFT_MYSQL_DB_PASSWORD}

But the way suggested by @Stefan Isele is more preferable, because in this case you have to declare just one env variable: spring.profiles.active. Spring will read the appropriate property file automatically by application-{profile-name}.properties template.


The easiest way to have different configurationsfor different environments is to use spring profiles.See externalised configuration.

This gives you a lot of flexibility.I am using it in my projects and it is extremely helpful.In your case you would have 3 profiles:'local', 'jenkins', and 'openshift'

You then have 3 profile specific property files:application-local.properties,application-jenkins.properties,and application-openshift.properties

There you can set the properties for the regarding environment.When you run the app you have to specify the profile to activate like this:-Dspring.profiles.active=jenkins

Edit

According to the spring doc you can set the system environment variableSPRING_PROFILES_ACTIVE to activate profiles and don't needto pass it as a parameter.

is there any way to pass active profile option for web app at run time ?

No.Spring determines the active profiles as one of the first steps,when building the application context.The active profiles are then used to decide which property files are read and which beans are instantiated.Once the application is started this cannot be changed.


Flyway doesn't recognize the direct environment variables into the application.properties (Spring-Boot V2.1).e.g

spring.datasource.url=jdbc:mysql://${DB_HOSTNAME}:${DB_PORT}/${DB_DATABASE}spring.datasource.username=${DB_USER}spring.datasource.password=${DB_PASS}

To solve this issue I did this environment variables, usually I create the file .env:

SPRING_DATASOURCE_URL=jdbc:mysql://127.0.0.1:3306/placeSPRING_DATASOURCE_USERNAME=rootSPRING_DATASOURCE_PASSWORD=root

And export the variables to my environment:

export $(cat .env | xargs)

And finally just run the command

mvn spring-boot:run

Or run your jar file

java -jar target/your-file.jar

There another approach here: https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/maven-plugin/examples/run-env-variables.html