2

I'm trying to understand the difference in Spring between -Drun.profiles and -Dspring.profiles.active.

Another answer in SO does not explain so much about the difference.

In my tests, both of them can be used to select a profile:

mvn spring-boot:run -Drun.profiles=prod

or

mvn spring-boot:run -Dspring.profiles.active=prod

So, what's the difference?

Dherik
  • 17,757
  • 11
  • 115
  • 164

1 Answers1

5

spring.profiles.active is one of the properties that Spring Boot applications support out of the box. Its used to specify at the level of Spring Boot application which profiles should be run.

Spring Boot supports many different properties, a full list can be found here.

Now, you won't find run.profiles among these properties, because its just a property that Spring Boot Maven plugin supports (and yes, it 'translates' it to the list of profiles to be used as well, so these properties might look similar), but the point is that -Drun.profiles will only work if you start the spring boot application with Maven plugin.

In production, however, the chances are that there won't be Maven at all, and the application will run as is (as a big jar) or even packed as Docker image or something. So for non maven-plugin usage you should use spring.profiles.active

The last point, that even in Maven --spring.profiles.active can be used, but it doesn't work out of the box. You should pass this parameter like this:

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

See this item in Github.

Hope this clarifies the differences between the two.

halfer
  • 19,824
  • 17
  • 99
  • 186
Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • plus, when using ``-Drun.profiles=prod`` with spring boot maven plugin, it is a convenience shortcut of specifying the ``spring.profiles.active`` argument. [see here](https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/maven-plugin/run-mojo.html#profiles) – HowieChih Dec 22 '17 at 08:04