Developing and compiling your Quarkus applications with Apache Maven

Guide
  • Red Hat build of Quarkus 2.7
  • Updated 09 March 2023
  • Published 18 May 2022

Developing and compiling your Quarkus applications with Apache Maven

Guide
Red Hat build of Quarkus 2.7
  • Updated 09 March 2023
  • Published 18 May 2022

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

This guide describes how to create a Red Hat build of Quarkus project by using the Apache Maven plug-in.

Where applicable, alternative instructions for using the Quarkus command-line interface (CLI) are provided. The Quarkus CLI is intended for development mode only. Red Hat does not support using the Quarkus CLI in production environments.

Prerequisites
  • Have OpenJDK 11 or 17 installed and the JAVA_HOME environment variable set to specify the location of the Java SDK.

    • In Quarkus 2.7, building native executables by using Java 17 is provided as a Technology Preview feature. To build native executables for production deployments, use Java 11.

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

  • Apache Maven 3.8.4 or later is installed.

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. About 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, Eclipse Vert.x, Apache Camel, Apache Kafka, Hibernate ORM (JPA), and RESTEasy (JAX-RS).

As a developer, you can choose the Java frameworks you want for your Java applications, which you can run in Java Virtual Machine (JVM) mode or compile and run in native mode. Quarkus provides a container-first approach to building Java applications. The container-first approach facilitates the containerization and efficient execution of microservices and functions. For this reason, Quarkus applications have a smaller memory footprint and faster startup times.

In addition, Quarkus optimizes the application development process with capabilities, such as unified configuration, automatic provisioning of unconfigured services, live coding, and continuous testing that allows you to get instant feedback on your code changes.

For information about the differences between the Quarkus community version and Red Hat build of Quarkus, see Differences between the Quarkus community version and Red Hat build of Quarkus.

2. About Apache Maven and Quarkus

Apache Maven is a distributed build automation tool that is 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 by using an XML file. This ensures that the project is built correctly and uniformly.

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 the Red Hat hosted 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. Red Hat build of Quarkus applications use the following Maven plug-ins:

  • Quarkus Maven plug-in (quarkus-maven-plugin): Enables Maven to create Quarkus projects, packages your applications into 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

To use the Red Hat hosted Quarkus repository with your Quarkus Maven project, configure your user settings.xml file. 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. If you want to apply the configuration to a specific project only, use the -s option and specify the path to the project-specific settings.xml file.

Procedure
  1. Open the Maven HOME/.m2/settings.xml file in a text editor or integrated development environment (IDE).

    If there is no 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. 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 several 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. If you want to apply the configuration to a specific project only, use the -s option and specify the path to the project-specific settings.xml file.

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 the 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/<root-directory-of-the-downloaded-archive>/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 these issues, delete the cached version of your local repository located in the ~/.m2/repository directory to force a download of the latest Maven artifacts.

3. Creating a Quarkus project on the command line

Use the Red Hat build of Quarkus Maven plug-in on the command line to create a Quarkus project by providing attributes and values on the command line or by using the plug-in in interactive mode. You can also create a Quarkus project by using the Quarkus command-line interface (CLI). The resulting project includes the following elements:

  • The Maven structure

  • An associated unit test

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

  • Example Dockerfile files for JVM and native mode in src/main/docker

  • The application configuration file

Prerequisites
  • Have OpenJDK 11 or 17 installed and the JAVA_HOME environment variable set to specify the location of the Java SDK.

    • In Quarkus 2.7, building native executables by using Java 17 is provided as a Technology Preview feature. To build native executables for production deployments, use Java 11.

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

  • Have Apache Maven 3.8.4 or later installed.

  • Have the Quarkus command-line interface (CLI) installed, if you plan to use this method to create your project. For more information, see Installing the Quarkus CLI.

The Quarkus CLI is intended for development mode only. Red Hat does not support using the Quarkus CLI in production environments.

