Build Ultra‑Fast Spring Native Apps with GraalVM and UPX
This guide walks you through installing GraalVM, configuring Spring Native with Maven, building a native executable, and compressing it with UPX, demonstrating how to create high‑performance Java microservices in just a few minutes.
Basic Environment
GraalVM is a high‑performance virtual machine that can significantly improve program performance and is well‑suited for microservices.
Download links (same as a regular JDK installation):
Linux (amd64) – link [1]
Linux (aarch64) – link [2]
MacOS (amd64) – link [3]
Windows (amd64) – link [4]
If you are using macOS Catalina or later, run the following command before installation to avoid a quarantine warning:
<code>sudo xattr -r -d com.apple.quarantine path/to/graalvm/folder/</code>Getting Started with Spring Native
Create a project from https://start.spring.io [5].
Add a Maven profile for native‑image builds:
<code><profiles>
<profile>
<id>native-image</id>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>native-image-maven-plugin</artifactId>
<version>21.0.0</version>
<configuration>
<!-- Specify the entry point of the Main class -->
<mainClass>com.example.demo.DemoApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>native-image</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles></code>Configure the existing
spring-boot-maven-pluginto produce an executable JAR:
<code><plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- Use classifier "exec" -->
<classifier>exec</classifier>
<image>
<builder>paketobuildpacks/builder:tiny</builder>
<env>
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
</env>
</image>
</configuration>
</plugin></code>Start Building
Run the Maven command:
<code>mvn -Pnative-image package</code>Build succeeds in about three minutes.
<code>[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:03 min
[INFO] Finished at: 2021-03-28T12:17:55+08:00
[INFO] Final Memory: 113M/310M
[INFO] ------------------------------------------------------------------------</code> <code>ls -lh target/com.example.demo.demoapplication
-rwxr-xr-x 1 user staff 57M Mar 28 12:17 target/com.example.demo.demoapplication</code>Run the native binary to test:
<code>target/com.example.demo.demoapplication</code>Compress Further with UPX
UPX (Ultimate Packer for Executables) is an open‑source tool that compresses executable files.
Install UPX and compress the native image:
<code>brew install upx
upx com.example.demo.demoapplication</code>The binary size is reduced to about 30% while remaining runnable.
<code>Ultimate Packer for eXecutables
Copyright (C) 1996 - 2020
UPX 3.96 Markus Oberhumer, Laszlo Molnar & John Reiser Jan 23rd 2020
File size Ratio Format Name
------------------- ------ -------- ------------------------------
60185112 -> 18501648 30.74% macho/amd64 com.example.demo.demoapplication
Packed 1 file.</code> <code>ls -lh target/com.example.demo.demoapplication
-rwxr-xr-x 1 user staff 18M Mar 28 12:17 target/com.example.demo.demoapplication</code>References
[1] https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-21.0.0.2/graalvm-ce-java11-linux-amd64-21.0.0.2.tar.gz
[2] https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-21.0.0.2/graalvm-ce-java11-linux-aarch64-21.0.0.2.tar.gz
[3] https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-21.0.0.2/graalvm-ce-java11-darwin-amd64-21.0.0.2.tar.gz
[4] https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-21.0.0.2/graalvm-ce-java11-windows-amd64-21.0.0.2.zip
[5] https://start.spring.io
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
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.