0

Can anyone tell me if it is possible to define the profile (Dev, Prod, Local) in a spring-loaded application by the environment in which the application is running?

My application is configured with a profile (Dev, Prod, Local), but every time the developer needs to set the active profile for each environment he will run the project

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
Ger
  • 583
  • 2
  • 13
  • 35
  • I have local and cloud in my application.yml. You can have a separate manifest-env.yml for each environment. – duffymo Feb 18 '19 at 20:38
  • In my application there are 3 application.properties, (Dev, homo, prod). In the master application there is the property spring.profiles.active = local to use in development and another one for production. I would like to perform an automated task where the developer does not need to remember to change this property in the application.properties file, running the risk of going wrong in production. I thought of something using maven's profile where jenkins would do the build according to environment, but it did not work – Ger Feb 18 '19 at 20:50
  • i am sure you have/use a (sort of) run script in every environment.... set it there like `java ... -Dspring.profiles.active=prod` – xerx593 Feb 18 '19 at 21:03
  • https://stackoverflow.com/questions/38520638/how-to-set-spring-profile-from-system-variable this will help you – kamlesh pandey Feb 19 '19 at 03:53

2 Answers2

3

I generally have a default profile for development and one shell script file for each environment.

You can set a default profile in your application.yml, like this:

spring:
  profiles.active: development

And you can pass a profile in the shell script, like this:

java jar my-app.jar --spring.profiles.active="PROD"
  • We do not have this approach with shell script, the build is done by jenkins. But I may be able to do that approach over there. Thanks for the reply Leandro Rosa I will try – Ger Feb 19 '19 at 11:52
1

Lean on your defaults - you get the default profile for free, and that's the one most developers should be using.

You can also add a custom profile to your ~/.spring-boot-devtools.properties file (provided devtools is active - check this reference) to override that default profile and set your own.

This also implies you pass in the profile you want to run in production. Do not set the production profile in your pre-packaged application.properties (or application.yml).

Makoto
  • 104,088
  • 27
  • 192
  • 230