Compiling Spring Boot applications to native executables¶
Spring Boot 3+ provides official support for compiling a Java application to a native executable. The documentation is found at GraalVM Native Images in the Spring Boot references.
To support native compilation with a Maven project, the native-maven-plugin declaration needs to be added to the
pom.xml
file:<plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> </plugin>
For a Gradle project, add the following plugin declaration to the plugins block in the
build.gradle
file:id 'org.graalvm.buildtools.native' version '0.10.6'
Install the GraalVM Community Edition snap from the snap store:
sudo snap install graalvm-jdk --channel=v21
Point the
JAVA_HOME
environment variable to the GraalVM CE installation:export JAVA_HOME=/snap/graalvm-jdk/current/graalvm-ce/
Finally, use this command to do a native compilation for your Maven project:
./mvnw -Pnative native:compile
To build a Gradle project, use this command:
./gradlew nativeCompile
The last step builds the application using the typical Maven/Gradle workflow, subsequently invoking GraalVM native-image to compile it into a native executable. For a Maven project, the native executable is created under the target directory in the project. The application may be launched by executing this file on the command line.
This screencast captures the workflow mentioned above for the Spring Boot Pet Clinic sample application.
Further reading¶
Refer to GraalVM native compilation for an introduction to GraalVM and native compilation of Java.