Settings JVMargs per instance type

So I was setting up my devops and realized that I had a defauklt set of JVMArgs.

But I realized that those would need to be changed per ‘instance’ in clouds environments (ie nano/medium/large) and to have a ‘default’ if no instance is set.

So I thought I would post here how I resolved this.

Basically in my ‘gradle.properties’ I added the following properties like so:

configType.nano="-Xms512m -Xmx1024m -XX:+UseParallelGC  -XX:ParallelGCThreads=4 -XX:MaxGCPauseMillis=200 -XX:MaxGCMinorPauseMillis=75 -server -noverify  -Xshare:off  -Djava.net.preferIPv4Stack=true -XX:+EliminateLocks -XX:GCTimeRatio=7 -Djava.security.egd=file:/dev/urandom -Djdk.map.althashing.threshold=512"
configType.medium="-Xms1024m -Xmx2048m -XX:+UseParallelGC  -XX:ParallelGCThreads=8 -XX:MaxGCPauseMillis=200 -XX:MaxGCMinorPauseMillis=75 -server -noverify  -Xshare:off  -Djava.net.preferIPv4Stack=true -XX:+EliminateLocks -XX:GCTimeRatio=7 -Djava.security.egd=file:/dev/urandom -Djdk.map.althashing.threshold=512"
configType.large="-Xms4048m -Xmx4096m -XX:+UseParallelGC  -XX:ParallelGCThreads=16 -XX:MaxGCPauseMillis=200 -XX:MaxGCMinorPauseMillis=75 -server -noverify  -Xshare:off  -Djava.net.preferIPv4Stack=true -XX:+EliminateLocks -XX:GCTimeRatio=7 -Djava.security.egd=file:/dev/urandom -Djdk.map.althashing.threshold=512"
configTypeDefault=nano

Then in the ‘build.gradle’, you can read in the JVMargs as a CLI property:

ext.javaMainClass = "demo.application.Application"

// -Pargs=configType=${configType}
application {
	mainClass = javaMainClass
	applicationDefaultJvmArgs = [project.property('configType.'+configTypeDefault)]
	if (project.hasProperty("args")) {
		List temp = project.args.split(',')
		temp.each(){
			List l = args.split('=')
			if(l[0]=='configType' && project.property('configType.'+l[1])) {
				applicationDefaultJvmArgs = [project.property('configType.'+l[1])]
			}
		}
	}
}

NOTE: Make sure to change your ‘javaMainClass’

This allows you to pass your set your JVMArgs in your gradle properties per instance (with a default type) and pass it as a CLI arg when building your artifact.

gradle clean build --stacktrace  -Pargs=configType=nano