9. Making a Simple Hello World Quarkus Application

Adarsh BhaskarAdarsh Bhaskar
5 min read

Quarkus is a Kubernetes-native Java framework tailored for GraalVM and HotSpot, designed to make Java a leading platform in Kubernetes and serverless environments. Its key advantages include:

  • Fast startup time (tens of milliseconds)

  • Low memory utilization

  • Optimized for developer productivity

  • Simplified deployment in cloud and containerized environments

This guide will walk you through creating a simple "Hello World" Quarkus application, explaining each component and how to set it up in different IDEs.

Project Structure

We'll create the following file structure:

demo/
├── pom.xml
├── Jenkinsfile
└── src/
    └── main/
        └── java/
            └── com/
                └── example/
                    ├── ExampleResource.java
                    └── MainApplication.java

Setting Up Your Development Environment

IntelliJ IDEA Setup

  • Install the Quarkus Tools plugin from the JetBrains Marketplace

  • Create a new project using File > New > Project

  • Select "Quarkus" as the project type

  • Configure your project settings (GroupId, ArtifactId, etc.)

  • Select the REST extension as a minimum

  • Create the project and navigate to the project structure

Eclipse Setup

  • Install the Quarkus Tools for Eclipse from the Eclipse Marketplace

  • Create a new project using File > New > Quarkus Project

  • Configure your project settings similarly to IntelliJ

  • Ensure you select the REST extensions

  • Finish and wait for the project to be created

Project Configuration (pom.xml)

Let's examine our Maven configuration file (C:\Users\Adars\Documents\Archived\baymaps\demo\pom.xml):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="<http://maven.apache.org/POM/4.0.0>" xmlns:xsi="<http://www.w3.org/2001/XMLSchema-instance>"
         xsi:schemaLocation="<http://maven.apache.org/POM/4.0.0> <https://maven.apache.org/xsd/maven-4.0.0.xsd>">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <compiler-plugin.version>3.11.0</compiler-plugin.version>
        <maven.compiler.release>17</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
        <quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
        <quarkus.platform.version>3.19.1</quarkus.platform.version>
        <surefire-plugin.version>3.0.0</surefire-plugin.version>
        <failsafe-plugin.version>3.0.0</failsafe-plugin.version>
        <quarkus.package.type>uber-jar</quarkus.package.type>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>${quarkus.platform.group-id}</groupId>
                <artifactId>${quarkus.platform.artifact-id}</artifactId>
                <version>${quarkus.platform.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-arc</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-junit5</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>io.quarkus</groupId>
                <artifactId>quarkus-maven-plugin</artifactId>
                <version>${quarkus.platform.version}</version>
                <extensions>true</extensions>
                <executions>
                    <execution>
                        <goals>
                            <goal>build</goal>
                            <goal>generate-code</goal>
                            <goal>generate-code-tests</goal>
                            <goal>native-image-agent</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${compiler-plugin.version}</version>
                <configuration>
                    <parameters>true</parameters>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${surefire-plugin.version}</version>
                <configuration>
                    <systemPropertyVariables>
                        <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
                        <maven.home>${maven.home}</maven.home>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>${failsafe-plugin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <systemPropertyVariables>
                        <native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
                        <java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
                        <maven.home>${maven.home}</maven.home>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Main-Class>com.example.MainApplication</Main-Class>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>native</id>
            <activation>
                <property>
                    <name>native</name>
                </property>
            </activation>
            <properties>
                <skipITs>false</skipITs>
                <quarkus.native.enabled>true</quarkus.native.enabled>
            </properties>
        </profile>
    </profiles>
</project>

The pom.xml file above defines several important aspects of our Quarkus project:

  • Basic project information: The groupId (com.example), artifactId (demo), and version (1.0-SNAPSHOT) identify our project.

  • Properties section: Defines important configuration values including Java version (17), encoding settings, and the Quarkus platform version (3.19.1).

  • Dependencies: Our project includes essential Quarkus dependencies:

    • quarkus-arc: Provides dependency injection support

    • quarkus-rest: Enables RESTful endpoints

    • Testing dependencies for JUnit5 and REST-assured

  • Build configuration: Sets up the Quarkus Maven plugin which provides goals for building, code generation, and native image creation.

  • Packaging: The quarkus.package.type property is set to uber-jar to package the application as a self-contained JAR.

REST Resource Implementation

Now, let's create our REST endpoint (C:\Users\Adars\Documents\Archived\baymaps\demo\src\main\java\com\example\ExampleResource.java):

package com.example;

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 ExampleResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        System.out.println("Hello from Quarkus!");
        return "Hello from Quarkus REST";
    }
}

