Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kryger opened this issue Jun 13, 2014 · 33 comments
Closed

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

kryger opened this issue Jun 13, 2014 · 33 comments

Comments

@kryger
Copy link
Contributor

kryger commented Jun 13, 2014

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 :).

@snicoll snicoll self-assigned this Jun 13, 2014
@snicoll
Copy link
Member

snicoll commented Jun 13, 2014

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

@kryger
Copy link
Contributor Author

kryger commented Jun 13, 2014

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

@snicoll
Copy link
Member

snicoll commented Jun 13, 2014

Cheers.

@kryger
Copy link
Contributor Author

kryger commented Jun 13, 2014

(For reference) The command should be:

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

@kryger kryger closed this as completed Jun 13, 2014
@snicoll
Copy link
Member

snicoll commented Jun 13, 2014

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

@lacivert
Copy link

@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

@lacivert
Copy link

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

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

@gregturn
Copy link
Contributor

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.

@clevertension
Copy link
Contributor

@gregturn
you method is work, thanks

@carloscorva
Copy link

carloscorva commented Feb 14, 2017

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

@timomeinen
Copy link

timomeinen commented Jan 4, 2018

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

@wilkinsona
Copy link
Member

wilkinsona commented Jan 4, 2018

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.

@timomeinen
Copy link

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

@snicoll
Copy link
Member

snicoll commented Jan 4, 2018

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

@wilkinsona
Copy link
Member

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.

@snicoll
Copy link
Member

snicoll commented Jan 4, 2018

@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.

@timomeinen
Copy link

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

@timomeinen
Copy link

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.

@snicoll
Copy link
Member

snicoll commented Jan 4, 2018

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.

@cyberoblivion
Copy link

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?

@wilkinsona
Copy link
Member

@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.

@cyberoblivion
Copy link

cyberoblivion commented Feb 28, 2018

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

@wilkinsona
Copy link
Member

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.

@snicoll
Copy link
Member

snicoll commented Feb 28, 2018

This is also covered in the migration guide on the wiki

@cyberoblivion
Copy link

That works thanks.

udinnet added a commit to udinnet/micro-services that referenced this issue May 29, 2018
udinnet added a commit to udinnet/micro-services that referenced this issue May 29, 2018
n0ps added a commit to finnishtransportagency/ksr that referenced this issue May 31, 2018
Switch from spring.profiles.active to spring-boot.run.profiles
See spring-projects/spring-boot#1095 (comment)
mafelt pushed a commit to finnishtransportagency/ksr that referenced this issue Jun 4, 2018
Switch from spring.profiles.active to spring-boot.run.profiles
See
spring-projects/spring-boot#1095 (comment)
@hendisantika
Copy link

Thanks @gregturn!
Your command works for me.

@orbmis
Copy link

orbmis commented Apr 26, 2019

@wilkinsona - your suggestion worked for me: #1095 (comment)

@yashwanth2804
Copy link

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

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

This worked

@fireflyk
Copy link

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

@lovelock

This comment has been minimized.

@snicoll

This comment has been minimized.

@bartieres
Copy link

bartieres commented Jul 21, 2021

This worked: mvn spring-boot:run -Dspring-boot.run.profiles=hom

@bglamadrid
Copy link

bglamadrid commented Aug 18, 2021

For Spring Boot 2 under PowerShell, this only worked surrounding dots within quotes, for example mvn spring-boot:run "-Dspring-boot.run.profiles=default,local,h2,local-h2"
Without these quotes, maven complains about unknown lifecycle phases.
And yes, mvn spring-boot:run -Dspring-boot".run."profiles=default,local,h2,local-h2 works just the same but it's kinda counter-intuitive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests