Quarkus – Creating First Application

Quarkus is an open-source Java framework. It generates executables that we can run native platform, which is small in size, consumes fewer resources, and is very fast. The Quarkus framework is beneficial in creating cloud-based Java applications as it consumes fewer resources and starts quickly compared to conventional Java applications.

In this article, we will create our first Quarkus application.

Table of Contents

Benefits of Quarkus framework

Below are a few of the essential benefits of the Quarkus application.

  • Suitable for serverless/cloud application development, as it uses less start-up time, and very minimal memory footprint.
  • Makes the application development easy by providing the live-reload feature.
  • We can also integrate Quarkus with other popular frameworks like Spring boot, etc.

Creating first Quarkus application

We can create the Quarkus app in many ways. Quarkus provides an online initializer tool, that can be used to generate the project template.

We can also use IDE-specific plugins or Maven plugins, etc. to generate the Quarkus project template.

In this article, we will use the IntelliJ IDE plugin in this example. We can install the IDE plugin from IntelliJ’s marketplace.

Once the plugin is installed, we get a new Quarkus generator option while creating new projects as shown below.

IntelliJ plugin

Enter the Group and Artifact details for the maven project as shown below. We will leave the rest of the details as it is.

quarkus IntelliJ plugin

On the dependencies screen, we will select the ReastEasy with JSON-B dependency. This will help in creating REST endpoints.

Finally, click on the Finish button for generating the Quarkus project.

quarkus first app

The Quarkus application folder structure

The below images show the folder structure of a typical Quarkus app.

quarkus folder structure
  • The pom.xml file contains all the application dependencies and other maven plugins that are essential for the application.
  • The src/main/docker folder contains the docker files required for dockerizing the Quarkus application. It has different docker files for packaging the application into to legacy/native docker image.
  • The src/main/java folder contains the main Quarkus application logic.
  • The application.properties configuration file under the src/main/resources folder helps in customizing the Quarkus application configuration properties.
  • The src/main/test folder contains the Unit and integration tests for our application.
  •  We can also use the maven wrappers which help on running the application without maven being installed.

The POM file

The below image shows the complete POM file of the project.

<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.asbnotebook</groupId>
  <artifactId>first-quarkus-app</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <properties>
    <compiler-plugin.version>3.8.1</compiler-plugin.version>
    <maven.compiler.release>11</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>2.13.0.Final</quarkus.platform.version>
    <skipITs>true</skipITs>
    <surefire-plugin.version>3.0.0-M7</surefire-plugin.version>
  </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-resteasy-jsonb</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-arc</artifactId>
    </dependency>
    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy</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>${quarkus.platform.group-id}</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>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${compiler-plugin.version}</version>
        <configuration>
          <compilerArgs>
            <arg>-parameters</arg>
          </compilerArgs>
        </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>${surefire-plugin.version}</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
            <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>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <profiles>
    <profile>
      <id>native</id>
      <activation>
        <property>
          <name>native</name>
        </property>
      </activation>
      <properties>
        <skipITs>false</skipITs>
        <quarkus.package.type>native</quarkus.package.type>
      </properties>
    </profile>
  </profiles>
</project>
  • The project inherits quarkus-bom that brings in all the Quarkus related dependencies to our application.
  • We have the quarkus-resteasy-jsonb and quarkus-resteasy dependencies, that help in enabling the web application capabilities.
  • The quarkus-arc dependency enables the dependency injection in the Quarkus application.
  • We also have the junit and rest-assured related dependencies, that are used in the unit and integration testing of the application.
  • We also have the quarkus-maven-plugin that is essential for the Quarkus application. It enables the generation of the application jar, running the application, etc.
  • We also have a native profile, that is useful to build and run our application in native mode.

The starter code

The generated application contains a simple “hello world” application.

It exposes a /hello endpoint using the Json-B library that returns a “Hello ReastEasy” message when invoked.

@Path("/hello")
public class GreetingResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "Hello RESTEasy";
    }
}

The application also generates the test case to test the endpoint. The Quarkus uses Rest assured library as we saw earlier.

@QuarkusTest
public class GreetingResourceTest {

    @Test
    public void testHelloEndpoint() {
        given()
          .when().get("/hello")
          .then()
             .statusCode(200)
             .body(is("Hello RESTEasy"));
    }
}

Running the application

To run the application, open the terminal from within the project’s base folder and execute the below maven command.

Quarkus also supports live reload and hence any changes made to the application are reflected without requiring to restart of the application.

mvn quarkus:dev

Invoke the API from a browser, and we should see the message “Hello RESTEasy”.

quarkus first app

Conclusion

In this article, we learned about the Quarkus framework. We also learned how to create our first Quarkus application using the IntelliJ IDEA.

Leave a Reply