Getting started with Quarkus

Guide
  • Red Hat build of Quarkus 2.2
  • Updated 22 February 2022
  • Published 02 December 2021

Getting started with Quarkus

Guide
Red Hat build of Quarkus 2.2
  • Updated 22 February 2022
  • Published 02 December 2021

As an application developer, you can use Red Hat build of Quarkus to create microservices-based applications written in Java that run in OpenShift and serverless environments. Applications compiled to native executables have small memory footprints and fast startup times.

This guide shows you how to use Apache Maven to create, test, package, and run a simple Quarkus project that exposes a hello HTTP endpoint. To demonstrate dependency injection, this endpoint uses a greeting bean.

78 OpenShift Quarkus Arch 0420
Prerequisites
  • Have OpenJDK (JDK) 11 installed and the JAVA_HOME environment variable set to specify the location of the Java SDK.

    • Log in to the Red Hat Customer Portal to download Red Hat build of Open JDK from the Software Downloads page.

  • Have Apache Maven 3.8.1 or later installed. Maven is available from the Apache Maven Project website.

For a completed example of the getting started exercise, download the Quarkus quickstart archive or clone the Quarkus Quickstarts Git repository. The Getting Started example is in the getting-started directory.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. We are beginning with these four terms: master, slave, blacklist, and whitelist. Because of the enormity of this endeavor, these changes will be implemented gradually over several upcoming releases. For more details, see our CTO Chris Wright’s message.

1. Red Hat build of Quarkus

Red Hat build of Quarkus is a Kubernetes-native Java stack that is optimized for use with containers and Red Hat OpenShift Container Platform. Quarkus is designed to work with popular Java standards, frameworks, and libraries such as Eclipse MicroProfile, Apache Kafka, RESTEasy (JAX-RS), Hibernate ORM (JPA), Spring, Infinispan, and Apache Camel.

The Quarkus dependency injection solution is based on CDI (contexts and dependency injection) and includes an extension framework to expand functionality and to configure, boot, and integrate a framework into your application. Dependency injection in the Red Hat build of Quarkus is based on Quarkus ArC, a CDI-based build-time-oriented dependency injection solution for Quarkus architecture. ArC is a transitive quarkus-resteasy dependency, and as such, it is already present in your project.

Quarkus provides a container-first approach to building Java applications. This approach makes it much easier to build microservices-based applications written in Java as well as enabling those applications to invoke functions running on serverless computing frameworks. For this reason, Quarkus applications have small memory footprints and fast startup times.

1.1. Quarkus BOMs

In Red Hat build of Quarkus 2.2, dependency versions of all core Quarkus extensions are managed by using com.redhat.quarkus.platform:quarkus-bom.

To view information about supported extension-specific BOMs that are available with Red Hat build of Quarkus, see Red Hat build of Quarkus Component Details.

With the new BOMs, you have fewer dependencies to manage than with the monolithic single BOM, because you only need to import the member-specific BOM for the platform-member extensions that you use in your application. Because every member-specific BOM is actually a fragment of the universal Quarkus BOM, you can import the member BOMs in any order without creating a conflict.

Red Hat supports only productized platform-member BOMs. Quarkus 2.2 does not support community-specific BOMs in a production environment.

2. Apache Maven and Quarkus

Apache Maven is a distributed build automation tool used in Java application development to create, manage, and build software projects. Maven uses standard configuration files called Project Object Model (POM) files to define projects and manage the build process. POM files describe the module and component dependencies, build order, and targets for the resulting project packaging and output using an XML file. This ensures that the project is built in a correct and uniform manner.

Maven repositories

A Maven repository stores Java libraries, plug-ins, and other build artifacts. The default public repository is the Maven 2 Central Repository, but repositories can be private and internal within a company to share common artifacts among development teams. Repositories are also available from third-parties.

You can use Red Hat’s hosted online Maven repository with your Quarkus projects or you can download the Red Hat build of Quarkus Maven repository.

Maven plug-ins

