Build and deploy a Quarkus application to OpenShift in minutes

This exercise, created by Ian Lawson, demonstrates how you can go from initial app idea to prototype code in as little as five minutes using Quarkus, Podman Desktop, and the no-cost Developer Sandbox. We'll scaffold a Quarkus application, build a container image locally using Podman Desktop, and then see how to install, run, and test the application in the Developer Sandbox from the command line.

In order to get full benefit from taking this lesson, you need:

In this lesson, you will:

  • Bootstrap a Quarkus application and edit the code
  • Build and test an image

Bootstrap the Quarkus application

The first step is to get some nice Quarkus source code from the Quarkus code scaffolding page. For this example, I will fall back on my old favorite, RESTEasy Classic.

  1. Set your Group appropriately (I always use org.uth), give your app a name, and choose the RESTEasy Classic option. 
  2. Make sure the Starter Code is set to Yes, then click the Generate your application button (Figure 1).
    Generate the Quarkus code.
    Figure 1: Generate the Quarkus code.
  3. Download the juicy ZIP file full of Quarkus goodness and unzip it in an empty directory.
  4. This scaffolded code gives you all the bits and pieces for your Quarkus application, including a Containerfile we will use shortly.

Edit the Java code

For the sake of this quick example, let's add another endpoint to the RESTful application at a subdirectory to differentiate the code.

  1. If you go into the root directory where you extracted the ZIP file, you will see a directory called src.
  2. This directory contains all the source code for the app, including the one-stop configuration file (application.properties) and the Docker/Podman build files for creating a composite Quarkus application, which we will use in a later step.
  3. For now, edit the Java file at the bottom of the src/main/java/ directory structure. There will be subdirectories based on your organization name, but the file will be called GreetingResource.java. This Java file is the scaffolded example from the Quarkus code page.
  4. Add the additional method and annotations for the @Path("/subdir"):
    package org.uth;
    import jakarta.ws.rs.GET;
    import jakarta.ws.rs.Path;
    import jakarta.ws.rs.Produces;
    import jakarta.ws.rs.core.MediaType;
    @Path("/hello")
    public class GreetingResource {
        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String hello() {
            return "Hello RESTEasy";
        }
    
        @Path("/subdir")
        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String systemTime() {
            return "Systime: " + System.currentTimeMillis();
        }
    }
    

Test with Quarkus dev mode

One of the nicest features of Quarkus is the dev mode feature, which enables hot deployment with background compilation. We will use it now to see if our code change has worked.

  1. At the root directory point where the pom.xml lives, type quarkus dev to run up the application from the source code. To test it, go to another terminal window and type:
    curl http://localhost:8080/hello/subdir
  2. If all has gone well, you should see a microsecond system time.
  3. As a quick test to show the power of the Quarkus dev mode, in the new terminal window, edit the file to change Systime to something else, and select Save.
  4. Repeat the cURL command, and whatever change you made will be reflected in the output.

Prepare the image build components

So, Quarkus dev mode works a treat, but the Containerfiles provided to build an image from the source need some binary components that aren't created as part of the dev mode.

  1. Exit the dev mode in the terminal where it is running (just hit to quit).
  2. To quickly create the necessary components, type the following command at the root directory (where the pom.xml lives):
    ./mvnw clean install
  3. If you look in the directory, there is a new folder called target where the binary artifacts are staged/created.
  4. If you do a directory listing, you will see a subdirectory called quarkus-app. The Containerfile uses this as the application component when building the composite image.

Congratulations. You're done with Lesson 1. Now it's time to move on to using the Developer Sandbox to run the application.

Previous resource
Overview: Build and deploy a Quarkus application to OpenShift in minutes
Next resource
Connect the image using Podman and the Developer Sandbox