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:
sudo xattr -r -d com.apple.quarantine path/to/graalvm/folder/Getting Started with Spring Native
Create a project from https://start.spring.io [5].
Add a Maven profile for native‑image builds:
<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>Configure the existing spring-boot-maven-plugin to produce an executable JAR:
<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>Start Building
Run the Maven command: mvn -Pnative-image package Build succeeds in about three minutes.
[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] ------------------------------------------------------------------------ ls -lh target/com.example.demo.demoapplication
-rwxr-xr-x 1 user staff 57M Mar 28 12:17 target/com.example.demo.demoapplicationRun the native binary to test:
target/com.example.demo.demoapplicationCompress Further with UPX
UPX (Ultimate Packer for Executables) is an open‑source tool that compresses executable files.
Install UPX and compress the native image:
brew install upx
upx com.example.demo.demoapplicationThe binary size is reduced to about 30% while remaining runnable.
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. ls -lh target/com.example.demo.demoapplication
-rwxr-xr-x 1 user staff 18M Mar 28 12:17 target/com.example.demo.demoapplicationReferences
[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
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