Procedure
  1. In a command terminal, enter the following command to verify that Maven is using OpenJDK 11 or 17 and that the Maven version is 3.8.4 or later:

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

  3. To use the Quarkus Maven plug-in to create a project, use one of the following methods:

    • Enter the following command:

      mvn com.redhat.quarkus.platform:quarkus-maven-plugin:2.7.7.Final-redhat-00005:create \
          -DprojectGroupId=<project_group_id> \
          -DprojectArtifactId=<project_artifact_id> \
          -DplatformGroupId=com.redhat.quarkus.platform \
          -DplatformArtifactId=quarkus-bom \
          -DplatformVersion=2.7.7.Final-redhat-00005

      In this command, replace the following values:

      • <project_group_id>: A unique identifier of your project

      • <project_artifact_id>: The name of your project and your project directory

    • Create the project in interactive mode:

      mvn com.redhat.quarkus.platform:quarkus-maven-plugin:2.7.7.Final-redhat-00005:create

      When prompted, enter the required attribute values.

      Alternatively, you can create your project using the default values for the project attributes by entering the following command:

      mvn com.redhat.quarkus.platform:quarkus-maven-plugin:2.7.7.Final-redhat-00005:create -B

    • Optionally, use the Quarkus CLI to enter the following command:

      quarkus create app my-groupId:my-artifactId
      • You can also get the list of available options with:

        quarkus create app --help

The following table lists the attributes that you can define with the create command:

Attribute Default Value Description

projectGroupId

org.acme

A unique identifier of your project.

projectArtifactId

code-with-quarkus

The name of your project and your project directory. If you do not specify the projectArtifactId, the Maven plug-in starts the interactive mode. If the directory already exists, the generation fails.

projectVersion

1.0-SNAPSHOT

The version of your project.

platformGroupId

com.redhat.quarkus.platform

The group id of your platform. All the existing platforms are provided by com.redhat.quarkus.platform. However, you can change the default value.

platformArtifactId

quarkus-bom

The artifact id of your platform BOM.

platformVersion

The latest platform version, for example, 2.7.7.Final-redhat-00005.

The version of the platform you want to use for your project. You can provide a version range and the Maven plug-in uses the latest version.

extensions

[]

The list of extensions you want to add to your project, separated by a comma.

By default, the Quarkus Maven plug-in uses the latest quarkus-bom file. The quarkus-bom file aggregates extensions so that you can reference them from your applications to align the dependency versions. When you are offline, the Quarkus Maven plug-in uses the latest locally available version of the quarkus-bom. If Maven finds the quarkus-bom version 2.0 or earlier, it uses the platform based on the quarkus-bom.

4. Creating a Quarkus project by configuring the pom.xml file

You can create a Quarkus project by configuring the Maven POM XML file.

