<a id="use-graalvm"></a>

# Compiling Spring Boot applications to native executables

This tutorial shows how to compile a Spring Boot application to a native executable using GraalVM.
Spring Boot 3+ provides official support for compiling a Java application to a native executable. The documentation is found at [GraalVM Native Images](https://docs.spring.io/spring-boot/reference/packaging/native-image/index.html) in the Spring Boot references.

1. To support native compilation with a Maven project, the [native-maven-plugin](https://graalvm.github.io/native-build-tools/latest/maven-plugin.html) declaration needs to be added to the `pom.xml` file:
   ```xml
   <plugin>
     <groupId>org.graalvm.buildtools</groupId>
     <artifactId>native-maven-plugin</artifactId>
   </plugin>
   ```

   For a Gradle project, add the following [plugin](https://graalvm.github.io/native-build-tools/latest/gradle-plugin.html) declaration to the plugins block in the `build.gradle` file:
   ```groovy
   id 'org.graalvm.buildtools.native' version '0.10.6'
   ```
2. Install the GraalVM Community Edition snap from the snap store:
   `dev@ubuntu:~$ ``sudo snap install graalvm-jdk --channel=v21`
3. Point the `JAVA_HOME` environment variable to the GraalVM CE installation:
   `dev@ubuntu:~$ ``export JAVA_HOME=/snap/graalvm-jdk/current/graalvm-ce/`
4. Finally, use this command to do a native compilation for your Maven project:
   `dev@ubuntu:~$ ``./mvnw -Pnative native:compile`

To build a Gradle project, use this command:

`dev@ubuntu:~$ ``./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](https://drive.google.com/file/d/1ZqSMvyhjia4T5MuJa1IcbWNDDkC5xJbD/view?usp=sharing) captures the workflow mentioned above for the [Spring Boot Pet Clinic](https://github.com/spring-projects/spring-petclinic) sample application.

## Further reading

Refer to [GraalVM native compilation](https://documentation.ubuntu.com/ubuntu-for-developers/explanation/graalvm-introduction.md#graalvm-introduction) for an introduction to GraalVM and native compilation of Java.
