Developers Notes
  • Welcome
  • Developer
    • Java
      • JUnit
        • Parameterized Test
        • Introduction to WireMock
      • Maven
        • Resource Reader and Writer
        • JUnit with Maven
        • Maven Run
        • A Quick Guide to Maven Wrapper
      • Spring
        • Autowired vs Resource
        • Spring OpenFeign 사용시 https 신뢰하는 방법
        • Aspect with Annotation
        • Spring JPA에서 Tibero를 사용하기 위한 설정
        • Spring module dependency
        • Mockito
          • Autowired @Value field in Spring with Mockito
        • SpringBoot Hybernate application.yml
        • ReflectionTestUtils
        • Spring Properties File Outside jar
        • Spring @RequestParam Annotation
        • Properties with Spring and Spring Boot
        • Passing JVM Options from Gradle bootRun
        • Securing Spring Boot API With API Key and Secret
        • Why Is Field Injection Not Recommended?
        • An Overview of Identifiers in Hibernate/JPA
      • Etcs
        • BigDecimal 사용시 주의 사항
        • static factory methods common naming conventions
        • List of Lists into a List (Stream)
        • Return null in stream
        • Logging with Lombok
        • JPA
        • Big-O Java Collections
    • MySQL
      • Active Connections on MySQL
      • MariaDB-Galera
      • FOUND_ROWS
      • MySQL Group Replication Requirements
      • Data Types Explicit Default Handling
    • C/C++
      • Autotool 사용법
      • Intruduction to GNU Autotools
      • mysql
        • C Api Flow
        • MySQL Connector/C++ 8.3 Developer Guide
      • Common vulnerabilities guide for C programmers
      • HTTP in C++
      • JSON in C++
      • How to get memory usage at runtime using C++?
      • How to get time in milliseconds using C++ on Linux?
      • Sleep Functions in C++
      • Calculate Cpu Usage on Linux as Top
    • CryptoGraphy
      • 인증 기관(CA;Certificate Authority) 구성하고 인증서 발급하기
      • KeyTool Import PrivateKey, Certificate
      • Java Keytool 사용법
      • PKCS, Public Key Cryptography Standard
      • CER/DER/CRT/CSR 형식 파일이란?
      • FIPS 140-2
      • SSL 인증서 발급
      • 사용법, tip 정리
      • OpenSSL
        • OpenSSL guide
        • Openssl RSA Private Key Encrypt
      • How to Read PEM File to Get Public and Private Keys
    • PKCS#11 API
      • PKCS#11 API-강좌1
      • PKCS#11 API-강좌2
      • PKCS#11 API-강좌3
      • PKCS#11 API-강좌4
      • PKCS#11 API-강좌5(C 언어로 된 Sample Code)
      • PKCS#11 API-강좌6(EC Key 생성 및 Signing)
    • Warehouse of PKI
    • GoLang
      • go-cshared-examples
      • Fun building shared libraries in Go
      • Golang time
      • Encoding Json
  • OpenSSL
    • OpenSSL Document
      • openssl-req
      • x509v3_config
      • Openssl Example
    • Creating a Self-Signed Certificate With OpenSSL
    • Openssl 3.x Provider
      • Writing OpenSSL Provider Skeleton
    • OpenSSL Certificate Command
  • DevOps
    • Docker
      • Environment Variables for MariaDB or MySQL Docker
      • Container Technology, Docker
      • Docker Trouble Shooting
      • Docker BuildKit
      • How to clear Docker cache and free up space on your system
    • Cloud
      • Serverless Architecture
      • AWS
        • AWS 주요 자습서 Link
        • Diagram-as-code for AWS architecture.
        • AWS Architecture icon
      • Install MariaDB Galera by Helm
      • Jenkinsfile VIM syntax highlighting
      • Cloud Development Kit for Kubernetes
    • VM
      • vagrant를 사용한 vm 설치 방법
    • Etcs
      • Logstash
        • Installing Logstash
        • Configuration Logstash Output
      • Rancher Install
      • Install ELK
      • Simpler Tool for Deploying Rancher
    • Ubuntu
      • Install SFTP Client
  • Etcs
    • Etcs
      • Useful Tools
      • Links
      • Entertainment
Powered by GitBook
On this page
  • 1. Overview
  • 2. Understanding bootRun
  • 3. Using jvmArgs in build.gradle File
  • 4. Using Command-Line Arguments
  • 5. Conclusion
Edit on GitHub
  1. Developer
  2. Java
  3. Spring

Passing JVM Options from Gradle bootRun

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.

PreviousProperties with Spring and Spring BootNextSecuring Spring Boot API With API Key and Secret

Last updated 1 year ago

Passing JVM Options from Gradle bootRun | BaeldungBaeldung
Logo