Sub topic 5
Setting up a Spring Boot project for native builds
This chapter covers the project configuration needed to build a Spring Boot 3 application as a GraalVM native executable. It assumes you can build Spring Boot projects with Maven or Gradle, know Java 17 or later, and understand what auto-configuration is. The build itself is covered in the next chapter, so here the goal is a correctly prepared project and a working toolchain.
What the build needs
Two separate pieces of tooling take part in a native build, and a setup problem usually comes from one of them being missing or mismatched.
- Spring's AOT (ahead-of-time) processing runs first. It inspects your application context at build time and generates source code, bytecode, and GraalVM configuration files.
- The GraalVM
native-imagetool runs second. It reads the compiled classes plus the generated metadata, analyzes everything reachable from the entry point, and produces the executable.
The build plugins connect the two. In Maven, the Spring Boot plugin runs AOT processing and the GraalVM Native Build Tools plugin invokes native-image. In Gradle, the Spring Boot plugin and the org.graalvm.buildtools.native plugin split the work the same way.
Installing a GraalVM distribution
To build on your own machine, you need a JDK distribution that includes native-image. Use a GraalVM release that matches the Java version your project targets. Spring Boot 3 requires Java 17 or later, and recent Spring Boot 3 releases work best with a current GraalVM JDK.
You can install it with SDKMAN or by downloading it from the GraalVM site. After installing, check the toolchain:
java -version
native-image --versionBoth commands should report a GraalVM build. If native-image is not found, either the installation does not include it or JAVA_HOME points at a different JDK. Set it explicitly:
export JAVA_HOME="$HOME/.sdkman/candidates/java/current"
export PATH="$JAVA_HOME/bin:$PATH"On Linux, native-image also needs a C toolchain (gcc, zlib headers, and glibc headers). On macOS, install the Xcode command line tools. On Windows, install the Visual Studio Build Tools with the C++ workload and run the build from the "x64 Native Tools" command prompt.
Maven setup
If your project inherits from spring-boot-starter-parent, the native setup is already done for you. The parent defines a profile named native that applies the native-maven-plugin and configures the Spring Boot plugin to run AOT processing when that profile is active.
A minimal pom.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.5</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>There is nothing native-specific in this file. The native profile comes from the parent, and you activate it on the command line with -Pnative. This keeps the normal JVM build fast: the slow AOT and native steps run only when you ask for them.
The final artifact is named after artifactId, so this project produces target/demo.
If you do not use the starter parent
Some organizations import the Spring Boot BOM (bill of materials) instead of inheriting from the parent. In that case the native profile does not exist. You have to declare it yourself, adding the org.graalvm.buildtools:native-maven-plugin and the AOT process-aot execution of the Spring Boot plugin. Version management is the main pitfall here. Take the plugin version from the Spring Boot dependency management or the Native Build Tools documentation for your Boot version, and do not mix arbitrary versions.
Gradle setup
On Gradle you apply the GraalVM plugin yourself. When it detects the Spring Boot plugin, it registers the AOT tasks and wires them in.
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'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}Two points are worth knowing:
- The Gradle plugin version is separate from the Spring Boot version. Pick a Native Build Tools version that your Spring Boot release supports. The Spring Boot reference documentation lists the version it was tested with.
- The
java.toolchainblock makes Gradle pick a matching JDK. For native builds, the toolchain must resolve to a GraalVM JDK, ornativeCompilefails because it cannot findnative-image. You can instead setJAVA_HOMEto a GraalVM installation.
The key tasks the plugin adds are:
| Task | Purpose |
|---|---|
| `processAot` | Runs Spring AOT processing for the main code |
| `nativeCompile` | Builds the native executable in `build/native/nativeCompile` |
| `bootBuildImage` | Builds a container image (native when the plugin is applied) |
| `nativeTest` | Runs the test suite as a native image |
Choosing an application style
A few project choices make native builds easier or harder.
- Prefer constructor injection and
@Configurationclasses over code that builds bean definitions dynamically at runtime. Spring AOT can only process what the context declares at build time. - Prefer Boot starters whose libraries already ship native metadata. Most popular libraries do, either inside the jar or in the shared GraalVM reachability metadata repository, which the Native Build Tools plugin downloads automatically.
- Prefer
@ConfigurationPropertiesbinding to records or simple classes. Spring registers hints for those automatically. - Avoid libraries that generate classes at runtime, such as those that rely on bytecode generation after startup. They usually need extra work.
Check each new dependency for native support before adopting it. Doing this when you add the dependency is cheaper than discovering an incompatibility after a ten-minute native build.
Verifying the setup before a full build
A full native compile takes minutes and uses several gigabytes of memory, so it helps to validate the cheaper steps first.
First, run the AOT step alone. This tells you whether Spring can process your context, without involving native-image:
./mvnw -Pnative process-aotOn Gradle:
./gradlew processAotThe generated sources appear under target/spring-aot/main/sources for Maven and build/generated/aotSources for Gradle. Open them and you will find classes such as DemoApplication__BeanDefinitions. They show exactly what Spring decided at build time. The generated hint files are in resources/META-INF/native-image.
Second, make sure the JVM build and tests pass normally, because a native build only adds failure modes to an already working application.
./mvnw clean verifyMemory and resources
native-image is memory hungry. As a rough guide, small applications need around 4 GB of free RAM, and larger ones need more. If the build is killed with no clear error, especially in a CI container or Docker Desktop with a low memory limit, raise the available memory first. You can also cap the builder heap through build arguments, for example -J-Xmx6g, set in the plugin configuration's buildArgs.
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<configuration>
<buildArgs>
<buildArg>-J-Xmx6g</buildArg>
</buildArgs>
</configuration>
</plugin>Common setup errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `native-image` not found | Standard JDK in use or `JAVA_HOME` wrong | Install GraalVM and fix `JAVA_HOME` and `PATH` |
| Profile `native` not found | Project does not inherit from the starter parent | Declare the plugins and profile manually |
| Build killed without a message | Out of memory | Increase RAM or container memory limit |
| Linker errors about missing `gcc` or `zlib` | C toolchain missing | Install the platform build tools |
| Unsupported class file major version | GraalVM JDK older than the project's Java target | Use a GraalVM release matching the Java version |
Exercise
A teammate runs ./mvnw -Pnative process-aot and it succeeds, but ./mvnw -Pnative native:compile fails immediately with "native-image not found". Which part of the toolchain is the problem, and why did the first command still succeed?
Show answer
The GraalVM native-image tool is missing from the active JDK, probably because a regular JDK is on the PATH or JAVA_HOME points to it. The process-aot goal only runs Spring's AOT processing, which is ordinary Java code and does not need native-image. Only the compile step invokes native-image, so only that step fails. The fix is to install a GraalVM distribution and set JAVA_HOME and PATH to it.
Exercise
Your project imports the Spring Boot BOM in dependencyManagement instead of inheriting from spring-boot-starter-parent. You run ./mvnw -Pnative native:compile and Maven reports that the profile does not exist. What do you need to add?
Show answer
The native profile is defined by the starter parent, so without it you must configure the build yourself. Add the org.graalvm.buildtools:native-maven-plugin and make sure the Spring Boot Maven plugin runs its process-aot goal, typically inside a profile you define. Use plugin versions that match your Spring Boot version.