Search code examples
gradleintellij-13

How to set system properties in IntelliJ IDEA 13 gradle task?


I have a Spring Boot project with gradle build tool. The JDBC url, username and password are kept in a property file which is not part of application it's a external property file, the path of the property file is taken from system properties as follows.

export _JAVA_OPTIONS=-DdatabaseConfiguration=db.properties

It is working if I run the application from terminal using gradle bootRun, but when I try to run from Intellij IDEA 13 gradle tasks its not working, the property value is null.

I tried the VM options in Run/Debug Configuration as in the below screen shoot its not working either

Run/Debug Configuration

How can the JAVA_OPTIONS can be set in Intellij IDEA 13 gradle tasks.


Solution

  • This is because every time you use the Gradle tool window to kick off tasks in IntelliJ, it creates/overwrites the launch configuration for that task.

    Basically, I've had to run from the Gradle tool window just once. Then I go into the failed Launch Config (shown in question) and enter the system property in the VM options. From there on out, I need to use that Launch Config to execute the task instead of the Gradle tool window.

    Update: Even better solution:

    • Preferences->Build, Execution, Deployment->Build Tools->Gradle->Gradle VM options
    • Add your system properties there (i.e. -Dappengine.sdk.root=/opt/google/google-cloud-sdk/platform/appengine-java-sdk)

    Doing this will keep them from getting overwritten/lost in the Launch configs that the Gradle tool window generates.

    Another thing to note is that using the Gradle tool window causes the commands to be run without access to Environment Variables. This can cause a lot of problems with builds that depend on these env vars.

    I ran into this today with the appengine-gradle-plugin and had to put

    -Dappengine.sdk.root=/opt/google/google-cloud-sdk/platform/appengine-java-sdk

    in the VM options because it was not seeing the env vars. From the command line, it picks up the env vars and works fine. This worked for my appengineRun task.

    But it does not work for appengineUpdate since that gives another error caused by lack of env vars: Toolkit not found: apple.awt.CToolkit