13

I use the gradle application plugin to generate the application folder. The installApp task provides a start script for me, but I have no idea how to set the jvm args from build.gradle.

Some jvm args I needed, such as file.encoding. I just modify the start script to set the DEFAULT_JVM_OPTS variable

#!/usr/bin/env bash

##############################################################################
##
##  MuzeeS3Deployer start up script for UN*X
##
##############################################################################

# Add default JVM options here. You can also use JAVA_OPTS and MUZEE_S_DEPLOYER_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=" -Dfile.encoding=utf-8 "

If the args not set, my console cannot show messages well:

qty:MuzeeS3Deployer qrtt1$ ./build/install/MuzeeS3Deployer/bin/MuzeeS3Deployer d
2012/10/14 #U###12:02:03 SyncCommand main
ĵ#i: no aws credentials found at /Users/qrtt1/AwsCredentials.properties

When I set the encoding:

qty:MuzeeS3Deployer qrtt1$ ./build/install/MuzeeS3Deployer/bin/MuzeeS3Deployer d
2012/10/14 下午 12:04:19 SyncCommand main
警告: no aws credentials found at /Users/qrtt1/AwsCredentials.properties

I got the solution from @Peter. Finally, I make a minor changes to the scripts:

startScripts {
    doLast {
        unixScript.text = unixScript.text.replace('DEFAULT_JVM_OPTS=""', 'DEFAULT_JVM_OPTS="-Dfile.encoding=utf-8"')
        windowsScript.text = windowsScript.text.replace('DEFAULT_JVM_OPTS=', 'DEFAULT_JVM_OPTS="-Dfile.encoding=utf-8"')
    }
}
Opal
  • 81,889
  • 28
  • 189
  • 210
qrtt1
  • 7,746
  • 8
  • 42
  • 62

2 Answers2

27

Support for JVM arguments was added in Gradle 1.7: https://docs.gradle.org/current/userguide/application_plugin.html#configureApplicationDefaultJvmArgs

For example, for setting file.encoding, you can do:

applicationDefaultJvmArgs = ['-Dfile.encoding=utf-8']
Eduardo
  • 319
  • 2
  • 14
Alex
  • 2,589
  • 3
  • 35
  • 44
10

There is currently no special support for setting DEFAULT_JVM_OPTS. However, you can do something like:

startScripts {
    doLast {
        unixScript.text = unixScript.text.replace('DEFAULT_JVM_OPTS=""', 'DEFAULT_JVM_OPTS="-Dfile.encoding=utf-8"')
    }
}

You may want to do something similar for windowsScript.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • 6
    Do not use this anymore! This sollution is outdated. – Tobias Kremer Feb 26 '14 at 15:59
  • 1
    yes, now you can add in build.gradle something like: applicationDefaultJvmArgs = ['-Dxxxxx'] – YaP Oct 20 '17 at 15:43
  • 1
    the issue would be if you need to enter different settings for Windows and Linux (f.e. paths). in that case, the solution from Peter seems to be easiest one. – YaP Oct 20 '17 at 15:53