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?

- 7,199
- 9
- 45
- 86

- 1,982
- 5
- 19
- 22
6 Answers
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
-
2I am new to Spring profiles ."ON THE SERVER". Where and how exactly do I need to set it. – Maheshwaran K Apr 04 '13 at 14:48
-
One more thing is that, I am starting tomcat through eclipse and not through commmand line. – Maheshwaran K Apr 04 '13 at 14:51
-
8What server are you using? If you are using tomcat, you can add this to the $CATALINA_HOME/conf/catalina.properties but omit the -D so you would just add spring.profiles.active=test – hyness Apr 04 '13 at 14:51
-
1`--spring.profiles.active=test` is a Spring Boot paradigm which did not exist in 2013 when I wrote this answer. If you choose to use it, it must be used as a program argument, which comes after the class or jar name. The `-Dspring.profiles.active=test` approach, which uses system properties, still works, but it must come before the class or jar name. I'll try to update this answer to better reflect the current spring approaches. – hyness May 26 '16 at 18:22
-
Is there any way to have different settings for each webapp / context? If I deploy Spring Boot app1 and want it to run with profile x and Spring Boot app2 with profile y, setting a environment var or JVM system property won't work. Is there any way to initialize the profile from a context environment? – djb Jul 27 '16 at 21:01
-
If you are using Spring Boot, you should be using an embedded tomcat, so you would be free to use different program arguments, system properties or even environment variables since each app would be running in a separate jvm. – hyness Jul 29 '16 at 16:00
-
neat, `-Dspring.profiles.active=dev` works as jvm argument, not `-DSPRING_PROFILES_ACTIVE=dev`. (useful while deploying using amazon elastic beanstalk) – prayagupa Jul 28 '17 at 19:21
-
But with this, when you run this on the server, you have to use nohup right? Or this also works when running the executable jar as a service? – Gabriel Feb 08 '18 at 19:11
-
can i change profile name while system running (profile for different data sources ) – Shaaban Ebrahim Mar 14 '18 at 10:07
There are at least two ways to do that:
defining context param in web.xml – that breaks "one package for all environments" statement. I don't recommend that
defining system property
-Dspring.profiles.active=your-active-profile
I believe that defining system property is a much better approach. So how to define system property for Tomcat? On the internet I could find a lot of advice like "modify catalina.sh" because you will not find any configuration file for doing stuff like that. Modifying catalina.sh is a dirty unmaintainable solution. There is a better way to do that.
Just create file setenv.sh in Tomcat's bin directory with content:
JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=dev"
and it will be loaded automatically during running catalina.sh start or run.
Here is a blog describing the above solution.
-
9For windows use set JAVA_OPTS=%JAVA_OPTS% -Dspring.profiles.active=dev (no quotes) – ThomasRS Aug 20 '15 at 08:46
-
3I think, it's better to use CATALINA_OPTS see : http://stackoverflow.com/questions/7738794/add-jvm-options-in-tomcat – Ortomala Lokni Nov 18 '16 at 14:54
-
2
For Eclipse, setting -Dspring.profiles.active
variable in the VM arguments should do the trick.
Go to
Right Click Project --> Run as --> Run Configurations --> Arguments
And add your -Dspring.profiles.active=dev
in the VM arguments

- 12,010
- 6
- 65
- 78
as System environment Variable:
Windows:
Start -> type "envi" select environment variables and add a new:
Name: spring_profiles_active
Value: dev
(or whatever yours is)
Linux: add following line to /etc/environment under PATH:
spring_profiles_active=prod
(or whatever profile is)
then also export spring_profiles_active=prod
so you have it in the runtime now.

- 1,813
- 21
- 29
-
This works great (tested in windows), also when running your application in an OSGI container. Just remember to restart the JVA instance after setting it, and make sure that the System Property named "spring.profiles.active" is unset. FYI, this does not actually set the System Property, but the Spring Environment instance profile. Thanks! – Tormod Haugene Sep 07 '17 at 09:00
In the <tomcat-home>\conf\catalina.properties
file, add this new line:
spring.profiles.active=dev

- 9,715
- 8
- 45
- 66

- 790
- 1
- 7
- 9
For Tomcat 8:
Linux :
Create setenv.sh and update it with following:
export SPRING_PROFILES_ACTIVE=dev
Windows:
Create setenv.bat and update it with following:
set SPRING_PROFILES_ACTIVE=dev

- 482
- 4
- 9