Maven plug-ins are defined parts of a POM file that achieve one or more goals. Quarkus applications use the following Maven plug-ins:

  • Quarkus Maven plug-in (quarkus-maven-plugin): Enables Maven to create Quarkus projects, packages your applications as JAR files, and provides a development mode.

  • Maven Surefire plug-in (maven-surefire-plugin): When Quarkus enables the test profile, the Maven Surefire plug-in is used during the test phase of the build life cycle to execute unit tests on your application. The plug-in generates text and XML files that contain the test reports.

2.1. Configuring the Maven settings.xml file for the online repository

You can use the Red Hat’s hosted online Quarkus repository with your Quarkus Maven project by configuring your user settings.xml file. This is the recommended approach. Maven settings used with a repository manager or repository on a shared server provide better control and manageability of projects.

When you configure the repository by modifying the Maven settings.xml file, the changes apply to all of your Maven projects.
Procedure
  1. Open the Maven ~/.m2/settings.xml file in a text editor or integrated development environment (IDE).

    If there is not a settings.xml file in the ~/.m2/ directory, copy the settings.xml file from the $MAVEN_HOME/.m2/conf/ directory into the ~/.m2/ directory.
  2. Add the following lines to the <profiles> element of the settings.xml file:

    <!-- Configure the Quarkus Maven repository -->
    <profile>
      <id>red-hat-enterprise-maven-repository</id>
      <repositories>
        <repository>
          <id>red-hat-enterprise-maven-repository</id>
          <url>https://maven.repository.redhat.com/ga/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>red-hat-enterprise-maven-repository</id>
          <url>https://maven.repository.redhat.com/ga/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  3. Add the following lines to the <activeProfiles> element of the settings.xml file and save the file.

    <activeProfile>red-hat-enterprise-maven-repository</activeProfile>

2.2. Downloading and configuring the Quarkus Maven repository

If you do not want to use the online Maven repository, you can download and configure the Quarkus Maven repository to create a Quarkus application with Maven. The Quarkus Maven repository contains many of the dependencies that Java developers typically use to build their applications. This procedure describes how to edit the settings.xml file to configure the Quarkus Maven repository.

When you configure the repository by modifying the Maven settings.xml file, the changes apply to all of your Maven projects.
Procedure
  1. Download the Quarkus Maven repository ZIP file from the Software Downloads page of the Red Hat Customer Portal (login required).

  2. Extract the downloaded archive.

  3. Change directory to the ~/.m2/ directory and open the Maven settings.xml file in a text editor or integrated development environment (IDE).

  4. Add the path of the Quarkus Maven repository that you downloaded to the <profiles> element of the settings.xml file. The format of the path of the Quarkus Maven repository must be file://$PATH, for example file:///home/userX/rh-quarkus-2.2.5.GA-maven-repository/maven-repository.

    <!-- Configure the Quarkus Maven repository -->
    <profile>
      <id>red-hat-enterprise-maven-repository</id>
      <repositories>
        <repository>
          <id>red-hat-enterprise-maven-repository</id>
          <url>file:///path/to/Quarkus/Maven/repository/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <id>red-hat-enterprise-maven-repository</id>
          <url>file:///path/to/Quarkus/Maven/repository/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
        </pluginRepository>
      </pluginRepositories>
    </profile>
  5. Add the following lines to the <activeProfiles> element of the settings.xml file and save the file.

    <activeProfile>red-hat-enterprise-maven-repository</activeProfile>

If your Maven repository contains outdated artifacts, you might encounter one of the following Maven error messages when you build or deploy your project, where <artifact_name> is the name of a missing artifact and <project_name> is the name of the project you are trying to build:

  • Missing artifact <project_name>

  • [ERROR] Failed to execute goal on project <artifact_name>; Could not resolve dependencies for <project_name>

To resolve the issue, delete the cached version of your local repository located in the ~/.m2/repository directory to force a download of the latest Maven artifacts.

3. Reconfiguring your Maven project to Red Hat build of Quarkus

You can migrate a Quarkus community project to Red Hat build of Quarkus by changing the Maven configuration in your project POM.