This Java class defines a simple REST endpoint with these key components:

  • @Path("/hello"): Specifies the URL path for this resource (http://localhost:8080/hello)

  • @GET: Indicates this method responds to HTTP GET requests

  • @Produces(MediaType.TEXT_PLAIN): Sets the Content-Type header to text/plain

  • The method returns a simple text greeting and also logs a message to the console

Main Application Class

Next, let's create our main application class (C:\Users\Adars\Documents\Archived\baymaps\demo\src\main\java\com\example\MainApplication.java):

package com.example;
import io.quarkus.runtime.Quarkus;
import io.quarkus.runtime.QuarkusApplication;
import io.quarkus.runtime.annotations.QuarkusMain;

@QuarkusMain
public class MainApplication implements QuarkusApplication {

    @Override
    public int run(String... args) {
        System.out.println("Hello World from Quarkus");
        Quarkus.waitForExit();
        return 0;
    }
}

This class serves as the entry point for our Quarkus application:

  • @QuarkusMain: Marks this class as the main entry point for the Quarkus application

  • Implements QuarkusApplication interface which requires a run method

  • The run method prints a welcome message and calls Quarkus.waitForExit() to keep the application running until terminated

Setting Up CI/CD with Jenkins

For continuous integration, we'll create a Jenkinsfile (C:\Users\Adars\Documents\Archived\baymaps\demo\Jenkinsfile):

pipeline {
    agent any

    tools {
        maven 'Maven'
    }

    stages {
        stage('Verify Java and Maven') {
            steps {
                sh 'java -version'
                sh 'mvn -version'
                sh 'mvn compile'
            }
        }

        stage('Build') {
            steps {
                sh 'mvn clean install -DskipTests=true'
            }
        }

        stage('Run JAR with Timeout') {
            steps {
                sh 'java -jar target/demo-1.0-SNAPSHOT-runner.jar'
                sleep 10
            }
        }

        stage('Stop') {
            steps {
                sh 'echo "Done! Successfully Executed!"'
            }
        }
    }

    post {
        always {
            cleanWs()
        }
    }
}

Building and Running the Application

From the Command Line

  • Open a terminal in your project directory

  • Build the project: mvn clean package

  • Run in development mode: mvn quarkus:dev

  • Access your endpoint at: http://localhost:8080/hello

From IntelliJ IDEA

  • Right-click on the project and select "Run As" > "Quarkus Application"

  • The application will start in development mode

  • Access your endpoint at: http://localhost:8080/hello

From Eclipse

  • Right-click on the project

  • Select "Run As" > "Quarkus Application"

  • The application will start in development mode

  • Access your endpoint at: http://localhost:8080/hello

Conclusion

You've successfully created a simple "Hello World" Quarkus application with a REST endpoint. This foundation can be expanded to build more complex applications by adding additional endpoints, services, and dependencies as needed.

0
Subscribe to my newsletter

Read articles from Adarsh Bhaskar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Adarsh Bhaskar
Adarsh Bhaskar

Hi there! I’m Adarsh, a passionate information science student with hands-on experience in machine learning, software development, and data analysis. I thrive on solving complex problems and enjoy collaborating with teams to bring innovative solutions to life. Whether it’s developing a recommendation engine or streamlining workflows with automation, I love diving into new technologies. I’m always eager to learn and explore fresh ideas, especially in the world of Flutter app development!