I am getting confused about how to use profiles within a Spring Boot App.
Assuming that I defined profiles in pom.xml
file,
Here is the different ways that I find.
mvnw -Pdev
mvnw spring-boot:run -Dspring-boot.run.profiles=dev
mvnw spring-boot:run -Dspring.profiles.active=dev
mvnw spring-boot:run --spring.profiles.active=dev
1/ Is there any difference between them ?
2/ What is the best approach to use ?
You should not confuse Maven profiles and Spring profiles - this is completely different things.
mvnw spring-boot:run -Dspring.profiles.active=dev
- it is telling a Spring which profile to use at runtime.
Spring Profile - is how your Spring application will be configured at the runtime, based on the active profile. You can read more about it here and here.
mvnw -P dev
- it telling a Maven to use build Maven profile
Maven Profile - is how your Java application will be compiled/build/packed. It is related to the compiling/packaging, not runtime execution of your Spring application, and it doesn't related to Spring at all. You can read more about it here and here.
This is answer to your first question.
Regarding your second question,
Maven profile are used when you need to build your application differently based on the profile settings. For example, pack a "fat jar" with all dependencies when you execute mvn -P fat-jar
.
Spring profile is used when you want to configure your Spring application to work differently at runtime, for example - to use development
and production
database, etc.