I am using maven with java Spring-boot.
How can efficiently handle configuration for different environment?
For instance, if I am on staging use this port, if I am on dev, use this port
Currently, I change all the necessary settings in the application.properties each time I move to a different environment
>Solution :
You can create different application.properties file for different environment. The environment name should be appended to the filename and separated with dash (-). For example,
you can have application-dev.properties for dev environment , you can also have ‘application-staging.propertiesfor staging environmentapplication-prod.properties` for production environment.
To activate each environment, pass it as a flag when are starting your .jar application for example
java -jar myapp.jar --spring.profile.active=staging
or you can define the active profile inside application.properties.
I.e (In the example below , your application will make use of the configuration inside application-staging.properties )
spring.profiles.active=staging #comment out to switch away from staging
#spring.profiles.active=dev # uncomment to set active profile to dev
#spring.profiles.active=prod # uncomment to set active profile to prod
You can now have different settings based on different environment in the custom created files.
For instance the staging environment could be configured to start on port 8888 as shown below

and the dev could be configured to start on port 8989

