OK, so you use Gradle to build your Java projects. And if you use the application
plugin in Gradle then you get also the startup script generation so you can ship that straight into your prod servers. Thing is, the application
plugin generates a “standard” script — which does include the classpath and a whole bunch of other nice “defaults”. But what do you do if the defaults generated are not quite enough and you need a bit of customization of the generated script?
This is where we start looking at the CreateStartsScript task and see what hooks we have to customize the script. mrhaki has a good blog post about this where he uses a closure for startScripts
to modify the classpath.
This of course gives us the idea of using a similar Groovy closure to change the defaults. Let’s say for instance we want to modify the JVM parameters at startup — we simply use a closure like:
startScripts { unixScript.text = unixScript.text.replaceAll( 'DEFAULT_JVM_PARAMS.*', 'DEFAULT_JVM_PARAMS=\'"-Xms2g"\'') } |
(I’m not worried about the Windows startup script here as you can see, just the Linux/Unix one 🙂 )
I spent the other day some time trying to figure out how to get the right regex to “catch” the DEFAULT_JVM_PARAMS
declaration line — and then I had an Eureka moment: I don’t need to do all of this if all I need is just changing the JVM startup parameters. The CreateStartsScript
taks nowadays offers defaultJvmOpts
property which does just that 🙂
startScripts { defaultJvmParams = [ '-Xms2g', '-Xmx4g' ] } |
Note that this is an Iterable
so we use a List
not a simple string — i.e. ['-Xms2g', '-Xmx4g']
and NOT '-Xms2g -Xmx4g'
!
Problem solved. Should teach me to RTFM next time 🙂