Spring profiles are powerful tools to run the same application with different configurations.
There are several ways to activate profile(s) we want to use.
Standalone applications
For packaged applications, one of them is using spring.profiles.active
property.
As -D argument:
java -Dspring.profiles.active=xyz -jar demo-0.0.1-SNAPSHOT.jar
...
2019-12-13 19:24:58.058 INFO 12425 --- [ ] DemoApplication :
The following profiles are active: xyz
Or as ENV variable (underscores instead of dots, as dots are illegal in env variables):
export spring_profiles_active=qwe
java -jar demo-0.0.1-SNAPSHOT.jar
2019-12-13 19:28:47.777 INFO 12571 --- [ ] DemoApplication :
The following profiles are active: qwe
Maven mvn spring-boot:run
problem
But if you want to run same application with mvn spring-boot:run
and -D
parameter, it doesn’t work:
mvn spring-boot:run -Dspring.profiles.active=foo
2019-12-13 19:42:35.489 INFO 12952 --- [ ] DemoApplication :
No active profile set, falling back to default profiles: default
Maven mvn spring-boot:run solution
To fix it we have to pass profile name in parameter with other name - spring-boot.run.profiles
:
mvn spring-boot:run -Dspring-boot.run.profiles=foo
2019-12-13 19:48:29.587 INFO 13059 --- [ ] DemoApplication :
The following profiles are active: foo
For multiple profiles separate them with a comma:
mvn spring-boot:run -Dspring-boot.run.profiles=abc,def
Option with exported ENV variable works without modifications.
Why isn’t it working
Spring Boot Maven plugin by default forks application we are running and all argument we passed to application are lost. With spring-boot.run.profiles
we pass it to plugin and it knowns how to set expected profile in app.
If we disable forking:
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>false</fork>
</configuration>
</plugin>
</plugins>
and run with spring.profiles.active
parameter, it will work:
mvn spring-boot:run -Dspring.profiles.active=foo
2019-12-13 19:42:35.489 INFO 12952 --- [ ] DemoApplication :
The following profiles are active: foo