how to use Spring Boot profiles

i have application.yml,application-dev.ymlandapplication-dev.yml

  1. I'm using the maven command mvn spring-boot:run -Dspring.profiles.active=dev it doesn't work and I can not choose the dev profile using mvn spring-boot:run. How do I choose it?
  2. The documentation says java -jar XXX.jar --spring.profiles.active=dev works, and I tried -Dspring.profiles.active=dev but it does not work. And in my project, i use java -jar XXX.jar it runs, but if I use java -jar XXX.jar --spring.profiles.active=dev to choose dev profile, console print so many logs and warns that i never see used java -jar XXX.jar,and tell me APPLICATION FAILED TO START

so how to solve two problems? thanks~


Solution 1:

I'm not sure I fully understand the question but I'll attempt to answer by providing a few details about profiles in Spring Boot.

For your #1 example, according to the docs you can select the profile using the Spring Boot Maven plugin using -Drun.profiles.

Edit: For Spring Boot 2.0+ run has been renamed to spring-boot.run and run.profiles has been renamed to spring-boot.run.profiles

mvn spring-boot:run -Dspring-boot.run.profiles=dev

https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/maven-plugin/examples/run-profiles.html

From your #2 example, you are defining the active profile after the name of the jar. You need to provide the JVM argument before the name of the jar you are running.

java -jar -Dspring.profiles.active=dev XXX.jar

General info:

You mention that you have both an application.yml and a application-dev.yml. Running with the dev profile will actually load both config files. Values from application-dev.yml will override the same values provided by application.yml but values from both yml files will be loaded.

There are also multiple ways to define the active profile.

You can define them as you did, using -Dspring.profiles.active when running your jar. You can also set the profile using a SPRING_PROFILES_ACTIVE environment variable or a spring.profiles.active system property.

More info can be found here: https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-set-active-spring-profiles

Solution 2:

If you are using the Spring Boot Maven Plugin, run:

mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar

(https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-profiles.html)

Solution 3:

Since Spring Boot v2+

Alternatives below works with Spring Boot v2.0.0 and newer.

With Spring Boot Maven Plugin

You can provide commandline argument like this:

mvn spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=dev"

You can provide JVM argument like this:

mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=dev"

java -jar

java -Dspring.profiles.active=dev -jar app.jar (VM param)

or

java -jar app.jar --spring.profiles.active=dev (program param)

A note on Windows Powershell

Powershell will require you to wrap in double quotes like shown here since it otherwise will result in an error because of how it interpret a period: java -D"spring.profiles.active=dev" -jar app.jar