ThisIsNoZaku ThisIsNoZaku - 1 year ago 268
Java Question

Passing system properties into Spring Boot from Gradle

I want to pass certain security sensitive properties into my Spring Boot application, which I start via Gradle during development, via command-line and/or externally set system properties. I can't write these into things like the build.gradle script or properties files because they will be committed into source control and will then be publicly available.

I cannot get any of the solutions I've found for making these values available to an application Gradle executes to work using Gradle 2.4.4.

Several solutions look like this, where the system properties are set in the bootRun task:

bootRun {

This appears to completely overwrite the default bootRun task and breaks Spring Boot's automatic main class detection.

Somewhere else this was suggested:

bootRun {
run {systemProperties}

This doesn't override the normal configuration, but the properties set inside the run closure are not available inside the bootRun task.

This all occurs when I launch Gradle using Intellij.

If I start Gradle from the command-line and attempt to pass in a property way, for example:

gradle bootRun

The property is never set anywhere, regardless of configuration. Changing the order of the arguments has no effect.

Answer Source

Pass the JVM args as gradle property and forward them to the application using the following configuration:

bootRun {
    if (project.hasProperty('jvmArgs')) {
        jvmArgs project.jvmArgs.split('\\s+')

to apply jvmArgs run: ./gradlew bootrun -PjvmArgs=""

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download