Sub topic 5
Setting up a Spring Boot project for native builds
This chapter assumes you know Java 17 or later, can build Spring Boot projects with Maven or Gradle, and understand at a conceptual level that GraalVM compiles your application ahead of time. You will prepare a project so that it can produce a native executable. Compiling and running it is covered in the next chapter.
What the build needs
Two tools take part in a native build:
- Spring's AOT processing runs first. It inspects your application and generates code and hints.
- The GraalVM
native-imagetool runs second. It compiles your classes and the generated output into one executable.
Your build tool must run both steps in order. Spring Boot's build plugins do this for you once native support is enabled.
Installing a GraalVM JDK
To compile locally, you need a GraalVM distribution that includes native-image. Install one for Java 17 or later, make it the active JDK, and check it:
java -version
native-image --versionIf native-image is not found, your active JDK is not a GraalVM distribution, or JAVA_HOME points somewhere else. If you only plan to build container images with buildpacks, you can skip this step. Buildpacks bring their own toolchain, as the container chapter shows.
Maven setup
Projects generated with Spring Initializr and the GraalVM Native Support dependency already have this configuration. If your project inherits from spring-boot-starter-parent, it also gets a native profile for free. That profile applies the GraalVM Native Build Tools plugin and configures Spring AOT.
A minimal pom.xml looks like this:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.5</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
</plugins>
</build>The versions of both plugins are managed by the parent, so you do not set them. The native plugin only takes effect when you activate the native profile with -Pnative.
If you cannot use the starter parent, for example because your company parent POM is mandatory, import the Spring Boot BOM instead. Then declare the native profile yourself and bind the process-aot goal of spring-boot-maven-plugin to it.
Gradle setup
On Gradle, apply the org.graalvm.buildtools.native plugin next to the Spring Boot plugin:
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.5'
id 'io.spring.dependency-management' version '1.1.6'
id 'org.graalvm.buildtools.native' version '0.10.3'
}When the native plugin sees the Spring Boot plugin, it reacts automatically. It registers the processAot task and wires it into nativeCompile. The plugin does not need a profile, because the tasks are always available.
Checking your setup
Compare what each tool gives you:
| Item | Maven | Gradle |
|---|---|---|
| Native plugin | `native-maven-plugin` | `org.graalvm.buildtools.native` |
| Activation | `-Pnative` profile | Plugin applied, tasks always present |
| AOT task | `spring-boot:process-aot` | `processAot` |
| Native build | `native:compile` | `nativeCompile` |
You can test the AOT step alone, without installing GraalVM:
./mvnw -Pnative spring-boot:process-aotOn Gradle, run ./gradlew processAot. Look in target/spring-aot (Maven) or build/generated/aotSources (Gradle) for generated bean definition classes. If they appear, Spring AOT is wired up.
Exercise
Your project inherits from spring-boot-starter-parent. Which Maven flag activates native support, and which command runs only the Spring AOT step without needing a GraalVM installation?
Show answer
Activate it with the -Pnative profile. Run ./mvnw -Pnative spring-boot:process-aot to execute only the AOT step. Generated sources appear under target/spring-aot.
Exercise
You run native-image --version and get "command not found", even though java -version works. What is the likely cause?
Show answer
The active JDK is not a GraalVM distribution that includes native-image, or JAVA_HOME and PATH point to a different JDK. Install GraalVM and make it the active JDK.