Building and Running a Spring Native Application with GraalVM and Docker
This article explains what Spring Native is, how to set up the required environment, the two main ways to build a native Spring Boot image using either Buildpacks or the GraalVM native-image Maven plugin, and demonstrates container deployment and performance comparison with traditional JVM execution.
What is Spring Native
Spring Native provides beta support for compiling Spring applications into native executables using the GraalVM native-image compiler, enabling lightweight container deployment and faster startup times compared to traditional JVM-based Spring Boot applications.
Environment Configuration
OS: Windows10 21H2
IDE: IntelliJ IDEA 2022.1.3
JDK: graalvm-ce-java11-22.2.0
Maven: 3.5.4
Docker Desktop for Windows: 4.12.0
Spring Boot: 2.7.4
Spring Native: 0.12.1Spring Native applications should be compiled with Java 11 or Java 17.
Two Main Build Methods
Use Spring Boot Buildpacks to generate a lightweight container that includes the native executable.
Use the GraalVM native-image Maven plugin to produce a native executable directly.
Method 1 relies on the spring-boot-maven-plugin with the command mvn spring-boot:build-image , while Method 2 uses mvn -Pnative package after installing the native-image tool.
Key Differences
Environment Dependencies
Method 1 requires Docker.
Method 2 requires Visual Studio with specific components (MSVC, Windows 10 SDK).
Maven Commands
Method 1: mvn spring-boot:build-image
Method 2: mvn -Pnative package
Installing GraalVM and Native‑Image
Download GraalVM from https://www.graalvm.org/downloads/ and install the native‑image component with gu install native-image .
Creating a Spring Boot Project
A minimal pom.xml is provided, including dependencies for spring-boot-starter-web and spring-native , and plugin configuration that enables native image building via Buildpacks.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
...
<properties>
<java.version>11</java.version>
<spring-native.version>0.12.1</spring-native.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-native</artifactId>
<version>${spring-native.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<builder>paketobuildpacks/builder:tiny</builder>
<env>
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
</env>
</image>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.experimental</groupId>
<artifactId>spring-aot-maven-plugin</artifactId>
<version>${spring-native.version}</version>
</plugin>
</plugins>
</build>
</project>Building the Native Image
Run the Maven commands:
mvn clean
mvn '-Dmaven.test.skip=true' spring-boot:build-imageThe build process consumes significant CPU and memory, and the resulting Docker image can be inspected with docker images .
Running the Container
docker run -id -p 8888:8888 --name=native-app spring-native:0.0.1-SNAPSHOT 59178df9f49bWhen started via Docker Desktop, the native Spring Boot application launches in about 31 ms and uses roughly 46 MB of memory, while the same application started with java -jar takes ~1.5 seconds and 238 MB.
References
Official Spring Native documentation: https://docs.spring.io/spring-native/docs/current/reference/htmlsingle/index.html
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.