Changing Spring Boot environment variables in the command line
This week while helping developers and testers to experiment with a backend application, some of them found useful to learn a simple trick to change Spring Boot properties when you can run the application locally (our testers build, compile, change the code, how cool is that?).
Here’s how it works. Say you have the following settings in your application’s application.properties:
my.application.database.username=sa
my.application.database.password=notasimplepassword
And that you want to change these parameters in order to, for instance, create an application error, so that you can code and test what happens to the frontend application in that situation.
You replace dots by underscores, and put all your words in upper case. So the variables above would be: MY_APPLICATION_DATABASE_USERNAME and MY_APPLICATION_DATABASE_PASSWORD.
Furthermore, you do not need to edit your application.properties file, if you are on Linux or Mac OS. You can start the application and override environment variables at the same time with the following syntax.
$ MY_APPLICATION_DATABASE_USERNAME=olivei MY_APPLICATION_DATABASE_PASSWORD=7655432222a mvn clean spring-boot:run
This way your application will start with the new values.
Happy hacking!
–EDIT–
As pointed by Stéphane Nicoll (thanks!), you could change the property values without having to use the upper case syntax.
mvn -Dmy.application.database.username=anotheruser clean spring-boot:run
And he even included a link to docs! ♥ the Internet and Open Source!
@kinow you can also specify them as command-line arguments (no need for the upper case thing)https://t.co/zBKpgcXN1C
— Stéphane Nicoll (@snicoll) November 22, 2016
or `-Drun.arguments`
Categories: Blog
Tags: Shell Script, Java