Setting the default active profile in Spring-boot
I want my default active profile to be production
if -Dspring.profiles.active
is not set.
I tried the following in my application.properties
but it did't work:
spring.profiles.default=production
Spring-boot version = 1.3.5.RELEASE
Solution 1:
What you are doing here is setting the default default profile (the profile that is used on any bean if you don't specify the @Profile
annotation) to be production
.
What you actually need to do is set the default active profile, which is done like this:
spring.profiles.active=production
Solution 2:
add --spring.profiles.active=production
Example:
java -jar file.jar --spring.profiles.active=production
Solution 3:
We to faced similar issue while setting spring.profiles.active
in java.
This is what we figured out in the end, after trying four different ways of providing spring.profiles.active
.
In java-8
$ java --spring.profiles.active=dev -jar my-service.jar
Gives unrecognized --spring.profiles.active option.
$ java -jar my-service.jar --spring.profiles.active=dev
# This works fine
$ java -Dspring.profiles.active=dev -jar my-service.jar
# This works fine
$ java -jar my-service.jar -Dspring.profiles.active=dev
# This doesn't works
In java-11
$ java --spring.profiles.active=dev -jar my-service.jar
Gives unrecognized --spring.profiles.active option.
$ java -jar my-service.jar --spring.profiles.active=dev
# This doesn't works
$ java -Dspring.profiles.active=dev -jar my-service.jar
# This works fine
$ java -jar my-service.jar -Dspring.profiles.active=dev
# This doesn't works
NOTE: If you're specifying spring.profiles.active
in your application.properties
file then make sure you provide spring.config.location
or spring.config.additional-location
option to java accordingly as mentioned above.
Solution 4:
If you're using maven I would do something like this:
Being production your default profile:
<properties>
<activeProfile>production</activeProfile>
</properties>
And as an example of other profiles:
<profiles>
<!--Your default profile... selected if none specified-->
<profile>
<id>production</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<activeProfile>production</activeProfile>
</properties>
</profile>
<!--Profile 2-->
<profile>
<id>development</id>
<properties>
<activeProfile>development</activeProfile>
</properties>
</profile>
<!--Profile 3-->
<profile>
<id>otherprofile</id>
<properties>
<activeProfile>otherprofile</activeProfile>
</properties>
</profile>
<profiles>
In your application.properties you'll have to set:
spring.profiles.active=@activeProfile@
This works for me every time, hope it solves your problem.