-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Delegate command line system properties to spring-boot-gradle-plugin runApp task #592
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
Conversation
…ties to the JavaExec of the runApp tasks. This fixes a issue that caused sytem properties that are provided to gradle via command line were not known to the spring boot application. For example, running 'gradle bootRun -Pspring.config.location=/some/custom/location/application.properties' did not work and the external config file was not loaded.
I'm not sure this is a great idea. As far as I can tell Project properties in Gradle (-P...) are not really supposed to be the same as System properties. Your patch, for example, would be useless to pass reserved property names to the app: try "gradle -Pname=Dave" and see what I mean. Also the Gradle guys seem to prefer that you propagate JVM arguments and System properties explicitly from the command line in your |
Okay understood. But that does not solve my problem. The idea is that the location of the global configuration file of the system is not part of the build script, as it should be configured for each developer working on the project. I somehow need to delegate the "spring.config.location" property to the apps when running bootRun task. |
Then don't you need to add something to your applicationDefaultJvmArgs =
[ "-Dspring.config.location=${properties['configLocation']?:'classpath:/application.properties'}" ] |
Okay, now I ended up doing the following: run {
systemProperties = System.properties
} Now the command
works. I am still not sure if this is the "correct" way. Maybe its more a gradle specific topic. I am going to ask the gradle forum for that. Still, thanks for your help. |
That seems like the right way to do it. Please come back and tell us if you find anything else out. I'm also thinking there needs to be a way to set command line arguments, but you can do pretty much everything in a regular Boot app with System properties, so maybe not urgent. |
I think the main problem here is that it is not immediately obvious that the bootRun extends the application plugin at all. I would expect to be able to do something like:
in my .gradle file. But the way the bootRun task extends the run task doesn't let me do that. The Grails Gradle plugin allows for properties to be passed via the grailsArgs property:
It would be great if boot had a similar mechanism for this. |
@lando thanks for the solution, exactly what i was looking for. |
Hi Guys! I don't see that this issue has ever been resolved. In fact I think it isn't. I'm running a project on Spring boot 1.1.4 and couldn't have passed any params to my bootRun task. In other words:
failed completely. As a workaround I tried many things that were described in this PR but what worked for me was:
Why on earth does Spring boot take args from a project property called 'applicationDefaultJvmArgs' ? |
Isn't that code just mapping "jvmArgs" to something more obscure that is needed by the gradle code that we wrap? I assume "jvmArgs" or "allJvmArgs" or "systemProperties" would work just as well (see http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.JavaExec.html). And there was never any indication that this issue was resolved. It may be resolved at some point and marked as "Won'tFix" because it seems to be a general problem with gradle, not specifically anything to do with Boot. Or maybe not.Tomas's point that the Grails plugin has taken the lead and exposed properties in a more friendly way is well made: we could have a stronger position here, but the pull request as it stands is not going to work, so it's waiting for someone to fix it. |
Hi @dsyer! Thanks for your immediate answer. Maybe I did sth wrong but I've been trying to pass all the possible params to the JavaExec task and nothing worked for me (the passed props were always ignored). Anyway this works for me as a workaround so I'll stick to that until there is some other way to fix it. |
@marcingrzejszczak - we end up doing something like this. tasks.withType(org.springframework.boot.gradle.run.BootRunTask) {
systemProperty('netflix.environment', 'test')
} You can use the -P project variable mechanism in gradle to externalize your variables, but it's not dynamic - http://mrhaki.blogspot.com/2010/10/gradle-goodness-pass-command-line.html |
@tomaslin thx for the info - it looks nicer than our hack ;) |
Any updates on the issue? |
I am working on a Spring Boot Project. We need different application.properties for every team member. Nothing from this discussion worked for me so i found another way to get it work. My solution:
In Eclipse i added the following to my Gradle launch config: I Think this should work out of the box. |
I have this in my
If there's an environment variable in existence it will take precedence. So I can do |
Why is is this so complicated?! There are several times where you want to test passing in a JVM property to the actual runtime of your developer application rather than to the Gradle runtime. You shouldn't have to set this of every property you wish to pass. So, if this is a different commandline syntax or a plugin to accept -Gabc.xyz="foo" then that would be fine (like Grails does it). |
Whilst it would be nice to offer an easier way to pass properties, we'd prefer to do it like Grails does
|
@itonics-tbeauvais #1176 is open to add Grails like support. |
So how do you pass JVM properties then? Where is the documentation covering how to do it, as recommended? |
@cyotee We're still looking at adding first class support for arguments (see #1176). We have added some updated documentation for the 2.0 release, but it doesn't specifically document how to pass arguments. Instead it states:
I realize that's a little open ended, but since Gradle is so flexible I'm not sure there is a single recommended approach. So far this seems like the most reasonable solution. If you're interested in contributing some additional documentation, this is the asciidoctor source file you should edit. |
It seems to me, the "most reasonable solution" doesn't work any longer. Starting from 2.0.0-M5 I needed to change the bootRun closure to this:
This does enable us to use: |
There's already an example for setting a system property:
That mechanism gives you access to anything that you configure on a |
Hi, |
Sorry for the churn. |
Thank you very much for the info, @wilkinsona! |
I have create a variable 'profiles' and configure gradle as following: bootRun {
if (project.hasProperty('profiles')) {
environment SPRING_PROFILES_ACTIVE: profiles
} else {
def profiles = 'local'
environment SPRING_PROFILES_ACTIVE: profiles
}
} Then at command line, give following arguments in order to pick up respective profiles
In order to pickup local profile as default, no arguments required
|
The best way that a found was: just specificy what profile do you want and be happy. I believe thats its best way to do that. I hope that it works to another person. Cheers! |
What does this line mean |
The spring-boot-gradle-plugin now delegates configured project properties to the JavaExec of the runApp tasks. This fixes a issue that caused sytem properties that are provided to gradle via command line were not known to the spring boot application. For example, running 'gradle bootRun -Pspring.config.location=/some/custom/location/application.properties' did not work and the external config file was not loaded.