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

  1. 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.

  2. 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.

  3. 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.

  4. 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

  1. 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.

  2. 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.

  3. 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.

  4. 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

  1. Installation Options
    Download GraalVM or Mandrel from official sources and unpack it. Alternatively, use tools like SDKMAN! or Homebrew for streamlined installation.

  2. Environment Variables
    Set the GRAALVM_HOME to the installation directory and optionally configure JAVA_HOME. Add the GraalVM bin directory to your system PATH.

  3. Platform-Specific Instructions
    On macOS, point the GRAALVM_HOME to the "Home" subdirectory. For Windows, set environment variables via the Control Panel or use Scoop.

  4. Addressing macOS Issues
    GraalVM binaries aren’t notarized for macOS. Use xattr to remove the quarantine attribute. Refer to the GraalVM documentation for more detailed workarounds.


Producing a Native Executable

  1. Using Maven Profiles
    Add a <profile> for native builds in pom.xml. This profile enables quarkus.native.enabled and ensures all required properties are set for the native image.

  2. Command for Compilation
    Use the command quarkus build --native to generate the native executable. This will package the application for optimal startup performance.

  3. Customizing Build Options
    Use quarkus.native.additional-build-args to pass extra parameters to the build process. You can also include these in application.properties.

  4. Output Files
    The build produces a native executable in the target directory. Run the file directly to verify the application: ./target/getting-started-1.0.0-SNAPSHOT-runner.


Examples

  1. 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>
    
  2. Native Build Command

     quarkus build --native
    
  3. Dockerized Build

     quarkus build --native -Dquarkus.native.container-build=true
    
  4. Running the Native Executable

     ./target/getting-started-1.0.0-SNAPSHOT-runner
    
  5. Configuring Additional Arguments

     quarkus.native.additional-build-args="--static,--libc=musl"
    

What’s Next

  1. Testing
    Run tests against native executables using ./mvnw verify -Dnative. Adjust test properties in application.properties for further customization.

  2. Containerization
    Package your native executable into a container using the provided Dockerfiles or container-image extensions.

  3. Performance Optimization
    Experiment with options like UPX compression and fully static linking to further reduce executable size and startup times.

  4. 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.