Quick revision
The short answer
We need a GraalVM distribution that includes native-image and a C toolchain, or we let Cloud Native Buildpacks build inside a container so no local GraalVM is needed. With Maven and spring-boot-starter-parent we run mvn -Pnative native:compile for an executable or mvn -Pnative spring-boot:build-image for a native container image; with Gradle we apply the org.graalvm.buildtools.native plugin and run nativeCompile or bootBuildImage. We run the tests as native too, with mvn -PnativeTest test or gradle nativeTest, to catch missing hints, and we plan for builds of several minutes using several GB of memory.
What to remember
In this answer, we saw what a native build of a Spring Boot 3 application needs: a GraalVM with native-image and a C toolchain for local builds, or only Docker for Buildpacks. With Maven we use the native profile and native:compile or spring-boot:build-image; with Gradle the native build tools plugin and nativeCompile or bootBuildImage. We run the tests in native mode with -PnativeTest or nativeTest to catch missing hints, and plan for builds that take minutes and need several GB of memory.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We need a GraalVM distribution that includes native-image and a C toolchain, or we let Cloud Native Buildpacks build inside a container so no local GraalVM is needed. With Maven and spring-boot-starter-parent we run mvn -Pnative native:compile for an executable or mvn -Pnative spring-boot:build-image for a native container image; with Gradle we apply the org.graalvm.buildtools.native plugin and run nativeCompile or bootBuildImage. We run the tests as native too, with mvn -PnativeTest test or gradle nativeTest, to catch missing hints, and we plan for builds of several minutes using several GB of memory.
Why this question is asked
Spring Boot 3 made native images a supported option rather than an experiment, but the build is different enough from a normal jar build that teams often stumble on the first try: wrong JDK, missing compiler toolchain, a build that eats all the memory on the CI runner, or a binary that builds fine and fails at run time. Interviewers ask this to see whether we have done it, not just read about it. In this answer, we will cover what to install, the Maven and Gradle commands and what each produces, the Buildpacks route that needs no local GraalVM, how to run the tests in native mode, and what to expect from build time and memory.
1What We Need Installed
There are two ways to build, and they need different things. For a local native executable, we need a GraalVM distribution, such as GraalVM for JDK 21 or 25 from Oracle or the community Liberica NIK, whose bin directory contains the native-image tool; current GraalVM releases ship native-image by default. native-image also needs the platform's C toolchain to link the executable: gcc, glibc development headers and zlib on Linux, the Xcode command line tools on macOS, and the Visual Studio build tools on Windows.
For a native container image, we only need Docker or a compatible runtime. Paketo Buildpacks download a GraalVM-based builder image and do the whole native compilation inside a container. That route also produces a Linux executable no matter what our laptop runs, which is what we deploy anyway.
2Building With Maven
A project whose parent is spring-boot-starter-parent inherits a profile called native. It configures Spring AOT processing and the GraalVM native-maven-plugin, which we declare in the build plugins without a version. Activating the profile with -Pnative and calling the native:compile goal runs the normal build, Spring's process-aot step and then native-image.
Notice that the executable lands in target with the artifact's name. The same profile also lets us build a native container with Buildpacks: mvn -Pnative spring-boot:build-image.
3Building With Gradle
With Gradle, applying the org.graalvm.buildtools.native plugin next to the Spring Boot plugin is enough. Spring Boot's plugin reacts to it by adding the AOT tasks, processAot and processTestAot, and configuring the native build. nativeCompile builds the executable under build/native/nativeCompile, and bootBuildImage builds a native container image with Buildpacks.
| Goal | Maven | Gradle |
|---|---|---|
| Native executable | mvn -Pnative native:compile | gradle nativeCompile |
| Native container image | mvn -Pnative spring-boot:build-image | gradle bootBuildImage |
| Run tests as native | mvn -PnativeTest test | gradle nativeTest |
| Needs local GraalVM | yes for native:compile | yes for nativeCompile |
4Running the Tests as a Native Image
A native build can succeed and the program still fail at run time, because something used reflection, a proxy or a resource that was not registered. Ordinary JVM tests do not catch this, since on the JVM everything is available. Spring Boot and the GraalVM build tools can compile the JUnit tests themselves into a native test executable and run them. With Maven the parent provides a nativeTest profile, run as mvn -PnativeTest test; with Gradle the task is nativeTest. They are slow, so many teams run them on the main branch or nightly rather than on every commit, but they are the cheapest place to find a missing hint.
Tests that rely on mocking frameworks which generate classes at run time may not work in native mode; such tests can be excluded from the native run with @DisabledInNativeImage.
5What to Expect From Build Time and Memory
A native build of a typical Spring Boot service takes two to ten minutes, compared with seconds for a jar, and native-image uses a lot of memory: plan for 8 GB or more for a medium service, and expect it to fail or swap badly on a 4 GB CI runner. The build is also CPU-heavy and benefits from several cores. In practice, teams keep the fast JVM build for every pull request and run the native build and native tests in a separate pipeline stage, with a larger runner and caching for the Maven or Gradle dependencies and the Buildpacks layers.
6Follow-up questions
Do I need GraalVM to build a native container image?
No. spring-boot:build-image and bootBuildImage use Paketo Buildpacks, which pull a builder image that contains the native toolchain and build inside a container. We only need Docker. A local GraalVM is needed for native:compile and nativeCompile.
Why does the native build need the Spring AOT step?
Spring normally decides which beans to create at startup, using reflection over annotations. For a native image, Spring's AOT processing runs that decision at build time, generates Java code for the bean definitions and writes the reflection hints native-image needs. The native profile and the Gradle plugin run it for us.
Can I build a Linux executable on a Mac?
Not with native:compile, which builds for the machine it runs on. A Buildpacks build produces a Linux image because it compiles inside a Linux container, which is the usual way to get a deployable binary from a laptop.
7Conclusion
In this answer, we saw what a native build of a Spring Boot 3 application needs: a GraalVM with native-image and a C toolchain for local builds, or only Docker for Buildpacks. With Maven we use the native profile and native:compile or spring-boot:build-image; with Gradle the native build tools plugin and nativeCompile or bootBuildImage. We run the tests in native mode with -PnativeTest or nativeTest to catch missing hints, and plan for builds that take minutes and need several GB of memory.
Open with
I need either a GraalVM distribution that includes native-image plus the C toolchain for my platform, like gcc and the glibc and zlib headers on Linux, or just Docker if I build with Buildpacks, which compiles inside a container.
Then, if they want more
With Maven and spring-boot-starter-parent, I add the native-maven-plugin to the build and run mvn -Pnative native:compile. The native profile runs Spring's AOT processing and then native-image, and I get an executable in target. With Gradle I apply the org.graalvm.buildtools.native plugin, Spring Boot adds processAot, and gradle nativeCompile puts the executable in build/native/nativeCompile.
For a container image I use mvn -Pnative spring-boot:build-image or gradle bootBuildImage, which does not need GraalVM locally and gives me a Linux image. Because a native build can succeed and still fail at runtime on missing reflection or resource hints, I also run the tests in native mode, with mvn -PnativeTest test or gradle nativeTest.
Those are slow, so I usually run them in a separate CI stage, not on every commit. And I expect a native build to take a few minutes and need 8 GB or more of memory, so CI runners have to be sized for it.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3, Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Kubernetes documentation, Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Describe how you build and test a Spring Boot 3 application as a native image with Maven...": https://scrivio-interview-library.netlify.app/spring-boot/native-and-aot/1f001895d0/