others-how to add JVM args/options/parameters when using gradle?

1. Purpose

In this post, I will demonstrate how to add JVM args/options/parameters when using gradle.



2. Solution

First add the application plugin in build.gradle as follows:

plugins {
    id("application")
}

The Application plugin facilitates creating an executable JVM application. It makes it easy to start the application locally during development, and to package the application as a TAR and/or ZIP including operating system specific start scripts.

Applying the Application plugin also implicitly applies the Java plugin. The main source set is effectively the “application”.

Applying the Application plugin also implicitly applies the Distribution plugin. A main distribution is created that packages up the application, including code dependencies and generated start scripts.

Then add options to application block as follows in build.gradle:

application {
    mainClass = 'com.bswen.myapp.Main'
    applicationDefaultJvmArgs = listOf("-javaagent:/Users/bswen/tech/skywalking/agent/skywalking-agent/skywalking-agent.jar",
        "-Dskywalking_config=/Users/bswen/KotlinProjects/blog/blog/skywalking/agent.config")
}

And if you have only one JVM argument, you can just do as follows:

application {
    applicationDefaultJvmArgs = ['-Dgreeting.language=en']
}

You can see that we can use applicationDefaultJvmArgs to set the JVM arguments which the application use to start.

3. Summary

In this post, I demonstrated how to set JVM arguments in gradle when using gradle . That’s it, thanks for your reading.