Passing JVM Options from Gradle bootRun – 从 Gradle bootRun 传递 JVM 选项

最后修改: 2023年 11月 30日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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.

Gradle是一款多用途自动化构建工具,用于开发、编译和测试软件包。它支持多种语言,但我们主要将其用于基于 Java 的语言,如KotlinGroovyScala

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.

在使用 Java 时,我们可能需要自定义 Java 应用程序中的 JVM 参数。在使用 Gradle 构建 Java 应用程序时,我们也可以通过调整 Gradle 配置来自定义应用程序的 JVM 参数。

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

在本教程中,我们将学习从 Gradle bootRunSpring Boot Java 应用程序传递 JVM 参数。

2. Understanding bootRun

2 了解 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.

GradlebootRun是一个gradle指定的任务,它与默认的Spring Boot Gradle Plugin一起提供。执行bootRun命令可在开发环境中启动应用程序,这对测试和开发非常有用。它主要用于迭代开发,因为它不需要任何单独的构建或部署目的。

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

简而言之,它提供了一种简化的方法,可在开发环境中构建应用程序并执行与 spring boot 开发相关的任务。

3. Using jvmArgs in build.gradle File

3.在 build.gradle 文件中使用 jvmArgs

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:

Gradle 提供了一种直接的方法,使用 build.gradle 文件为 bootRun 命令添加 JVM 参数。为了说明这一点,让我们来看看使用 bootRun 命令为 Spring boot 应用程序添加 JVM 参数的命令:

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:

我们可以看到,使用 jvmArgs 选项修改了 Springboot 应用程序的 max/min 堆。现在,让我们使用ps命令来验证对 springboot 应用程序的 JVM 更改:

$ 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 选项更改了 Spring Boot 应用程序的 maxmin 堆。这样,JVM 参数将动态附加到 Spring Boot 应用程序。此外,我们还可以使用 -D 选项向 bootRun 添加自定义属性。为了演示,让我们来看看 bootRun 任务:

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:

这样,我们就可以同时传递 JVM 选项和自定义属性属性。为了说明这一点,让我们用 jvm 参数来验证自定义值:

$ 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:

此外,我们还可以将这些属性文件放入 gradle.properties 中,然后在 build.gradle 中使用它们:

baeldung=test
max.heap.size=512m

Now, we can use it in the bootRun command:

现在,我们可以在 bootRun 命令中使用它:

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.

通过上述方法,我们可以将配置文件从主 build.gradle 文件中分离出来。

4. Using Command-Line Arguments

4.使用命令行参数

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 命令提供 JVM 选项。在 Gradle 中,可以使用 -D 标志指定系统属性,使用 -X 指定 JVM 选项:

$ ./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:

我们可以使用该命令在运行时动态提供 JVM 选项,而无需修改 Gradle 构建文件。为了演示,让我们使用 ps 命令来验证 JVM 参数:

$ 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.

上述命令使用 ./gradlew bootRun 命令直接设置 jvm 参数。

5. Conclusion

5.结论

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

在本文中,我们学习了向 bootRun 命令传递 JVM 选项的不同方法。

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.

首先,我们了解了 bootRun 的重要性和基本用法。然后,我们探索了如何使用命令行参数和 build.gradle 文件为 bootRun 提供 JVM 选项。