Prerequisites
Procedure
  • Change the following values in the <properties> section of the pom.xml file of your project:

    • Change the value of the <quarkus.platform.group-id> property to com.redhat.quarkus.platform.

    • Change the value of the <quarkus.platform.version> property to 2.2.5.Final-redhat-00007.

      pom.xml
      <project>
        ...
        <properties>
           ...
           <quarkus.platform.group-id>com.redhat.quarkus.platform</quarkus.platform.group-id>
           <quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
           <quarkus.platform.version>2.2.5.Final-redhat-00007</quarkus.platform.version>
           ...
        </properties>
        ...
      </project>

4. Configuring Quarkus developer tools

With the Quarkus developer tools you can easily complete a host of tasks. For example:

  • Creating a Maven project for your application.

  • Adding and configuring an extension that you use in your application.

  • Deploying your application on an OpenShift cluster.

4.1. Configuring Quarkus extension registry client

Quarkus 2.2 introduces an extension registry, registry.quarkus.redhat.com, which hosts Quarkus extensions provided by Red Hat. You can configure your Quarkus developer tools to access extensions in this registry by adding the registry to your registry client configuration file, a YAML file that contains a list of registries.

  • The default Quarkus registry is registry.quarkus.io and you normally don’t have to configure it. However, if a user provides a custom registry list and registry.quarkus.io is not in it, registry.quarkus.io is not enabled.

  • Ensure that the registry you prefer appears first in the registry list because Quarkus developer tools search for registries in the order they are configured.

Procedure
  1. Open the config.yaml file that contains your extension registry configuration. When you configure your extension registries for the first time, you might need to create a config.yaml file in the <user_home_directory_name>/.quarkus directory on your machine.

  2. Add the new registry to this file. For example:

    config.yaml
    registries:
      - registry.quarkus.redhat.com
      - registry.quarkus.io

5. Creating the Getting Started project

The getting-started project lets you get up and running with a simple Quarkus application using Apache Maven and the Quarkus Maven plug-in.

Procedure
  1. In a command terminal, enter the following command to verify that Maven is using JDK 11 or 17, and that the Maven version is 3.8.1 or higher:

    mvn --version
  2. If the preceding command does not return JDK 11 or 17, add the path to JDK 11 to the PATH environment variable and enter the preceding command again.

  3. To generate the project, enter one of the following commands:

    Apple macOS and Microsoft Windows are supported as developer environments, but are not supported as production environments.
    • If you are using Linux or Apple macOS, enter the following command:

      mvn com.redhat.quarkus.platform:quarkus-maven-plugin:2.2.5.Final-redhat-00007:create \
          -DprojectGroupId=org.acme \
          -DprojectArtifactId=getting-started \
          -DplatformGroupId=com.redhat.quarkus.platform \
          -DplatformVersion=2.2.5.Final-redhat-00007 \
          -DclassName="org.acme.quickstart.GreetingResource" \
          -Dpath="/hello"
      cd getting-started
    • If you are using the Microsoft Windows command line, enter the following command:

      mvn com.redhat.quarkus.platform:quarkus-maven-plugin:2.2.5.Final-redhat-00007:create
      -DprojectGroupId=org.acme -DprojectArtifactId=getting-started
      -DplatformGroupId=com.redhat.quarkus.platform
      -DplatformVersion=2.2.5.Final-redhat-00007
      -DclassName="org.acme.quickstart.GreetingResource"
      -Dpath="/hello"
    • If you are using the Microsoft Windows Powershell, enter the following command:

      mvn com.redhat.quarkus.platform:quarkus-maven-plugin:2.2.5.Final-redhat-00007:create
      "-DprojectGroupId=org.acme"
      "-DprojectArtifactId=getting-started"
      "-DplatformVersion=2.2.5.Final-redhat-00007"
      "-DplatformGroupId=com.redhat.quarkus.platform"
      "-DclassName=org.acme.quickstart.GreetingResource"
      "-Dpath=/hello"

      These commands create the following elements in the ./getting-started directory:

      • The Maven project directory structure

      • An org.acme.quickstart.GreetingResource resource exposed on /hello

      • Associated unit tests for testing your application in native mode and JVM mode

      • A landing page that is accessible on http://localhost:8080 after you start the application

      • Example Dockerfile.jvm, Dockerfile.native, and Dockerfile.legacy-jar files in the src/main/docker directory

      • The application configuration file

  4. After the directory structure is created, open the pom.xml file in a text editor and examine the contents of the file:

    pom.xml
    <project>
      ...
      <properties>
         ...
         <quarkus.platform.group-id>com.redhat.quarkus.platform</quarkus.platform.group-id>
         <quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
         <quarkus.platform.version>2.2.5.Final-redhat-00007</quarkus.platform.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>
      ...
      <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>
            ...
          </plugins>
          ...
      </build>
     ...
    </project>

    The Quarkus BOM is included in the <dependencyManagement> section of the pom.xml file. Therefore, you do not need to list the versions of individual Quarkus dependencies in the pom.xml file. In addition, you can see the quarkus-maven-plugin plug-in that is responsible for packaging the application and providing the development mode.

  5. Review the quarkus-resteasy dependency in the pom.xml file. This dependency enables you to develop REST applications:

    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-resteasy</artifactId>
    </dependency>
  6. Review the src/main/java/org/acme/quickstart/GreetingResource.java file:

    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    
    @Path("/hello")
    public class GreetingResource {
    
        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String hello() {
            return "Hello RESTEasy";
        }
    }

    This file contains a simple REST endpoint that returns hello as a response to a request that you send to the /hello endpoint.

    With Quarkus, the Application class for JAX-RS is supported but not required. In addition, only one instance of the GreetingResource class is created and not one per request. You can configure this by using different *Scoped annotations, for example ApplicationScoped, RequestScoped, and so forth.

