Spring-boot: -Dspring.profiles.active ignored when mvn spring-boot:run

Created on 13 Jun 2014  路  31Comments  路  Source: spring-projects/spring-boot

Upgrading 1.0.2.RELEASE->1.1.0.RELEASE changed how passing profile to mvn spring-boot:run works, the -Dspring.profiles.active flag seems to be ignored. I created a simple project that reproduces the problem: https://github.com/kryger/bug-spring-maven-profiles

_(Note that application.yml defines port=3333, application-production.yml defines port=1111)_

Steps to reproduce:

  1. Check out the project - it uses 1.1.0.RELEASE
  2. mvn spring-boot:run -Dspring.profiles.active=production -> Tomcat starts on port 3333 (expected 1111)
  3. Edit pom.xml and change <version> to 1.0.2.RELEASE
  4. mvn spring-boot:run -Dspring.profiles.active=production again -> Tomcat starts on port 1111

I had a look at 1.1.0.RELEASE's release notes but couldn't find anything that would explain this change - no idea if it's a bug or a feature :).

Most helpful comment

(For reference) The command should be:

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

All 31 comments

In 1.1.0 the plugin forks a process which is the reason why your system property is no longer available. The release notes indicate that the documentation of the maven plugin are now published.

You'll find an example there

Ah, thanks - I'll have a look and close the issue as soon as I confirm it works.

Cheers.

(For reference) The command should be:

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

I have also added a section in the release notes regarding this change. Thanks for the report!

@kryger I tried your way but I could not succeed. spring.profiles.active is not a maven/pom parameter. I think there must be another way to go on command line. Are you sure that it works? I think this issue should be opened.

By the way I am working with spring boot 1.2.5 release, maven 3, java 8u60

Sorry for misunderstanding, my fault! It was about another thing but this one works:

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

I have always liked "SPRING_PROFILES_ACTIVE=production mvn spring-boot:run". It's the way to pass lots of critical env variables to really any app, maven or whatever.

@gregturn
you method is work, thanks

It works using:
mvn spring-boot:run -Drun.profiles=default, primary, secondary

Please refer to: http://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-profiles.html

The following is ignored:
mvn spring-boot:run -Drun.profiles=development

However, using -Drun.jvmArguments works:
mvn spring-boot:run -Drun.jvmArguments="-Dspring.profiles.active=development"

Using: spring-boot-maven-plugin 1.5.9.RELEASE

That's to be expected if Maven forks a new JVM for the application. In your first example, the system property isn't being ignored, it's just being set in Maven's JVM rather than the application's JVM. The second example works as it ensures that the system property is passed to the JVM that's used to run the application.

Thanks @wilkinsona, that makes sense. But how does this fit to the documentation? This is quite misleading:

The profiles to enable can be specified on the command line as well, make sure to separate them with a comma, that is: mvn spring-boot:run -Drun.profiles=foo,bar

@timomeinen what version of Spring Boot are you using? run.profiles is indeed a property of the plugin in 1.x.

Oops. Sorry. @snicoll has just pointed out that it's run.profiles which is a property of the plugin and it should pass it to the app, irrespective of which JVM it's running in so my comment doesn't apply in this case.

@timomeinen alright so I think we need a sample that we can run that reproduces the behaviour you've described. Please create a separate issue with that.

Using 1.5.9.RELEASE. I will try to create a sample application.

The demo is working as expected. In my application just the logging is not working. This is because of my own configuration in logback-spring.xml as can be seen here: #5611

I am using Janino to get conditional processing in logback (because of the not working scan feature #5611): <if condition='property("spring.profiles.active").contains("development")'>

The problem is, that the Maven plugin won't set the spring.profiles.active System property. Is it possible for the plugin to forward this property as the system property spring.profiles.active?

That would also clarify the behaviour as given in the documentation for run.profiles:

Convenience shortcut of specifying the 'spring.profiles.active' argument.

The problem is, that the Maven plugin won't set the spring.profiles.active System property.

It does set spring.profiles.active as a command-line argument (--spring.profiles.active). The javadoc is correct as it states "argument". It doesn't state system property there at all.

This appears to be broken on version 2.0.0.RC2
if I revert to 1.5.10.RELEASE it works as expected. Maybe some code didn't get merged to that version?

@cyberoblivion What do you mean by "this"? No change was made as a result of this issue so there's nothing to have been merged. You should use -Drun.profiles to make sure that the profiles are set, irrespective of whether or not a new JVM is forked.

Oh sorry, yes -Drun.profiles does not seem to work in spring-boot-maven-plugin:2.0.0.RC2.
No active profiles are ever set in the RC2 version.
I can just change the pom to downgrade to 1.5.10.RELEASE and same command activates proper profile

example:
/apache-maven-3.5.0/bin/mvn spring-boot:run -Drun.profiles=myprofile

My mistake, it should be -Dspring-boot.run.profiles in 2.0. See https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/maven-plugin/examples/run-profiles.html for details.

This is also covered in the migration guide on the wiki

That works thanks.

Thanks @gregturn!
Your command works for me.

@wilkinsona - your suggestion worked for me: https://github.com/spring-projects/spring-boot/issues/1095#issuecomment-369324316

Sorry for misunderstanding, my fault! It was about another thing but this one works:

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

This worked

I tried on spring boot 2.
Two ways work

SPRING_PROFILES_ACTIVE=beta mvn spring-boot:run
mvn spring-boot:run -Dspring-boot.run.profiles=beta

I tried on spring boot 2.
Two ways work

SPRING_PROFILES_ACTIVE=beta mvn spring-boot:run
mvn spring-boot:run -Dspring-boot.run.profiles=beta

Good conclusion!
However there is another question

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

won't work for me (Springboot version 2.2.4.RELEASE), but when set it in IDEA, it works

image
image

I wonder if it is just a property to tell IDEA to start append -Dspring.profiles.active=dev when starting it with java?

jvm.arguments isn't a valid command-line property, see the maven plugin documentation. Please ask questions on StackOverflow, as mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.

Was this page helpful?
0 / 5 - 0 ratings