Search code examples
springspring-bootspring-profiles

Setting Spring Profile variable


I have two Spring profiles: dev and test. I want to set the active profile in the server environment, I don't want to set it in my code so that wherever I deploy my application the profile gets loaded based on the profile in the server. How can I do that?


Solution

  • You can simply set a system property on the server as follows...

    -Dspring.profiles.active=test
    

    Edit: To add this to tomcat in eclipse, select Run -> Run Configurations and choose your Tomcat run configuration. Click the Arguments tab and add -Dspring.profiles.active=test at the end of VM arguments. Another way would be to add the property to your catalina.properties in your Servers project, but if you add it there omit the -D.

    Edit: For use with Spring Boot, you have an additional choice. You can pass the property as a program argument if you prepend the property with two dashes.

    Here are two examples using a Spring Boot executable jar file...

    System Property

    $ java -jar -Dspring.profiles.active=test myproject.jar
    

    Program Argument

    $ java -jar myproject.jar --spring.profiles.active=test