You can create a Quarkus Maven project using the code.quarkus.redhat.com project generator. See Creating a Quarkus Maven project using code.quarkus.redhat.com for details.

6. Compiling and starting the Quarkus Getting Started project

After you have created the Quarkus Getting Started project, you can compile the Hello application and verify that the hello endpoint returns hello.

This example uses the Quarkus built-in development mode. In development mode, you can update the application sources and configurations while your application is running. Your changes will appear in the running application.

Prerequisites
  • You have created the Quarkus Getting Started project.

Procedure
  1. To compile the Quarkus Hello application in development mode, enter the following command from the project directory:

    ./mvnw quarkus:dev

    The following example shows the output of this command:

    [INFO] ----------------------< org.acme:getting-started >----------------------
    [INFO] Building getting-started 1.0.0-SNAPSHOT
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO]
    [INFO] --- quarkus-maven-plugin:2.2.3.Final-redhat-00013:generate-code (default) @ getting-started ---
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ getting-started ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 2 resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ getting-started ---
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 1 source file to /home/mjurc/Playground/getting-started/target/classes
    [INFO]
    [INFO] --- quarkus-maven-plugin:2.2.3.Final-redhat-00013:dev (default-cli) @ getting-started ---
    [INFO] Invoking org.apache.maven.plugins:maven-resources-plugin:2.6:testResources @ getting-started
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory /home/mjurc/Playground/getting-started/src/test/resources
    [INFO] Invoking org.apache.maven.plugins:maven-compiler-plugin:3.8.1:testCompile @ getting-started
    [INFO] Changes detected - recompiling the module!
    [INFO] Compiling 2 source files to /home/mjurc/Playground/getting-started/target/test-classes
    Listening for transport dt_socket at address: 5005
    __  ____  __  _____   ___  __ ____  ______
     --/ __ \/ / / / _ | / _ \/ //_/ / / / __/
     -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
    --\___\_\____/_/ |_/_/|_/_/|_|\____/___/
    Powered by Quarkus 2.2.3.Final-redhat-00013
    2021-11-23 16:16:19,014 INFO  [io.quarkus] (Quarkus Main Thread) getting-started 1.0.0-SNAPSHOT on JVM (powered by Quarkus 2.2.3.Final-redhat-00013) started in 1.582s. Listening on: http://localhost:8080
    2021-11-23 16:16:19,017 INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
    2021-11-23 16:16:19,018 INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy, smallrye-context-propagation]
  2. Enter the following command in a new terminal window to send a request to the endpoint provided by the application:

    getting-started curl -w "\n" http://localhost:8080/hello
    Hello RESTEasy

    This example uses the "\n" attribute to automatically add a new line before the output of the command. This prevents your terminal from printing a '%' character or putting both the result and the next command prompt on the same line.

  3. To stop the application, press Ctrl+C.

