Lesson 11. Building a Native Executable with Quarkus .
Here's the rewritten, detailed tutorial with preserved examples and rephrased descriptions split into four equal-length points per section.
Building a Native Executable with Quarkus
This guide details the process of creating a native executable using Quarkus. Learn how to compile your application, package it for containers, and debug the native executable efficiently.
Prerequisites
Tools and Setup
You’ll need JDK 17+, Apache Maven 3.9.9, and a working container runtime (Docker or Podman). These tools are essential for building and running native executables.Development Environment
Ensure an IDE is installed and a functional C development environment is configured. For Linux, install GCC, glibc, and zlib headers. On macOS, XCode is required.Mandrel or GraalVM
A GraalVM distribution is necessary for native image generation. Install either Mandrel or GraalVM CE/EE. Ensure JAVA_HOME and GRAALVM_HOME are properly set.Getting Started Code
The application created in the Getting Started guide is required as a base. Clone or download the repository to follow along with this guide.
Background
Purpose of Native Executables
Native executables bundle application code, libraries, Java APIs, and a minimal VM. This approach ensures faster startup and smaller disk footprints, ideal for cloud environments.Mandrel vs. GraalVM
Mandrel is a streamlined GraalVM CE distribution tailored for Quarkus. It removes unnecessary features, reducing size and improving compatibility with Linux containers.Advantages of Mandrel
Mandrel is built with upstream OpenJDK, ensuring adherence to open-source standards. Its exclusion of polyglot programming makes it optimized for Quarkus.Platform-Specific Recommendations
Mandrel works best for Linux containers. For macOS, use GraalVM CE/EE as Mandrel doesn’t support this platform. Check the Mandrel README for detailed installation steps.
Configuring GraalVM
Installation Options
Download GraalVM or Mandrel from official sources and unpack it. Alternatively, use tools like SDKMAN! or Homebrew for streamlined installation.Environment Variables
Set theGRAALVM_HOME
to the installation directory and optionally configureJAVA_HOME
. Add the GraalVMbin
directory to your system PATH.Platform-Specific Instructions
On macOS, point theGRAALVM_HOME
to the "Home" subdirectory. For Windows, set environment variables via the Control Panel or use Scoop.Addressing macOS Issues
GraalVM binaries aren’t notarized for macOS. Usexattr
to remove the quarantine attribute. Refer to the GraalVM documentation for more detailed workarounds.
Producing a Native Executable
Using Maven Profiles
Add a<profile>
for native builds inpom.xml
. This profile enablesquarkus.native.enabled
and ensures all required properties are set for the native image.Command for Compilation
Use the commandquarkus build --native
to generate the native executable. This will package the application for optimal startup performance.Customizing Build Options
Usequarkus.native.additional-build-args
to pass extra parameters to the build process. You can also include these inapplication.properties
.Output Files
The build produces a native executable in thetarget
directory. Run the file directly to verify the application:./target/getting-started-1.0.0-SNAPSHOT-runner
.
Examples
Sample Maven Profile
<profiles> <profile> <id>native</id> <activation> <property> <name>native</name> </property> </activation> <properties> <quarkus.native.enabled>true</quarkus.native.enabled> </properties> </profile> </profiles>
Native Build Command
quarkus build --native
Dockerized Build
quarkus build --native -Dquarkus.native.container-build=true
Running the Native Executable
./target/getting-started-1.0.0-SNAPSHOT-runner
Configuring Additional Arguments
quarkus.native.additional-build-args="--static,--libc=musl"
What’s Next
Testing
Run tests against native executables using./mvnw verify -Dnative
. Adjust test properties inapplication.properties
for further customization.Containerization
Package your native executable into a container using the provided Dockerfiles or container-image extensions.Performance Optimization
Experiment with options like UPX compression and fully static linking to further reduce executable size and startup times.Kubernetes Deployment
Deploy your native executables to Kubernetes or OpenShift for production-ready, scalable applications.
This detailed guide equips you with the knowledge to create, test, and deploy native executables using Quarkus. Refer to the Native Reference Guide for more advanced configurations and optimizations.