Search code examples
javagradlejvm-arguments

Why gradle application plugin fails to set JVM arguments


I have a Java program with a main() method which expects a system property -Dmy_sys_prop=SOME_VALUE. I use application plugin to execute my program. The configuration in my build.gradle looks like:

apply plugin: "java"
apply plugin: "maven"
apply plugin: "application"

sourceCompatibility = 1.6
targetCompatibility = 1.6

ext.mySysProp = '"-Dmy_sys_prop=' + mySysProp + '"'

applicationDefaultJvmArgs = [mySysProp]
mainClassName = "com.demo.SampleApp"

task wrapper(type: Wrapper) {
    gradleVersion = '2.0'
}

I am using following command to run the program.

./gradlew clean build run -PmySysProp=SOME_VALUE

I am not able to understand why mySysProp value is not available to my Java program.

Could someone help me here? Or, show me a better way to achieve this.

Thanks, NN


Solution

  • The quotes are off (this is not a shell environment where arguments can/need to be enclosed in quotes), and overriding mySysProp with an extra property of the same name is scary/confusing. Try:

    applicationDefaultJvmArgs = ["-DmySysProp=$mySysProp"]
    

    PS: I recommend to use def foo = "bar" instead of ext.foo = "bar" unless the property needs to be accessible from outer scopes and/or other build scripts (cf. local vs. global variable).