3

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 {
    systemProperties System.properties
}

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 System.properties}
}

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 -Dspring.email.password=123 bootRun

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

ThisIsNoZaku
  • 2,213
  • 2
  • 26
  • 37

1 Answers1

5

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="-Dspring.profiles.active=myprofile -Dspring.email.password=secret"

fateddy
  • 6,887
  • 3
  • 22
  • 26
  • 1
    It's a clever way of doing it. Wondering if there is any similar to "mvn -Dspring.profiles.active=..." in Maven. Sounds like in Gradle, there is no easy way to pass in task scope property ? – tuan.dinh Jan 28 '20 at 01:58
  • with gradle you can always pass along properties as cli-argument (eg. `-P prop=foo`) and access them in your tasks (`project.properties[“prop”]`). You can always write your own logic using groovy (or kotlin). – fateddy Jan 29 '20 at 12:12