7. Using Quarkus dependency injection

Dependency injection enables a service to be used in a way that is completely independent of any client consumption. It separates the creation of client dependencies from the client’s behavior, which enables program designs to be loosely coupled.

Dependency injection in Red Hat build of Quarkus is based on Quarkus ArC which is a CDI-based build-time oriented dependency injection solution tailored for Quarkus architecture. Because ArC is a transitive dependency of quarkus-resteasy, and quarkus-resteasy is a dependency of your project, ArC will already be downloaded.

Prerequisites
  • You have created the Quarkus Getting Started project.

Procedure
  1. To modify the application and add a companion bean, create the src/main/java/org/acme/quickstart/GreetingService.java file with the following content:

    package org.acme.quickstart;
    
    import javax.enterprise.context.ApplicationScoped;
    
    @ApplicationScoped
    public class GreetingService {
    
        public String greeting(String name) {
            return "hello " + name;
        }
    
    }
  2. Edit the src/main/java/org/acme/quickstart/GreetingResource.java to inject the GreetingService and create a new endpoint using it:

    package org.acme.quickstart;
    
    import javax.inject.Inject;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    
    import org.jboss.resteasy.annotations.jaxrs.PathParam;
    
    @Path("/hello")
    public class GreetingResource {
    
        @Inject
        GreetingService service;
    
        @GET
        @Produces(MediaType.TEXT_PLAIN)
        @Path("/greeting/{name}")
        public String greeting(@PathParam String name) {
            return service.greeting(name);
        }
    
        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String hello() {
            return "hello";
        }
    }
  3. If you stopped the application, enter the following command to restart it:

    ./mvnw quarkus:dev
  4. To verify that the endpoint returns hello quarkus, enter the following command in a new terminal window:

    curl -w "\n" http://localhost:8080/hello/greeting/quarkus
    hello quarkus

8. Testing your Quarkus application with JUnit

After you compile your Quarkus Getting Started project, test your application with the JUnit 5 framework to ensure that it runs as expected. There are two test dependencies in the Quarkus project generated pom.xml file:

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

The quarkus-junit5 dependency is required for testing because it provides the @QuarkusTest annotation that controls the JUnit 5 testing framework. The rest-assured dependency is not required but because it provides a convenient way to test HTTP endpoints, it is integrated as well. It automatically sets the correct URL so no configuration is required.

These tests use the REST-assured framework, but you can use a different library if you prefer.
Prerequisites
  • You have compiled the Quarkus Getting Started project.