Procedure
  1. Open the pom.xml file in a text editor.

  2. Add the configuration properties that contain the following items:

    • The Quarkus Maven plug-in version

    • The Quarkus BOM groupID, artifactID, and version

    • The Maven Surefire plug-in version

    <properties>
        <quarkus.platform.version>2.7.7.Final-redhat-00005</quarkus.platform.version>
        <quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
        <quarkus.platform.group-id>com.redhat.quarkus.platform</quarkus.platform.group-id>
        <surefire-plugin.version>3.0.0-M5</surefire-plugin.version>
    </properties>
  3. Add the Quarkus GAV (group, artifact, version) and use the quarkus-bom file to omit the versions of the different Quarkus dependencies:

    <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>
  4. Add the Quarkus Maven plug-in:

    <build>
        <plugins>
            <plugin>
                <groupId>${quarkus.platform.group-id}</groupId>
                <artifactId>quarkus-maven-plugin</artifactId>
                <version>${quarkus.platform.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
            </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>
        </plugins>
    </build>
    The maven-surefire-plugin runs the unit tests for your application.
  5. Optional: To build a native application, add a specific native profile that includes the Maven Failsafe Plugin:

    <profiles>
        <profile>
            <id>native</id>
            <properties>
                <quarkus.package.type>native</quarkus.package.type>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>${surefire-plugin.version}</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                                <configuration>
                                    <systemProperties>
                                        <native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
                                    </systemProperties>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    • Tests that include IT in their names and contain the @NativeImageTest annotation are run against the native executable.

    • For more details about how native mode differs from JVM mode, see Difference between JVM and native mode in the Quarkus Getting Started guide.

5. Creating the Getting Started project by 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 that are required to compile your project into a native executable.

With Red Hat build of Quarkus 2.7.6 SP1 and later, when you generate a new project by using the code.quarkus.redhat.com starter code, Java 17 is used by default. If you want an application to target Java 11, you must set the expected Java version in the Configure your application section of https://code.quarkus.redhat.com.

This section guide you through the process of generating a Quarkus Maven project, including the following activities:

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

  • Have prepared your environment to use Apache Maven. For more information, see Preparing your environment.

  • Have configured your Quarkus Maven repository. You can use either the Red Hat hosted Quarkus repository or you can download and configure the Quarkus Maven repository to create a Quarkus application with Maven.

  • Have the Quarkus command-line interface (CLI) installed, if you plan to use this method to start your Quarkus project in development mode. For more information, see Installing the Quarkus CLI.

The Quarkus CLI is intended for development mode only. Red Hat does not support using the Quarkus CLI in production environments.

Procedure
  1. On your web browser, navigate to https://code.quarkus.redhat.com.

  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 following setups:

      • 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. To display the fields that contain further application details, select More options.

    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. However, 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

      The code.quarkus.redhat.com repository 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. The extensions that you select are included as dependencies of your Quarkus application and the Quarkus platform also ensures that the extensions are compatible in future versions.

    Ensure that you do not use the RESTEasy and the 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 verified together, and are therefore easier to use and upgrade.

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

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

  5. To confirm your choices, select Generate your application. The download link for the archive that contains your generated project displays. The custom command that you can use to compile and start your application also displays.

  6. To save the archive with the generated project files to your machine, select Download the ZIP.

  7. Extract the contents of the archive.

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

    cd <directory_name>
  9. To compile and start your application in development mode, use one of the following ways:

    • Using Maven:

      ./mvnw quarkus:dev
    • Using the Quarkus CLI:

      quarkus dev

6. Configuring the Java compiler

By default, the Quarkus Maven plug-in passes compiler flags to the javac command from the maven-compiler-plugin plug-in.

Procedure
  • To customize the compiler flags used in development mode, add a configuration section to the plugin block and set the compilerArgs property. You can also set source, target, and jvmArgs. For example, to pass -verbose to both the JVM and javac, add the following lines:

    <plugin>
      <groupId>com.redhat.quarkus.platform</groupId>
      <artifactId>quarkus-maven-plugin</artifactId>
      <version>${quarkus.platform.version}</version>
    
      <configuration>
        <source>${maven.compiler.source}</source>
        <target>${maven.compiler.target}</target>
        <compilerArgs>
          <arg>-verbose</arg>
        </compilerArgs>
        <jvmArgs>-verbose</jvmArgs>
      </configuration>
    
      ...
    </plugin>

7. Installing and managing Java extensions

You can use Java extensions to expand the functionality of your application and to configure, boot, and integrate a framework into your application. This procedure shows you how to find and add extensions to your Quarkus project.

Prerequisites
  • You have a Quarkus Maven project.

  • Have the Quarkus command-line interface (CLI) installed, if you plan to use this method to manage your Quarkus extensions. For more information, see Installing the Quarkus CLI.

The Quarkus CLI is intended for development mode only. Red Hat does not support using the Quarkus CLI in production environments.

Procedure
  1. Navigate to your Quarkus project directory.

  2. List all of the available extensions by using one of the following ways:

    • Using Maven:

      ./mvnw quarkus:list-extensions
    • Using the Quarkus CLI:

      quarkus extension --installable
  3. Add an extension to your project by using one of the following ways:

    • Using Maven, enter the following command where <extension> is the group, artifact, version (GAV) of the extension that you want to add:

      ./mvnw quarkus:add-extension -Dextensions="<extension>"

      For example, to add the Agroal extension, enter the following command:

      ./mvnw quarkus:add-extension -Dextensions="io.quarkus:quarkus-agroal"
    • Using the Quarkus CLI, enter the following command where <extension> is the group, artifact, version (GAV) of the extension that you want to add:

      quarkus extension add "<extension>"
  4. To search for a specific extension, enter the extension name or partial name after -Dextensions=. The following example searches for extensions that contain the text jdbc, agroal, and non-exist-ent in the name:

    ./mvnw quarkus:add-extension -Dextensions=jdbc,agroal,non-exist-ent

    This command returns the following result:

    ❌ Multiple extensions matching 'jdbc'
        * io.quarkus:quarkus-jdbc-h2
        * io.quarkus:quarkus-jdbc-mariadb
        * io.quarkus:quarkus-jdbc-postgresql
        Be more specific, for example, by using the exact name or the full GAV.
    ✅ Adding extension io.quarkus:quarkus-agroal
    ❌ Cannot find a dependency matching 'non-exist-ent', maybe a typo?
    [...]

8. Importing your Quarkus project into an IDE

Although it is possible to develop your Quarkus project in a text editor, you might find it easier to use an integrated development environment (IDE) to work on your project. The following instructions show you how to import your Quarkus project into specific IDEs.

Prerequisites
  • Have a Quarkus Maven project.

  • Have the Quarkus command-line interface (CLI) installed, if you plan to start your project in development mode. For more information, see Installing the Quarkus CLI.

The Quarkus CLI is intended for development mode only. Red Hat does not support using the Quarkus CLI in production environments.

Procedure

Complete the steps in one of the following sections:

CodeReady Studio or Eclipse
  1. In CodeReady Studio or Eclipse, click FileImport.

  2. Select MavenExisting Maven Project.

  3. On the next screen, select the root location of the project. A list of the available modules appears.

  4. Select the generated project and click Finish.

  5. To compile and start your application, use one of the following ways:

    • Using Maven:

      ./mvnw quarkus:dev
    • Using the Quarkus CLI:

      quarkus dev
IntelliJ
  1. In IntelliJ, complete one of the following tasks:

    • Select FileNewProject From Existing Sources.

    • On the Welcome page, select Import project.

  2. Select the project root directory.

  3. Select Import project from external model, and then select Maven.

  4. Review the options, and then click Next.

  5. Click Finish.

  6. To compile and start your application, use one of the following ways:

    • Using Maven:

      ./mvnw quarkus:dev
    • Using the Quarkus CLI:

      quarkus dev
Apache NetBeans
  1. Select FileOpen Project.

  2. Select the project root directory.

  3. Click Open Project.

  4. To compile and start your application, use one of the following ways:

    • Using Maven:

      ./mvnw quarkus:dev
    • Using the Quarkus CLI:

      quarkus dev
Visual Studio Code
  1. Install the Java Extension Pack.

  2. In Visual Studio Code, open your project directory.

Verification

The project loads as a Maven project.

9. Configuring the Quarkus project output

Before you build your application, you can control the output of the build command by changing the default values of application properties in the application.properties file.

Prerequisites
  • You have a Quarkus Maven project.

Procedure
  1. Open the application.properties file in a text editor.

    TIP: You can find the application.properties file in the {project}/src/main/resources folder.

  2. Edit the values of properties that you want to change and save the file.

    The following table list the properties that you can change:

    Property Description Type Default

    quarkus.package.main-class

    The entry point of the application. In most cases, you need to change this value.

    string

    io.quarkus.runner.GeneratedMain

    quarkus.package.type

    The requested output type for the package. Can be set to either 'jar' (uses 'fast-jar'), 'legacy-jar' for the pre-1.12 default jar packaging, 'uber-jar', 'native', or 'native-sources'.

    string

    jar

    quarkus.package.manifest.add-implementation-entries

    Whether or not the implementation information should be included in the runner JAR file’s MANIFEST.MF file.

    boolean

    true

    quarkus.package.user-configured-ignored-entries

    Files that should not be copied to the output artifact.

    string (list)

    quarkus.package.runner-suffix

    The suffix that is applied to the runner JAR file.

    string

    -runner

    quarkus.package.output-directory

    The output folder for the application build. This is resolved relative to the build system target directory.

    string

    quarkus.package.output-name

    The name of the final artifact.

    string

10. Testing your Quarkus application in JVM mode with a custom profile

Similar to any other running mode, configuration values for testing are read from the src/main/resources/application.properties file.

By default, the test profile is active during testing in JVM mode, which means that properties prefixed with %test take precedence. For example, when you execute a test with the following configuration, the value returned for the property message is Test Value.

message=Hello
%test.message=Test Value

If the %test profile is not active (dev, prod), the value returned for the property message is Hello.

Your application might require multiple test profiles, for example, when you need to run the same set of tests against different database instances. To do this, you need to override the testing profile name, which can be done by setting the system property quarkus.test.profile when executing Maven. By doing so, you can control which sets of configuration values are active during the test.

To learn more about standard testing with the 'Starting With Quarkus' example, see Testing your Quarkus application with JUnit in the Getting Started guide.

Prerequisites
  • A Quarkus project created with Apache Maven.

Procedure

When running tests on a Quarkus application, the test configuration profile is set as active by default. However, you can change the profile to a custom profile by using the quarkus.test.profile system property.

  1. Run the following command to test your application:

mvn test -Dquarkus.test.profile=__<profile-name>__

You cannot use a custom test configuration profile in native mode. Native tests always run under the prod profile.

11. Logging the Quarkus application build classpath tree

The Quarkus build process adds deployment dependencies of the extensions that you use in the application to the original application classpath. You can see which dependencies and versions are included in the build classpath. The quarkus-bootstrap Maven plug-in includes the build-tree goal, which displays the build dependency tree for the application.

Prerequisites
  • You have a Quarkus Maven application.

Procedure
  • To list the build dependency tree of your application, enter the following command:

    ./mvnw quarkus:dependency-tree

The following extract provides an example of the output that the preceding command displays:

[INFO] --- quarkus-bootstrap-maven-plugin:2.7:build-tree (default-cli) @ getting-started ---
[INFO] org.acme:getting-started:jar:1.0-SNAPSHOT
[INFO] └─ io.quarkus:quarkus-resteasy-deployment:jar:2.7 (compile)
[INFO]    ├─ io.quarkus:quarkus-resteasy-server-common-deployment:jar:2.7 (compile)
[INFO]    │  ├─ io.quarkus:quarkus-core-deployment:jar:2.7 (compile)
[INFO]    │  │  ├─ commons-beanutils:commons-beanutils:jar:1.9.3 (compile)
[INFO]    │  │  │  ├─ commons-logging:commons-logging:jar:1.2 (compile)
[INFO]    │  │  │  └─ commons-collections:commons-collections:jar:3.2.2 (compile)
...
The mvn dependency:tree command displays only the runtime dependencies of your application

12. Producing a native executable

A native binary is an executable that is created to run on a specific operating system (OS) or CPU architecture.

The following list outlines some examples of a native executable:

  • A universal binary for Mac

  • An ELF binary for Linux

  • An EXE binary for Windows

When you build a native executable, one advantage is that your application and dependencies, including the JVM, are packaged into a single file. The native executable for your application contains the following items:

  • The compiled application code

  • The Java APIs

  • The required libraries

  • A reduced version of the Java virtual machine (JVM) for improved application startup times and minimal disk and memory footprint

To produce a native executable from your Quarkus application, you can select either an in-container build or a local-host build. The following table explains the different building options that you can use:

Table 1. Building options for producing a native executable
Building option Requires Uses Results in Benefits

In-container build - Supported

A container runtime, for example, Podman or Docker

The default registry.access.redhat.com/quarkus/mandrel-21-rhel8:21.3 builder image

A Linux 64-bit executable

GraalVM does not need to be set up locally, which makes your CI pipelines run more efficiently

Local-host build - Only supported upstream

A local installation of GraalVM or Mandrel

Its local installation as a default for the quarkus.native.builder-image property

An executable that has the same OS and CPU architecture as the machine on which the build is executed

An alternative for developers that are not allowed or don’t want to use tools such as Docker or Podman. Overall is faster than containers.

Red Hat build of Quarkus supports the building of native Linux executables by using the Red Hat build of Quarkus Native builder, which is a productized distribution of Mandrel. Building native executables by using Oracle GraalVM Community Edition (CE), Mandrel community edition, or any other distributions of GraalVM is not supported for Red Hat build of Quarkus.

12.1. Producing a native executable by using an in-container build

To create a native executable and run the native image tests, use the native profile that is provided by Red Hat build of Quarkus for an in-container build.

Prerequisites
  • Podman or Docker is installed.

  • The container has access to at least 8GB of memory.

Procedure
  1. Open the Getting Started project pom.xml file, and verify that the project includes the native profile:

    <profiles>
        <profile>
            <id>native</id>
            <properties>
                <quarkus.package.type>native</quarkus.package.type>
            </properties>
        </profile>
    </profiles>
  2. Build a native executable by using one of the following ways:

    • Using Maven:

      • For Docker:

        ./mvnw package -Pnative -Dquarkus.native.container-build=true
      • For Podman:

        ./mvnw package -Pnative -Dquarkus.native.container-build=true -Dquarkus.native.container-runtime=podman
    • Using the Quarkus CLI:

      • For Docker:

        quarkus build --native -Dquarkus.native.container-build=true
      • For Podman:

        quarkus build --native -Dquarkus.native.container-build=true -Dquarkus.native.container-runtime=podman
        Step results

        These commands create a *-runner binary in the target directory, where the following is true:

      • The *-runner file is the built native binary produced by Quarkus.

      • The target directory is a directory that Maven creates when you build a Maven application.

        Compiling a Quarkus application to a native executable consumes a large amount of memory during analysis and optimization. You can limit the amount of memory used during native compilation by setting the quarkus.native.native-image-xmx configuration property. Setting low memory limits might increase the build time.

  3. To run the native executable, enter the following command:

    ./target/*-runner
Additional resources

For more information, see 'Native executable configuration properties' in Compiling your Quarkus applications to native executables.

12.2. Producing a native executable by using a local-host build

If you are not using Docker or Podman, use the Quarkus local-host build option to create and run a native executable.

Using the local-host build approach is faster than using containers and is suitable for machines that use a Linux operating system.

Using the following procedure in production is not supported by Quarkus. Use this method only when testing or as a backup approach when Docker or Podman is not available.

Prerequisites
  • A local installation of Mandrel or GraalVm, correctly configured according to the Building a native executable guide.

    • Additionally, for a GraalVM installation, native-image must also be installed.

Procedure
  1. For GraalVM or Mandrel, build a native executable by using one of the following ways:

    • Using Maven:

      ./mvnw package -Pnative
    • Using the Quarkus CLI:

      quarkus build --native
      Step results

      These commands create a *-runner binary in the target directory, where the following is true:

      • The *-runner file is the built native binary produced by Quarkus.

      • The target directory is a directory that Maven creates when you build a Maven application.

        When you build the native executable, the prod profile is enabled unless modified in the quarkus.profile property.

  2. Run the native executable:

    ./target/*-runner
    • Native tests run using the prod profile by default unless modified in the quarkus.test.native-image-profile property.

Additional resources

For more information, see Producing a native executable.

12.3. Creating a container manually

This section shows you how to manually create a container image with your application for Linux AMD64. When you produce a native image by using the Quarkus Native container, the native image creates an executable that targets Linux AMD64. If your host operating system is different from Linux AMD64, you cannot run the binary directly and you need to create a container manually.

Your Quarkus Getting Started project includes a Dockerfile.native in the src/main/docker directory with the following content:

FROM registry.access.redhat.com/ubi8/ubi-minimal:8.5
WORKDIR /work/
RUN chown 1001 /work \
    && chmod "g+rwX" /work \
    && chown 1001:root /work
COPY --chown=1001:root target/*-runner /work/application

EXPOSE 8080
USER 1001

CMD ["./application", "-Dquarkus.http.host=0.0.0.0"]
Universal Base Image (UBI)

The following list displays the suitable images for use with Dockerfiles.

  • Red Hat Universal Base Image 8 (UBI8). This base image is designed and engineered to be the base layer for all of your containerized applications, middleware, and utilities.

    registry.access.redhat.com/ubi8/ubi:8.5
  • Red Hat Universal Base Image 8 Minimal (UBI8-minimal). A stripped-down UBI8 image that uses microdnf as a package manager.

    registry.access.redhat.com/ubi8/ubi-minimal:8.5
  • All Red Hat Base images are available on the Container images catalog site.

Procedure
  1. Build a native Linux executable by using one of the following methods:

    • Docker:

      ./mvnw package -Pnative -Dquarkus.native.container-build=true
    • Podman:

      ./mvnw package -Pnative -Dquarkus.native.container-build=true -Dquarkus.native.container-runtime=podman
  2. Build the container image by using one of the following methods:

    • Docker:

      docker build -f src/main/docker/Dockerfile.native -t quarkus-quickstart/getting-started .
    • Podman

      podman build -f src/main/docker/Dockerfile.native -t quarkus-quickstart/getting-started .
  3. Run the container by using one of the following methods:

    • Docker:

      docker run -i --rm -p 8080:8080 quarkus-quickstart/getting-started
    • Podman:

      podman run -i --rm -p 8080:8080 quarkus-quickstart/getting-started

13. Testing the native executable

Test the application in native mode to test the functionality of the native executable. Use the @NativeImageTest annotation to build the native executable and run tests against the HTTP endpoints.

Procedure
  1. Open the pom.xml file and verify that the native profile has the following elements:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <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>

    The Maven Failsafe plug-in (maven-failsafe-plugin) runs the integration test and also indicates the location of the native executable that is generated.

  2. Open the src/test/java/org/acme/quickstart/NativeGreetingResourceIT.java file and verify that it includes the following content:

    package org.acme.quickstart;
    
    
    import io.quarkus.test.junit.NativeImageTest;
    
    @NativeImageTest 1
    public class NativeGreetingResourceIT extends GreetingResourceTest { 2
    
        // Run the same tests
    
    }
    1. Use another test runner that starts the application from the native file before the tests. The executable is retrieved by using the native.image.path system property configured in the Maven Failsafe plug-in.

    2. This example extends the GreetingResourceTest, but you can also create a new test.

  3. Run the test:

    ./mvnw verify -Pnative

    The following example shows the output of this command:

    ./mvnw verify -Pnative
    ...
    [getting-started-1.0-SNAPSHOT-runner:18820]     universe:     587.26 ms
    [getting-started-1.0-SNAPSHOT-runner:18820]      (parse):   2,247.59 ms
    [getting-started-1.0-SNAPSHOT-runner:18820]     (inline):   1,985.70 ms
    [getting-started-1.0-SNAPSHOT-runner:18820]    (compile):  14,922.77 ms
    [getting-started-1.0-SNAPSHOT-runner:18820]      compile:  20,361.28 ms
    [getting-started-1.0-SNAPSHOT-runner:18820]        image:   2,228.30 ms
    [getting-started-1.0-SNAPSHOT-runner:18820]        write:     364.35 ms
    [getting-started-1.0-SNAPSHOT-runner:18820]      [total]:  52,777.76 ms
    [INFO]
    [INFO] --- maven-failsafe-plugin:2.22.1:integration-test (default) @ getting-started ---
    [INFO]
    [INFO] -------------------------------------------------------
    [INFO]  T E S T S
    [INFO] -------------------------------------------------------
    [INFO] Running org.acme.quickstart.NativeGreetingResourceIT
    Executing [/data/home/gsmet/git/quarkus-quickstarts/getting-started/target/getting-started-1.0-SNAPSHOT-runner, -Dquarkus.http.port=8081, -Dtest.url=http://localhost:8081, -Dquarkus.log.file.path=build/quarkus.log]
    2019-04-15 11:33:20,348 INFO  [io.quarkus] (main) Quarkus 999-SNAPSHOT started in 0.002s. Listening on: http://[::]:8081
    2019-04-15 11:33:20,348 INFO  [io.quarkus] (main) Installed features: [cdi, resteasy]
    [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.387 s - in org.acme.quickstart.NativeGreetingResourceIT
    ...

    Quarkus waits for 60 seconds for the native image to start before automatically failing the native tests. You can change this duration by configuring the quarkus.test.wait-time system property.

    You can extend the wait time by using the following command where <duration> is the wait time in seconds:

    ./mvnw verify -Pnative -Dquarkus.test.wait-time=<duration>

14. Using Quarkus development mode

Development mode enables hot deployment with background compilation, which means that when you modify your Java or resource files and then refresh your browser, the changes automatically take effect. This also works for resource files such as the configuration property file. You can use either Maven or the Quarkus command-line interface (CLI) to start Quarkus in development mode.

Prerequisites
  • Have a Quarkus Maven application.

  • Have the Quarkus command-line interface (CLI) installed, if you plan to use this method to start Quarkus in development mode. For more information, see Installing the Quarkus CLI.

The Quarkus CLI is intended for development mode only. Red Hat does not support using the Quarkus CLI in production environments.

Procedure
  1. Switch to the directory that contains your Quarkus application pom.xml file.

  2. To compile and start your Quarkus application in development mode, use one of the following methods:

    • Using Maven:

      ./mvnw quarkus:dev
    • Using the Quarkus CLI:

      quarkus dev
  3. Make changes to your application and save the files.

  4. Refresh the browser to trigger a scan of the workspace.

    If any changes are detected, the Java files are recompiled and the application is redeployed. Your request is then serviced by the redeployed application. If there are any issues with compilation or deployment, an error page appears.

    In development mode, the debugger is activated and listens on port 5005.

  5. Optional: To wait for the debugger to attach before running the application, include -Dsuspend:

    ./mvnw quarkus:dev -Dsuspend
  6. Optional: To prevent the debugger from running, include -Ddebug=false:

    ./mvnw quarkus:dev -Ddebug=false

15. Debugging your Quarkus project

When Red Hat build of Quarkus starts in development mode, debugging is enabled by default, and the debugger listens on port 5005 without suspending the JVM. You can enable and configure the debugging feature of Quarkus from the command line or by configuring the system properties. You can also use the Quarkus CLI to debug your project.

Prerequisites
  • Have a Red Hat build of Quarkus Maven project.

  • Have the Quarkus command-line interface (CLI) installed, if you plan to use this method to create your project. For more information, see Installing the Quarkus CLI.

The Quarkus CLI is intended for development mode only. Red Hat does not support using the Quarkus CLI in production environments.

Procedure

Use one of the following methods to control debugging:

Controlling the debugger by configuring system properties
  1. Change one of the following values of the debug system property where PORT is the port that the debugger is listening on:

    • false: The JVM starts with debug mode disabled.

    • true: The JVM starts in debug mode and is listening on port 5005.

    • client: The JVM starts in client mode and tries to connect to localhost:5005.

    • PORT: The JVM starts in debug mode and is listening on PORT.

  2. To suspend the JVM while running in debug mode, set the value of the suspend system property to one of the following values:

    • y or true: The debug mode JVM launch suspends.

    • n or false: The debug mode JVM starts without suspending.

Controlling the debugger from the command line
  • To compile and start your Quarkus application in debug mode with a suspended JVM, use one of the following ways

    • Using Maven:

      ./mvnw quarkus:dev -Dsuspend
    • Using the Quarkus CLI:

      quarkus dev -Dsuspend
Enabling the debugger for specific host network interfaces

In development mode, by default, for security reasons, Quarkus sets the debug host interface to localhost.

To enable the debugger for a specific host network interface, you can use the -DdebugHost option by using one of the following ways:

  • Using Maven:

    ./mvnw quarkus:dev -DdebugHost=<host-ip-address>
  • Using the Quarkus CLI:

    quarkus dev -DdebugHost=<host-ip-address>

Where <host-ip-address> is the IP address of the host network interface that you want to enable debugging on.

To enable debugging on all host interfaces, replace <host-ip-address> with the following value:

0.0.0.0

16. Additional resources

Revised on 2023-03-09 05:34:43 UTC