このコンテンツは選択した言語では利用できません。
Getting started with Quarkus
Abstract
Chapter 1. Getting started with Quarkus リンクのコピーリンクがクリップボードにコピーされました!
As an application developer, you can use Red Hat build of Quarkus to create microservices-based applications written in Java that run on OpenShift environments. Quarkus applications can run on top of a Java virtual machine (JVM) or be compiled to native executables. Native applications have a smaller memory footprint and a faster startup time than their JVM counterpart.
You can create a Quarkus application in either of the following ways:
- Using Apache Maven and the Quarkus Maven plug-in
- Using code.quarkus.redhat.com
- Using the Quarkus command-line interface (CLI)
This guide shows how to get started with Quarkus and to create, test, package, and run a simple Quarkus project that exposes a hello HTTP endpoint. To demonstrate dependency injection, the hello HTTP endpoint uses a greeting bean.
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 available 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.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.
1.2. Preparing your environment リンクのコピーリンクがクリップボードにコピーされました!
Before you start using Quarkus, you must prepare your environment.
Procedure
Confirm the following installations are completed on your system:
OpenJDK 11 or 17 is installed and the
JAVA_HOMEenvironment variable is 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. Apache Maven is available from the Apache Maven Project website.
(Optional): If you want to use the Quarkus command-line interface (CLI), ensure that it is installed.
- For instructions on how to install the Quarkus CLI, refer to the community-specific information at Quarkus CLI.
The Quarkus CLI is intended for development mode only. Red Hat does not support using the Quarkus CLI in production environments.
1.2.1. About Quarkus BOMs リンクのコピーリンクがクリップボードにコピーされました!
From Red Hat build of Quarkus 2.2, dependency versions of all core Quarkus extensions are managed by using the com.redhat.quarkus.platform:quarkus-bom file.
The purpose of the Bill of Materials (BOM) file is to manage dependency versions of Quarkus artifacts in your project so that when you use a BOM in your project, you do not need to specify which dependency versions work together. Instead, you can import the Quarkus BOM file to the pom.xml configuration file, where the dependency versions are included in the <dependencyManagement> section of the file. Therefore, you do not need to list the versions of individual Quarkus dependencies that are managed by the specified BOM in the pom.xml file.
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.
You only need to import the member-specific BOM for the platform-member extensions that you use in your application. Therefore, you have fewer dependencies to manage as compared to a monolithic single BOM. Because every member-specific BOM is a fragment of the universal Quarkus BOM, you can import the member BOMs in any order without creating a conflict.
1.2.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 thetestprofile, the Maven Surefire plug-in is used during thetestphase 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.
1.2.3. 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
Open the Maven
HOME/.m2/settings.xmlfile in a text editor or integrated development environment (IDE).NoteIf there is no
settings.xmlfile in the~/.m2/directory, copy thesettings.xmlfile from the$MAVEN_HOME/.m2/conf/directory into the~/.m2/directory.Add the following lines to the
<profiles>element of thesettings.xmlfile:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the following lines to the
<activeProfiles>element of thesettings.xmlfile and save the file.<activeProfile>red-hat-enterprise-maven-repository</activeProfile>
<activeProfile>red-hat-enterprise-maven-repository</activeProfile>Copy to Clipboard Copied! Toggle word wrap Toggle overflow
1.2.4. 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
- Download the Quarkus Maven repository ZIP file from the Software Downloads page of the Red Hat Customer Portal (login required).
- Extract the downloaded archive.
-
Change the directory to the
~/.m2/directory and open the Mavensettings.xmlfile in a text editor or integrated development environment (IDE). Add the path of the Quarkus Maven repository that you downloaded to the
<profiles>element of thesettings.xmlfile. The format of the path of the Quarkus Maven repository must befile://$PATH, for example,file:///home/userX/<root-directory-of-the-downloaded-archive>/maven-repository.Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the following lines to the
<activeProfiles>element of thesettings.xmlfile and save the file.<activeProfile>red-hat-enterprise-maven-repository</activeProfile>
<activeProfile>red-hat-enterprise-maven-repository</activeProfile>Copy to Clipboard Copied! Toggle word wrap Toggle overflow
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.
1.2.5. 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
-
Have a Quarkus project built with Maven that depends on Quarkus community artifacts in the
pom.xmlfile.
Procedure
Change the following values in the
<properties>section of thepom.xmlfile of your project:-
Change the value of the
<quarkus.platform.group-id>property tocom.redhat.quarkus.platform. Change the value of the
<quarkus.platform.version>property to2.7.7.Final-redhat-00005.pom.xml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
-
Change the value of the
1.3. Configuring Quarkus developer tools リンクのコピーリンクがクリップボードにコピーされました!
Using Quarkus developer tools, you can complete a host of tasks, such as:
- 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.
1.3.1. Configuring Quarkus extension registry client リンクのコピーリンクがクリップボードにコピーされました!
The extension registry, registry.quarkus.redhat.com, hosts Quarkus extensions that are 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. The registry client configuration file is a YAML file that contains a list of registries.
-
The default Quarkus registry is
registry.quarkus.ioand typically, you do not need to configure it. However, if a user provides a custom registry list andregistry.quarkus.iois not on it, thenregistry.quarkus.iois not enabled. - Ensure that the registry you prefer appears first on the registry list, because Quarkus developer tools search for registries in the order that they are configured.
Procedure
-
Open the
config.yamlfile that contains your extension registry configuration. When you configure your extension registries for the first time, you might need to create aconfig.yamlfile in the<user_home_directory_name>/.quarkusdirectory on your machine. Add the new registry to this file. For example:
config.yaml
registries: - registry.quarkus.redhat.com - registry.quarkus.io
registries: - registry.quarkus.redhat.com - registry.quarkus.ioCopy to Clipboard Copied! Toggle word wrap Toggle overflow
1.4. Creating the Getting Started project リンクのコピーリンクがクリップボードにコピーされました!
By creating a getting-started project, you can get up and running with a simple Quarkus application. You can create a getting-started project in either of the following ways:
- Using Apache Maven and the Quarkus Maven plug-in
- Using code.quarkus.redhat.com to generate a Quarkus Maven project
- Using the Quarkus command-line interface (CLI)
Prerequisites
- Have prepared your environment. For more information, see Preparing your environment.
Procedure
-
Depending on your requirements, select the method you want to use to create your
getting-startedproject.
1.4.1. Creating the Getting Started project by using Apache Maven リンクのコピーリンクがクリップボードにコピーされました!
You can create a getting-started project by using Apache Maven and the Quarkus Maven plug-in. With this getting-started project, you can get up and running with a simple Quarkus application.
Prerequisites
- 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.
- If you use the Red Hat hosted Quarkus repository, see Configuring the Maven settings.xml file for the online repository.
- If you want to configure the Quarkus Maven repository to create a Quarkus application with Maven, see Configuring the Quarkus Maven repository.
Procedure
To verify that Maven is using OpenJDK 11 or 17, that the Maven version is 3.8.4 or later, and that
mvnis accessible from the PATH environment variable, enter the following command:mvn --version
mvn --versionCopy to Clipboard Copied! Toggle word wrap Toggle overflow - 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.
To generate the project, enter one of the following commands:
NoteApple 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:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you are using the Microsoft Windows command line, enter the following command:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you are using the Microsoft Windows Powershell, enter the following command:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow These commands create the following elements in the
./getting-starteddirectory:- The Maven project directory structure
-
An
org.acme.quickstart.GreetingResourceresource 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:8080after you start the application -
Example Dockerfiles in the
src/main/dockerdirectory - The application configuration file
After the directory structure is created, open the
pom.xmlfile in a text editor and examine the contents of the file:pom.xml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The Quarkus BOM is included in the
<dependencyManagement>section of thepom.xmlfile. Therefore, you do not need to list the versions of individual Quarkus dependencies in thepom.xmlfile. In addition, you can see thequarkus-maven-pluginplug-in that is responsible for packaging the application.Review the
quarkus-resteasydependency in thepom.xmlfile. This dependency enables you to develop REST applications:<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-resteasy</artifactId> </dependency><dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-resteasy</artifactId> </dependency>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Review the
src/main/java/org/acme/quickstart/GreetingResource.javafile:Copy to Clipboard Copied! Toggle word wrap Toggle overflow This file contains a simple REST endpoint that returns
helloas a response to a request that you send to the/helloendpoint.NoteWith Quarkus, the
Applicationclass for JAX-RS is supported but not required. In addition, only one instance of theGreetingResourceclass is created and not one per request. You can configure this by using different*Scopedannotations, for exampleApplicationScoped,RequestScoped, and so on.
1.4.2. 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.
- If you use the Red Hat hosted Quarkus repository, see Configuring the Maven settings.xml file for the online repository.
- If you want to configure the Quarkus Maven repository to create a Quarkus application with Maven, see Configuring the Quarkus Maven repository.
- 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
- On your web browser, navigate to https://code.quarkus.redhat.com.
Specify basic details about your project:
-
Enter a group name for your project. The name format follows the Java package naming convention, for example,
org.acme. -
Enter a name that you want to use for Maven artifacts generated from your project, for example,
code-with-quarkus. 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
NoteRed Hat provides support for using code.quarkus.redhat.com to create Quarkus Maven projects only.
-
Enter a group name for your project. The name format follows the Java package naming convention, for example,
Specify additional details about your application project:
- To display the fields that contain further application details, select More options.
-
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. 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.
NoteThe 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.xmlfile after you generate your project.
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.
ImportantEnsure that you do not use the
RESTEasyand theRESTEasy Reactiveextensions in the same project.The quark icon (
) 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.
- 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.
- To save the archive with the generated project files to your machine, select Download the ZIP.
- Extract the contents of the archive.
Navigate to the directory that contains your extracted project files:
cd <directory_name>
cd <directory_name>Copy to Clipboard Copied! Toggle word wrap Toggle overflow To compile and start your application in development mode, use one of the following ways:
Using Maven:
./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow Using the Quarkus CLI:
quarkus dev
quarkus devCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Additional resources
1.4.2.1. Support levels for Quarkus extensions リンクのコピーリンクがクリップボードにコピーされました!
Red Hat provides different levels of support for extensions that are available on code.quarkus.redhat.com that you can add to your Quarkus project. The support level is indicated by labels next to the name of each extension.
Red Hat does not support unlabeled extensions for use in production environments.
| Support level | Description |
|---|---|
| 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. |
| STARTER-CODE | You can automatically generate the example code for extensions. |
You can expand the overflow menu (⌄) next to each of the extensions to access additional options that you can use to do the following actions:
- Add the extension to an existing project using the Quarkus Maven plug-in on the command line
-
Copy an XML snippet to add the extension to a project’s
pom.xmlfile -
Obtain the
groupId,artifactId, andversionof each extension - Open the extension guide
1.4.3. Creating the Getting Started project by using the Quarkus CLI リンクのコピーリンクがクリップボードにコピーされました!
You can create your getting-started project by using the Quarkus command-line interface (CLI).
With the Quarkus CLI, you can create projects, manage extensions, and run build and development commands.
Use the Quarkus CLI in development mode only. The Quarkus CLI is intended for development mode only. Red Hat does not support using the Quarkus CLI in production environments.
Prerequisites
- Have installed the Quarkus CLI. For more information, see Preparing your environment.
- Have configured your Quarkus developer tools to access extensions in the extension registry. For more information, see Configuring Quarkus extension registry client.
Procedure
To generate the project, in a command terminal, enter the following command:
quarkus create && cd code-with-quarkus
quarkus create && cd code-with-quarkusCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteYou can also specify the 'app' subcommand, for example,
quarkus create app, however, it is not mandatory as the 'app' subcommand is implied if it is not specified.With this command, the Quarkus project is created in a folder called 'code-with-quarkus' in your current working directory.
By default, the
groupId,artifactId, andversionattributes are specified with the following default values:- groupId='org.acme'
- artifactId='code-with-quarkus'
version='1.0.0-SNAPSHOT'
To change the values of the
groupId,artifactId, andversionattributes, issue thequarkus createcommand and specify the following syntax on the CLI:groupId:artifactId:versionFor example,
quarkus create app mygroupId:myartifactid:version
NoteTo view information on all the available Quarkus commands, specify the ‘help’ parameter:
quarkus --help
quarkus --helpCopy to Clipboard Copied! Toggle word wrap Toggle overflow Review the
src/main/java/org/acme/GreetingResource.javafile in a text editor:Copy to Clipboard Copied! Toggle word wrap Toggle overflow This file contains a simple REST endpoint that returns
helloas a response to a request that you send to the/helloendpoint.
Verification
- Compile and start your application in development mode.
- Package and run your Getting Started project from the Quarkus CLI.
1.5. 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 procedure uses the Quarkus built-in development mode, which allows you to update the application sources and configurations while your application is running. The changes you make appear in the running application.
The command that you use to compile your Quarkus Hello application depends on the developer tool that you have installed on the machine.
Prerequisites
- You have created the Quarkus Getting Started project.
Procedure
- Navigate to the project directory.
To compile the Quarkus
Helloapplication in development mode, use one of the following methods, depending on the developer tool that you intend to use:If you prefer to use Apache Maven, enter the following command:
mvn quarkus:dev
mvn quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow If you prefer to use the Quarkus command-line interface (CLI), enter the following command:
quarkus dev
quarkus devCopy to Clipboard Copied! Toggle word wrap Toggle overflow If you prefer to use the Maven wrapper, use the following command:
./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow Expected output
The following extract shows an example of the expected output:
INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated. INFO [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy, smallrye-context-propagation]
INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated. INFO [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy, smallrye-context-propagation]Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
To send a request to the endpoint that is provided by the application, enter the following command in a new terminal window:
getting-started curl -w "\n" http://localhost:8080/hello Hello RESTEasy
getting-started curl -w "\n" http://localhost:8080/hello Hello RESTEasyCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThe
"\n"attribute automatically adds a new line before the output of the command, which prevents your terminal from printing a '%' character or putting both the result and the next shell prompt on the same line.
1.6. 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
To modify the application and add a companion bean, create the
src/main/java/org/acme/quickstart/GreetingService.javafile with the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Edit the
src/main/java/org/acme/quickstart/GreetingResource.javato inject theGreetingServiceand create a new endpoint using it:Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you stopped the application, enter the following command to restart it:
./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow 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
curl -w "\n" http://localhost:8080/hello/greeting/quarkus hello quarkusCopy to Clipboard Copied! Toggle word wrap Toggle overflow
1.7. Testing your Quarkus application リンクのコピーリンクがクリップボードにコピーされました!
After you compile your Quarkus Getting Started project, test your application with the JUnit 5 framework to ensure that it runs as expected.
Alternatively, you can enable continuous testing of your Quarkus application. For more information, see Enabling and running continuous testing.
There are two test dependencies in the pom.xml file generated by the Quarkus project :
-
quarkus-junit5: Required for testing because it provides the@QuarkusTestannotation that controls the JUnit 5 testing framework. -
rest-assured: Therest-assureddependency is not required but, because it provides a convenient way to test HTTP endpoints, it is integrated. Therest-assureddependency automatically sets the correct URL so no configuration is required.
Pom.xml file:
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. For more information, see Compiling and starting the Quarkus Getting Started project.
Procedure
Open the generated
pom.xmlfile and review the contents:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Note the values of the following properties:
-
The
java.util.logging.managersystem property is set to ensure that your application uses the correct log manager for the test. -
The
maven.homeproperty points to the location of thesettings.xmlfile, which you can store the custom Maven configuration that you want to apply to your project.
-
The
Edit the
src/test/java/org/acme/quickstart/GreetingResourceTest.javafile to match the following content:Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteBy using the
QuarkusTestrunner, you instruct JUnit to start the application before starting the tests.To run these tests from Maven, enter the following command:
./mvnw test
./mvnw testCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteYou can also run the test from your IDE. If you do this, stop the application first.
By default, tests run on port
8081, so they do not conflict with the running application. In Quarkus, theRestAssureddependency is configured to use this port.NoteIf you want to use a different client, use the
@TestHTTPResourceannotation to directly inject the URL of the tested application into a field in theTestclass. This field can be of typeString,URLorURI. You can also enter the test path in the@TestHTTPResourceannotation. For example, to test a servlet that is mapped to/myservlet, add the following lines to your test:@TestHTTPResource("/myservlet") URL testUrl;@TestHTTPResource("/myservlet") URL testUrl;Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
If necessary, specify the test port in the
quarkus.http.test-portconfiguration property.
1.8. Enabling and running continuous testing リンクのコピーリンクがクリップボードにコピーされました!
With Red Hat build of Quarkus, you can continuously test your code changes as you develop your applications. Quarkus provides a continuous testing feature, which you can run immediately after you make and save a change to the code.
When you run continuous testing, testing is paused after you start the application. You can resume the testing as soon as the application starts. The Quarkus application determines which tests run so that tests are run only on code that has changed.
The continuous testing feature of Quarkus is enabled by default. You can choose to disable continuous testing by setting the quarkus.test.continuous-testing property in the src/main/resources/application.properties file to "disabled".
If you previously disabled continuous testing, and want to enable it again, you must restart your Quarkus application before you can start testing.
Prerequisites
- Have compiled the Quarkus Getting Started application (or any other application). For more information, see Compiling and starting the Quarkus Getting Started project.
Procedure
Start your Quarkus application.
If you created your Getting Project project by using the code.quarkus.redhat.com or the Quarkus CLI, the Maven wrapper is provided when you generate the project. Enter the following command from your project directory:
./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow If you created your Getting Project project by using Apache Maven, which is installed on your machine, enter the following command:
mvn quarkus:dev
mvn quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow If you are running continuous testing in development mode and are using the Quarkus CLI, enter the following command:
quarkus dev
quarkus devCopy to Clipboard Copied! Toggle word wrap Toggle overflow
View details of the testing status in the output log that is generated by the command.
NoteTo view the output log, you might need to scroll to the bottom of the screen.
When continuous testing is enabled, the following message is displayed:
Tests paused, press [r] to resume, [h] for more options>
Tests paused, press [r] to resume, [h] for more options>Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteBy default, when continuous testing is enabled, testing is paused after you start the application. To view the list of keyboard commands that are available for controlling how you run your tests, see Commands for controlling continuous testing on Quarkus.
- To start running the tests, press `r ` on your keyboard.
View the updated output log to monitor the test status and test results, check test statistics, and get guidance for follow-up actions. For example:
Tests all passed, 2 tests were run, 0 were skipped. Tests took 1470ms. Press [r] to re-run, [v] to view full results, [p] to pause, [h] for more options>
Tests all passed, 2 tests were run, 0 were skipped. Tests took 1470ms. Press [r] to re-run, [v] to view full results, [p] to pause, [h] for more options>Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
-
Make a code change. For example, in a text editor, open the
src/main/java/GreetingsResource.javafile. Change the "hello" endpoint to return "Hello world" and save the file.
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Verify that Quarkus immediately re-runs the test to test the changed code.
View the output log to check the test results. In this example, the expected result is that the test fails because the test verifies that the changed string returns the value "hello". Therefore, the test fails by changing the string to "Hello world".
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
If you change the value back to "hello" again, the test automatically runs again.
Additional resources
1.8.1. Commands for controlling continuous testing リンクのコピーリンクがクリップボードにコピーされました!
You can use hotkey commands on your keyboard to control your options for continuous testing. To view the full list of commands, press 'h' on your keyboard. The following options are available.
| Command | Description |
|---|---|
| r | Re-run all tests. |
| f | Re-run all tests that failed. |
| b | Toggle 'broken only' mode. Only the tests that were failing previously are run, even if other tests are affected by your code changes. This option might be useful if you change code that is used by a lot of tests, but you want to only review the failed tests. |
| v | Print output detailing test failures from the last test run to the console. This option might be useful if there was a considerable amount of console output since the last test run. |
| p | Pause running tests temporarily. This might be useful if you are making a lot of code changes, but do not want to get test feedback until you finish making changes. |
| o | Print test output to the console. This is disabled by default. When test output is disabled, the output is filtered and saved, but not displayed on the console. You can view the test output on the Development UI. |
| i | Toggle instrumentation-based reload. Using this option does not directly affect testing, but does allow live reload to occur. This might be useful to avoid a restart if a change does not affect the structure of a class. |
| l | Toggle live reload. Using this option does not directly affect testing, but enables you to turn live reloading on and off. |
| s | Force restart. Using this option, you can force a scan of changed files and a live reload that includes the changes. Note that even if there are no code changes and live reload is disabled, the application still restarts. |
1.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.
The command that you use to package and run your Quarkus Getting Started application depends on the developer tool that you have installed on the machine.
Prerequisites
- You have compiled the Quarkus Getting Started project.
Procedure
-
Navigate to the
rootdirectory. To package your Quarkus Getting Started project, use one of the following methods, depending on the developer tool that you intend to use:
If you prefer to use Apache Maven, enter the following command:
mvn package
mvn packageCopy to Clipboard Copied! Toggle word wrap Toggle overflow If you prefer to use the Quarkus command-line interface (CLI), enter the following command:
quarkus build
quarkus buildCopy to Clipboard Copied! Toggle word wrap Toggle overflow If you prefer to use the Maven wrapper, use the following command:
./mvnw package
./mvnw packageCopy to Clipboard Copied! Toggle word wrap Toggle overflow This command produces the following JAR files in the
/targetdirectory:-
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 thetarget/libdirectory.
-
Enter the following command to start your application:
java -jar target/quarkus-app/quarkus-run.jar
java -jar target/quarkus-app/quarkus-run.jarCopy to Clipboard Copied! Toggle word wrap Toggle overflow Note- Before running the application, do not forget to stop the development mode (hit CTRL+C), or you will have a port conflict.
-
The
Class-Pathentry of theMANIFEST.MFfile from therunnerJAR file explicitly lists the JAR files from thelibdirectory. If you want to deploy your application from another location, you must copy therunnerJAR file as well as thelibdirectory.
1.10. Difference between JVM and native mode リンクのコピーリンクがクリップボードにコピーされました!
The following section describes the difference between a native application compiled with Mandrel or GraalVM’s native-image tool and a classic JVM application.
1.10.1. Compiling an 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 fileThese JAR files work on all operating systems and are built much faster than native images.
1.10.2. Compiling an application into a native image リンクのコピーリンクがクリップボードにコピーされました!
This is done 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
.exefile 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.Using Mandrel
Mandrel is a specialized distribution of GraalVM for Red Hat build of Quarkus and also the recommended option for building native executables that target Linux containerized environments. While the Mandrel approach is perfect for embedding the compilation output in a containerized environment, note that only a Linux64 bit native executable is provided; thus, an outcome such as
.exeis not an option.- Mandrel users are encouraged to use containers to build their native executables.
To use the official Mandrel image to compile an application into native mode using a local installation of Docker or Podman, add the following properties:
-Dquarkus.native.container-build=true -Dquarkus.native.builder-image=quay.io/quarkus/ubi-quarkus-mandrel:{MandrelVersion}-{JDK-ver-other}-Dquarkus.native.container-build=true -Dquarkus.native.builder-image=quay.io/quarkus/ubi-quarkus-mandrel:{MandrelVersion}-{JDK-ver-other}Copy to Clipboard Copied! Toggle word wrap Toggle overflow - For a detailed guide on how to build a native executable using Mandrel, see the Compiling your Quarkus applications to native executables
- The list of available Mandrel images
Using GraalVM
Because Mandrel currently does not support macOS and Windows platforms, use Oracle GraalVM for building native executables on these operation systems.
Building native executables using Oracle GraalVM directly on bare metal Linux or Windows distributions are possible. Read the GraalVM README and release notes for more details about this process.
- To build a native executable using the GraalVM, see the Compiling your Quarkus applications to native executables.
1.11. Packaging and running the Quarkus Getting Started application in native mode リンクのコピーリンクがクリップボードにコピーされました!
In native mode, the output from the application builds is a platform-dependent native binary file rather than a compress or archive JAR file. For more details about how native mode differs from the JVM, see the Difference between JVM and native mode chapter of the Getting Started guide.
Prerequisites
- Have OpenJDK 11 or 17 installed and JAVA_HOME configured appropriately.
- Apache Maven 3.8.1 or later is available.
- A working C development environment is available.
- Have a working container runtime, such as Docker or Podman.
- 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.
- Have cloned and compiled the Quarkus Getting Started project.
Have downloaded and installed a community or enterprise edition of GraalVM. To download and install a community or an enterprise edition of GraalVM, refer to the official GraalVM Getting Started documentation.
Alternatively, use platform-specific install tools like sdkman, homebrew, or scoop.
NoteWhile you can use the community edition of GraalVM to complete all of the procedures in the Getting Started guide, the community edition of GraalVM is not supported in a Red Hat build of Quarkus production environment. For more information, see Compiling your Quarkus applications to native executables.
Procedure
Configure the runtime environment by setting the
GRAALVM_HOMEenvironment variable to the GraalVM installation directory. For example:export GRAALVM_HOME=$HOME/Development/graalvm/
export GRAALVM_HOME=$HOME/Development/graalvm/Copy to Clipboard Copied! Toggle word wrap Toggle overflow On macOS, point the variable to the
Homesub-directory:export GRAALVM_HOME=$HOME/Development/graalvm/Contents/Home/
export GRAALVM_HOME=$HOME/Development/graalvm/Contents/Home/Copy to Clipboard Copied! Toggle word wrap Toggle overflow - On Windows, set your environment variables by using the Control Panel.
Install the
native-imagetool:${GRAALVM_HOME}/bin/gu install native-image${GRAALVM_HOME}/bin/gu install native-imageCopy to Clipboard Copied! Toggle word wrap Toggle overflow Set the
JAVA_HOMEenvironment variable to the GraalVM installation directory:export JAVA_HOME=${GRAALVM_HOME}export JAVA_HOME=${GRAALVM_HOME}Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the GraalVM
bindirectory to the path:export PATH=${GRAALVM_HOME}/bin:$PATHexport PATH=${GRAALVM_HOME}/bin:$PATHCopy to Clipboard Copied! Toggle word wrap Toggle overflow Navigate to the Getting Started project folder:
cd getting-started
cd getting-startedCopy to Clipboard Copied! Toggle word wrap Toggle overflow Compile a native image by using one of the following ways:
Using Maven:
mvn clean package -Pnative
mvn clean package -PnativeCopy to Clipboard Copied! Toggle word wrap Toggle overflow Using the Quarkus CLI:
quarkus build --native
quarkus build --nativeCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Start the application:
./target/getting-started-1.0.0-SNAPSHOT-runner
./target/getting-started-1.0.0-SNAPSHOT-runnerCopy to Clipboard Copied! Toggle word wrap Toggle overflow Observe the log message and verify that it contains the word
native:2022-03-04 09:51:51,505 INFO [io.quarkus] (main) getting-started 1.0.0-SNAPSHOT native (powered by Quarkus 2.7.3.Final) started in 0.043s. Listening on: http://0.0.0.0:8080
2022-03-04 09:51:51,505 INFO [io.quarkus] (main) getting-started 1.0.0-SNAPSHOT native (powered by Quarkus 2.7.3.Final) started in 0.043s. Listening on: http://0.0.0.0:8080Copy to Clipboard Copied! Toggle word wrap Toggle overflow