Procedure
  1. Open the generated pom.xml file and review the contents:

    <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>
           </systemPropertyVariable>
        </configuration>
    </plugin>

    Note, that:

    • the java.util.logging.manager system property is set to ensure that you application uses the correct log manager for the test.

    • the maven.home property points to the location of the settings.xml file in which you can store custom Maven configuration that you want to apply to your project.

  2. Edit the src/test/java/org/acme/quickstart/GreetingResourceTest.java file to match the following content:

    package org.acme.quickstart;
    
    import io.quarkus.test.junit.QuarkusTest;
    import org.junit.jupiter.api.Test;
    
    import java.util.UUID;
    
    import static io.restassured.RestAssured.given;
    import static org.hamcrest.CoreMatchers.is;
    
    @QuarkusTest
    public class GreetingResourceTest {
    
        @Test
        public void testHelloEndpoint() {
            given()
              .when().get("/hello")
              .then()
                 .statusCode(200)
                 .body(is("hello"));
        }
    
        @Test
        public void testGreetingEndpoint() {
            String uuid = UUID.randomUUID().toString();
            given()
              .pathParam("name", uuid)
              .when().get("/hello/greeting/{name}")
              .then()
                .statusCode(200)
                .body(is("hello " + uuid));
        }
    
    }
    By using the QuarkusTest runner, you instruct JUnit to start the application before starting the tests.
  3. To run these tests from Maven, enter the following command:

    ./mvnw test
    You can also run the test from your IDE. If you do this, make sure to stop the application first.

    By default, tests run on port 8081 so they do not conflict with the running application. In Quarkus, the RestAssured dependency is configured to use this port. If you want to use a different client, use the @TestHTTPResource annotation to directly inject the URL of the tested application into a field on the Test class. This field can be of the type String, URL or URI. You can also provide the test path in this annotation. For example, to test a servlet mapped to /myservlet, add the following lines to your test:

    @TestHTTPResource("/myservlet")
    URL testUrl;
  4. If necessary, specify the test port in the quarkus.http.test-port configuration property.

Quarkus also creates a system property called test.url that is set to the base test URL for situations where you cannot use injection.

9. Packaging and running the Quarkus Getting Started application

After you compile your Quarkus Getting Started project, you can package it in a JAR file and run it from the command line.

Prerequisites
  • You have compiled the Quarkus Getting Started project.

Procedure
  1. To package your Quarkus Getting Started project, enter the following command in the root directory:

    ./mvnw package

    This command produces the following JAR files in the /target directory:

    • getting-started-1.0-0-SNAPSHOT.jar: Contains the classes and resources of the projects. This is the regular artifact produced by the Maven build.

    • quarkus-app/quarkus-run.jar: Is an executable JAR file. This file is not an uber-JAR file because the dependencies are copied into the target/lib directory.

      When your application is running in development mode, you must press Ctrl+C to stop your application. You will encounter a port conflict when you try to package your application when development mode is enabled.
  2. Enter the following command to start your application:

    java -jar target/quarkus-app/quarkus-run.jar
    The Class-Path entry of the MANIFEST.MF file from the runner JAR file explicitly lists the JAR files from the lib directory. If you want to deploy your application from another location, you must copy the runner JAR file as well as the lib directory.

10. Difference between JVM and native mode

The following section describes the difference between a native application compiled with GraalVM’s native-image tool and a classic JVM application, and provides additional resources for developing applications for native mode.

When building an application, you can choose one of the following compilation methods.

  • Compile the application as a classic JVM application. Based on the quarkus.package.type configuration property, this results in one of the following:

    • fast-jar: A JAR file optimized for Quarkus and the default configuration option. Results in faster slightly startup time and slightly reduced memory usage

    • legacy-jar: A typical JAR file

    • uber-jar: A single standalone JAR file

      These JAR files work on all operating systems and are built much faster than native images.

  • Compile the application into a native image by setting the quarkus.package.type configuration property to native :

    • This creates an executable binary file compiled specifically for an operating system of your choice, such as the .exe file for Windows. These files have faster start times and lesser RAM consumption than JAVA JAR files, but their compilation takes several minutes. In addition, the maximum throughput achievable using a native binary is lower than a regular JVM application, because the profile-guided optimizations are missing.

Additional resources
  • See the Building a native executable section of Quarkus documentation for additional information such as:

    • Compiling the application to a native executable

    • Packaging the native executable in a container

    • Debugging native executable

  • For various tips and tricks for dealing with problems that might occur when attempting to run Java applications as native executables, see the Tips for writing native application guide.

11. Creating a Quarkus Maven project using code.quarkus.redhat.com

As an application developer, you can use code.quarkus.redhat.com to generate a Quarkus Maven project and automatically add and configure the extensions that you want to use in your application. In addition, code.quarkus.redhat.com automatically manages the configuration parameters required to compile your project into a native executable.

