wissel.net

Usability - Productivity - Business - The web - Singapore & Twins

Quarkus and GraalVM starter


When Java is one of the languanges in your portfolio, you might have heard of Quarkus, an alternative to Spring Boot build on top of vert.x and GraalVM, a polyglot runtime for Java and other languages.

This article describes the getting started that worked for me.

Moving parts

The fist hurdle to overcome is to install all the bits and pieces. There are plenty of versions (Java 11 - Java 21) and methods (maven, CLI, packet managers), so it con be confusing what to pick. I tried most of them and created a mess and a deep appreciation for the "reset to snapshot" feature afforded by virtual machines. Instructions work for macOS, Linux or Linux on Windows.Here we go:

  1. install SdkMan.

The tool helps to keep your software development kits under control. From their website:

"Meet SDKMAN! – your reliable companion for effortlessly managing multiple Software Development Kits on Unix systems. Imagine having different versions of SDKs and needing a stress-free way to switch between them. SDKMAN! steps in with its easy-to-use Command Line Interface (CLI) and API."

curl -s "https://get.sdkman.io" | bash

You can thank me later. Side note: there are 16 different JDK offerings that can be installed, we are spoiled for choices

  1. install GraalVM

Currently, as of time of writing, there are three GraalVM distributions available. The Open Source, community supported GraalVM Community Edition, the commercial, Oracle supported Oracle GraalVM which requires a license in production and the ReedHat backed Mandrel. Mandrel is advertised as "specifically to support Quarkus". The Java 21 version was not yet available on sdkman, so I used the community edition. To stick with Mandrel I will use the container build option, later more on that

# List SDKs
sdk list java
# install GraalVM
sdk install java 21-graaalce
  1. Install Quarkus

This will install the Quarkus CLI and for good measure Apache Maven. The Quarkus CLI makes a pleasant developer experience

sdk install quarkus
sdk install mvn
  1. Install Docker

You can use Docker desktop (required a license for larger organisations) or Rancher Desktop (which also handles Kubernetes), Podman Desktop, any of the alternatives or the command line. New to Docker? There's plenty of fish

Now we are good to go. Skipping the Code with Quarkus tutorial lets build a n app in java and native put it into a container

Getting started

The quarkus command line allows to kick start a new application and to install any of the over 600 extensions (which modify the pom.xml)

# init a new project
quarkus create app com.notessensei.demo:native-is-fun
# add docker support
quarkus extension add 'container-image-docker'
# start dev mode with hot reload
quarkus dev

Navigate to http://localhost:8080 and http://localhost:8080/hello to see your app in action.

Now it's a good time to integrate Quarkus into your IDE.

Build time

Quarkus provides full support for maven, so you can use your familiar workflow with mvn clean package. However there's a more interesting option available. You can use a build image. This has the advantage that you build in a clean environment and don't get bitten by the configuration of your machine, the syndrom known as "Works on my machine". Here you can also ensure that your code will run in the targeted version. I have the community edition installed, but want to ship on Mandrel.

# Use the quarkus CLI to build in a container
quarkus build  \
  -Dquarkus.native.container-build=true \
  -Dquarkus.native.builder-image=quay.io/quarkus/ubi-quarkus-mandrel-builder-image:jdk-21

Going native

Compile to JVM free binaries, pack it into a container for deployment. You have 2 options:

  • use the build in container support
# Use the quarkus CLI to build in a container
quarkus build --native \
  -Dquarkus.native.container-build=true \
  -Dquarkus.native.builder-image=quay.io/quarkus/ubi-quarkus-mandrel-builder-image:jdk-21 \
  -Dquarkus.container-image.build=true
  • use a docker command with s tuned image
# first build
mvn clean package -Dnative
# then dockerize (don't forget the dot at the end)
docker build --file src/main/docker/Dockerfile.native-micro --tag stw/native-is-fun .

You can verify the success using docker image ls or run the image

docker run -i --rm -p 8080:8080 stw/native-is-fun

As usual YMMV


Posted by on 13 October 2023 | Comments (0) | categories: Java WebDevelopment

Comments

  1. No comments yet, be the first to comment