Spring-Native demo with Spring Boot 3
Setting up a spring-native application in spring-boot-3
Some time ago, Spring Boot 3 was released. Spring Boot 3 introduced the official support for Spring-Native, which provides support for compiling Spring applications to native executables using the GraalVM native-image compiler. Compared to the Java Virtual Machine, native images can enable cheaper and more sustainable hosting for many types of workloads. These include microservices, function workloads, well suited to containers and Kubernetes.
Now it is time to see what we can do with it. Let’s try to find out if that really works and how time-consuming the build is. Starting by reading the Spring-Native Docs and setting up an example hello-world application.
Setting up GraalVM
Set up the GraalVM with the help of sdkman.
sdk install java 22.3.r17-grl
Downloading demo project from Spring Initializr
The next step is to get the basic spring-application. On Spring Initializr I select:
- Project: Gradle - Groovy
- Language: Java
- Spring Boot: 3.0.1
- Packaging: Jar
- Java: 17
- Dependencies: GraalVM Native Support
Make a change to the output
To see if it really works I go to the DemoApplication and change the following line to:
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println("Hurra this is a short test.");
}
Pre-Build
Before building check that JAVA_HOME is set, else you’ll get: "Cannot query the value of property 'javaLauncher' because it has no value available."
You need gcc installed (sudo apt install gcc), else you’ll get "Error: Default native-compiler executable 'gcc' not found via environment variable PATH"
Build
To build the application, I compile it with:
./gradlew nativeCompile
Run the application
Run it locally with:
./build/native/nativeCompile/demo
or
./gradlew nativeRun
Result
Summarized Task List for the demo application
- checking out an example Application from https://start.spring.io/.
- install GraalVM I use sdkman https://sdkman.io/
- Setting GraalVM in path
.zshrcadd (or the config file of whatever shell you use)export GRAALVM_HOME=~/.sdkman/candidates/java/22.3.r17-grl export PATH="$GRAALVM_HOME:$PATH" - Compile with:
./gradlew nativeCompile - Run with:
./gradlew nativeRun - try to run it locally:
./build/native/nativeCompile/demo
Conclusion
It was possible to set up a Spring Boot 3 project with the help of the Spring Initializr. Compiling worked for the first project on a Mac and the demo-program started out of the box. The build is quite time-consuming but not as long as I’ve heard from other people. It took less than a minute for the demo application.
Sourcecode on github
The source-code can be found on github: https://github.com/mhoffmann-attempto/spring-boot-native-demo
Troubleshooting for WSL2 (Windows Subsystem for Linux 2)
For WSL2 users: if you get an error like
It appears as though libz:.a is missing. Please install it.
...
/usr/bin/ld: cannot find -lz: No such file or directory
Please install it using: sudo apt install zlib1g-dev