This section walks you through the process of generating a Quarkus Maven project, including:

  • Specifying basic details about your application.

  • Choosing the extensions that you want to include in your project.

  • Generating a downloadable archive with your project files.

  • Using the custom commands for compiling and starting your application.

Prerequisites
  • Have a web browser.

Procedure
  1. Navigate to https://code.quarkus.redhat.com using a web browser.

  2. Specify basic details about your project:

    1. Enter a group name for your project. The name format follows the Java package naming convention, for example, org.acme.

    2. Enter a name that you want to use for Maven artifacts generated from your project, for example, code-with-quarkus.

    3. Select the build tool you want to use to compile and start your application. The build tool that you choose determines:

      • the directory structure of your generated project

      • the format of configuration files used in your generated project

      • the custom build script and command for compiling and starting your application that code.quarkus.redhat.com displays for you after you generate your project

        Red Hat provides support for using code.quarkus.redhat.com to create Quarkus Maven projects only.

        Screenshot of the basic application details section on the code.quarkus.redhat.com site
  3. Specify additional details about your application project:

    1. Select Configure more options to display the fields that contain further application details.

    2. Enter a version that is used in artifacts generated from your project. The default value of this field is 1.0.0-SNAPSHOT. Using semantic versioning is recommended, but you can use a different type of versioning if you prefer.

    3. Select whether you want code.quarkus.redhat.com to add starter code to your project. When you add extensions that are marked with CODE to your project from the list of extensions, you can enable this option to automatically create example class files and resource files for those extensions when you generate your project. This option does not affect your generated project when you do not add any extensions that provide an example code.

      Screenshot of the application details section on the code.quarkus.redhat.com site showing the extended form with additional application details

      code.quarkus.redhat.com automatically uses the latest release of Red Hat build of Quarkus. You can manually change the BOM version in the pom.xml file after you generate your project.

  4. Select the extensions you want to use in your application from the list of extensions. The selected extensions are included as dependencies of your Quarkus application, with their versions being managed by the Quarkus platform for ensuring their compatibility. Ensure that you do not use the RESTEasy and RESTEasy Reactive extensions in the same project.

    The quark icon (quark) next to an extension indicates that the extension is part of the Red Hat build of Quarkus platform release. Red Hat recommends that you use extensions from the same platform, because they are tested and verfied together, and thus are easier to use and upgrade.

    You can enable the option to automatically generate starter code for extensions that are marked with CODE.

    Screenshot of the list of extensions at the code.quarkus.redhat.com site that you can add to your project

    • Red Hat provides different levels of support for individual extensions on the list, which are indicated by labels next to the name of each extension:

      • SUPPORTED Red Hat fully supports extensions for use in enterprise applications in production environments.

      • TECH-PREVIEW Red Hat offers limited support to extensions in production environments under the Technology Preview Features Support Scope.

      • DEV-SUPPORT Red Hat does not support extensions for use in production environments, but Red Hat developers support the core functionality that they provide for use in developing new applications.

      • DEPRECATED Red Hat plans to replace extensions with more recent technology or implementation that provides the same functionality.

      • CODE You can automatically generate the example code for extensions.

      • Red Hat does not support the unlabeled extensions for use in production environments.

    • Expand the overflow menu (⌄) next to each of the extensions to access additional options that you can use to:

      • add the extension to an existing project using the Quarkus maven plugin on the command line

      • copy an XML snippet to add the extension to a project’s pom.xml file

      • obtain the groupId, artifactId and version of each extension

      • open the extension guide

        Screenshot of an expanded overflow menu next to one of the extensions on code.quarkus.redhat.coim showing the custom commands that you can copy

  5. Select Generate your application to confirm your choices and display the overlay screen with the download link for the archive that contains your generated project. The overlay screen also shows the custom command that you can use to compile and start your application.

  6. Select Download the ZIP to save the archive with the generated project files to your machine.

  7. Extract the contents of the archive.

  8. Navigate to the directory that contains your extracted project files:

    cd <directory_name>
  9. Compile and start your application in development mode:

    ./mvnw quarkus:dev

12. Additional resources

Revised on 2022-02-22 13:04:26 UTC