When running a Spring Boot application using the bootRun task in Gradle, you may need to configure JVM options to customize the runtime environment, such as setting memory sizes or other system properties.
To pass JVM options to the bootRun task, configure the bootRun task within the build.gradle file. Here is an example configuration demonstrating how to set the maximum and minimum heap memory for a Spring Boot application:
groovybootRun { // Configure JVM options jvmArgs = [ '-Xmx1024m', // Set maximum heap memory to 1024MB '-Xms512m', // Set minimum heap memory to 512MB '-Dsome.property=value' // Set system property ] }
In this example, jvmArgs is an array containing all parameters intended for the JVM. Each parameter is a string formatted identically to command-line usage. Here, we configure the maximum heap memory (Xmx) to 1024MB, the minimum heap memory (Xms) to 512MB, and define the system property some.property.
You can add additional JVM options to this list as needed. This approach enables effortless adjustment of JVM settings in development and testing environments without modifying application code or using command-line arguments.
When executing the ./gradlew bootRun command, Gradle launches the application with these configurations. This setup ensures clear and centralized environment management, enhancing project maintainability and team consistency.