Quarkus extensions are one of Quarkus' best hidden-in-plain-sight features. Read on to learn how Quarkus extensions give Java superpowers and how you can get started with them.
What are Quarkus extensions?
Quarkus extensions are essentially adapter layers for Java-based libraries or technologies that enhance your application.
However, the scope of Quarkus extensions goes well beyond "just" importing dependent libraries. They can significantly increase the application's performance, help developers be more productive while developing their applications, integrate complex dependencies much easier, and simplify the application's source code.
Examples of Quarkus extensions include the Java Database Connectivity (JDBC) libraries, OpenAPI generators, Kubernetes manifest generators, and Apache Camel components. But there are many, many more.
Quarkus extensions enhance app performance
A significant advantage of using Quarkus extensions is that they integrate seamlessly into the Quarkus architecture to take advantage of its superb build time optimization phase. This way, extensions can prescribe how to load and scan your application's bytecode (including the dependencies) and configuration optimally during the build augmentation stage instead of during startup time. This allows for significantly reduced resource usage and a much faster startup time for the application's dependent libraries and technologies, just like the core Quarkus components.
Extensions also get access to the preparation phase for GraalVM native compilation so that they can leverage the advantages of natively compiled Quarkus binaries. Keep in mind that it is the extension's responsibility to make sure it is compatible with native compilation, so make sure to read its documentation. For a deeper understanding of how this works, check out the Quarkus documentation on how to write an extension.
Quarkus extensions increase developer productivity and joy
Aside from aiding the performance of your application, Quarkus extensions can give a significant boost to developer productivity and help make developing applications more enjoyable as well by not having to fiddle around with configurations, extend the Quarkus CLI, and being able to leverage Quarkus's Dev Mode.
Dev Mode
Quarkus Dev Mode is a feature that starts up the application locally (or even remotely) and provides a set of capabilities to allow developers to iterate and test their code changes quickly. Dev Mode does targeted hot reloads on the fly when you change your code. This has the benefit of not needing to do manual restarts or rebuilding the application each time you change something.
This benefit applies to extensions as well. They will only be (re-)loaded if they have been changed, added, or removed. Any dependent configuration of the extension, such as a database migration script or similar, will also have the same behavior. For example, if you include an import.sql
, the import statement will run only during Dev Mode startup or when the file gets changed.
Dev Services
Extensions can also leverage the Quarkus Dev Services capability. Dev Services provides an automated way to spin up dependent services, typically in a local container and usually using the Testcontainers project. Think of databases, Apache Kafka clusters, Keycloak, etc. Dev Services are wired into your application without further configuration, making them a powerful way to increase developer productivity because you don't have to worry about setting up complicated dependent services on your local machine.
To use Dev Services, all you have to do is add the relevant extension—e.g. jdbc-postgresql
or camel-quarkus-kafka
(Figure 1)—and when you start up Quarkus's Dev Mode (using the command quarkus dev
or mvn quarkus:dev
), the extension will start up a Dev Service automatically. Because Dev Services usually rely on containers, you will need to have a container runtime on your machine such as Podman or Docker to enjoy this feature (Figure 2).
Dev UI
When Quarkus runs in Dev Mode, it also serves a web application called the Dev UI, which, among other things, shows the extensions currently used by your application. Extensions can expose additional information and capabilities in the Dev UI. At the minimum, they show details of the extension or a link to further documentation (which can be very handy!).
Extensions can add actionable capabilities to the Dev UI as well (Figure 3). For many extensions, you can change the configuration on the fly through the Dev UI, and some extensions have purpose-built capabilities such as building a container image, deploying to Red Hat OpenShift, accessing and interacting with the Swagger UI, or even adding records to a Kafka topic running in a Dev Service. Of course, these capabilities depend on the extension and what has been implemented for it.
Extensions with a related Dev Service will also display the configuration that was automatically generated by Quarkus when the Dev Service started up. This can be very useful for developers to determine what configuration settings are needed for a particular dependency, such as the data source's type, JDBC URL, username, and password, as you can see in Figure 4.
Quarkus CLI
The quarkus
command lets you create projects, manage extensions, and do essential build and development tasks using the underlying project build tool (e.g., Maven or Gradle). Extensions can extend the functionality of the Quarkus CLI commands by virtue of Quarkus CLI plug-ins (Figure 5). This plug-in system can dynamically add commands and subcommands to the CLI. Some plug-in extensions are available out of the box, which you can retrieve with the Quarkus plug-in list-installable
command. In addition, you can also install executable jars from community extensions using their Maven coordinates.
Quarkus extensions catalog and versioning
Quarkus has an extensive catalog of official Quarkus extensions. To get an idea of the extensions that are available for Quarkus, take a look at the quarkus.io extensions catalog page.
There is no need to keep track of the versioning of any individual extension, thanks to the concept of the Quarkus platform. This concept promises that any combination of the Quarkus extensions within the platform can be used in the same application without causing conflict. This also means that the versions of the extensions are determined by the platform. In practice, this works through the use of a Quarkus platform BOM artifact, which is imported as a dependency on your project.
Thanks to this Quarkus platform concept, you don't have to explicitly define the version of each Quarkus extension (or the libraries they are acting as a wrapper for) in your pom.xml
or build.gradle
file, and when you upgrade your Quarkus version, your extension versions are also automatically updated. Bear in mind that the extensions catalog might also contain extensions that are not necessarily production-ready, so it's always a good practice to read up on the extension's documentation before using it.
The Quarkiverse
The Quarkiverse GitHub organization provides hosting for Quarkus extension projects—not only for the ones that are featured in the main Quarkus extensions catalog, but also for additional extensions provided by Quarkus community developers. These community extensions are fully maintained by the extension's independent development teams and are typically kept up to date so that they are compatible with the different Quarkus versions. However, there are no explicit guarantees, so ensuring the project is active and up-to-date before using them is essential.
How to add extensions to your Quarkus project
You can add a Quarkus extension in a few ways:
- If you're generating a new project using code.quarkus.io or the supported Red Hat build of Quarkus, code.quarkus.redhat.com, you can select the extensions from the list and they will automatically be added to the generated code starter.
- Using the Quarkus CLI, you can add extensions using the quarkus extension add command. For example, to add the OpenShift client extension to your project, you would type
quarkus extension add quarkus-openshift-client
. (Quarkus will automatically resolve it toquarkus.io:quarkus-openshift-client
). - If you're developing with VS Code or IntelliJ IDEA, you can also use the Quarkus plug-in to add extensions to your project, as illustrated in Figure 6.
Go forth and extend your Quarkus application!
This was a brief introduction to Quarkus extensions. If you want to learn more about Quarkus, check out the Quarkus overview on Red Hat Developer, or get started with Quarkus tutorials on developers.redhat.com/learn. If you want to learn even more about how Quarkus extensions work, or perhaps how to build your own extension, you can find more information on quarkus.io.
Last updated: September 19, 2023