Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

Gradle is a multi-purpose automation build tool to develop, compile, and test software packages. It supports a wide range of languages, but primarily, we use it for Java-based languages like Kotlin, Groovy, and Scala.

While working with Java, we might need to customize the JVM arguments in a Java application. As we are using Gradle to build a Java application, we can also customize the JVM arguments of the application by tuning the Gradle configuration.

In this tutorial, we’ll learn to pass the JVM arguments from the Gradle bootRun to a Spring Boot Java application.

2. Understanding bootRun

Gradle bootRun is a gradle-specified task that comes with the default Spring Boot Gradle Plugin. It helps us to run the Spring Boot application from Gradle itself directly. Executing the bootRun command starts our application in a development environment, which is very useful for testing and development purposes. Primarily, it is used for iterative development as it doesn’t need any separate build or deployment purposes.

In short, it provides a simplified way to build an application in a dev environment and execute tasks related to spring boot development.

3. Using jvmArgs in build.gradle File

Gradle provides a straightforward way to add JVM args to the bootRun command using the build.gradle file. To illustrate, let’s look at the command to add JVM args to a spring boot application using the bootRun command:

bootRun {
    jvmArgs([
        "-Xms256m",
        "-Xmx512m"
    ])
}

As we can see, the max/min heap of the springboot application is modified using the jvmArgs option. Now, let’s verify the JVM changes to the spring boot application using the ps command:

$ ps -ef | grep java | grep spring
502  7870  7254   0  8:07PM ??  0:03.89 /Library/Java/JavaVirtualMachines/jdk-14.0.2.jdk/Contents/Home/bin/java
-XX:TieredStopAtLevel=1 -Xms256m -Xmx512m -Dfile.encoding=UTF-8 -Duser.country=IN 
-Duser.language=en com.example.demo.DemoApplication

In the above bootRun task, we changed the max and min heap of the Spring Boot application using the jvmArgs option. This way, the JVM parameter will be attached to the Spring Boot application dynamically. Furthermore, we can also add customized properties to the bootRun using the -D option. To demonstrate, let’s take a look at the bootRun task:

bootRun {
    jvmArgs(['-Dbaeldung=test', '-Xmx512m'])
}

This way, we can pass both the JVM options and custom property attributes. To illustrate, let’s verify the custom value with the jvm arguments:

$ ps -ef | grep java | grep spring
502  8423  7254   0  8:16PM ??  0:00.62 /Library/Java/JavaVirtualMachines/jdk-14.0.2.jdk/Contents/Home/bin/java 
-XX:TieredStopAtLevel=1  -Dbaeldung=test -Xms256m -Xmx512m -Dfile.encoding=UTF-8 -Duser.country=IN 
-Duser.language=en com.example.demo.DemoApplication

In addition, we can also put these properties files into gradle.properties and then use them in the build.gradle:

baeldung=test
max.heap.size=512m

Now, we can use it in the bootRun command:

bootRun {
    jvmArgs([
        "-Dbaeldung=${project.findProperty('baeldung')}",
	"-Xmx${project.findProperty('max.heap.size')}"
    ])
}

Using the above way we can separate the configuration file from the main build.gradle file.

4. Using Command-Line Arguments

We can also provide JVM options directly to the ./gradlew bootRun command. In Gradle, system properties can be specified with the -D flag, and JVM options can be specified using -X:

$ ./gradlew bootRun --args='--spring-boot.run.jvmArguments="-Xmx512m" --baeldung=test'

We can use this command to supply JVM options dynamically at runtime without modifying the Gradle build file. To demonstrate, let’s verify the JVM arguments using the ps command:

$ ps -ef | grep java | grep spring 
 502 58504 90399   0  7:21AM ?? 0:02.95 /Library/Java/JavaVirtualMachines/jdk-14.0.2.jdk/Contents/Home/bin/java 
 -XX:TieredStopAtLevel=1 -Xms256m -Xmx512m -Dfile.encoding=UTF-8 -Duser.country=IN -Duser.language=en 
 com.example.demo.DemoApplication --spring-boot.run.jvmArguments=-Xmx512m --baeldung=test

The above command directly sets the jvm arguments using the ./gradlew bootRun command.

5. Conclusion

In this article, we learned different ways to pass the JVM options to the bootRun command.

First, we learned the importance and basic usage of bootRun. We then explored the use of command line arguments and the build.gradle file to supply the JVM options to bootRun.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!