Speed up development and save time with Live Reload and Testing in Quarkus 2.0

David
Geek Culture
6 min readJun 29, 2021

--

Quarkus comes with a toolchain enabling developers rapid start their development with its extensions to all major IDEs. One of its prominent and ease of use features is its ability to perform live reload in development mode, offering the developer to change implementation on the fly without restarting. With the new version 2.0, developers can do more. There is now an interactive shell in development mode offering a set of different options for more granular control of what to run in live mode. Quarkus also comes with a CLI binary which enables further rapid development and project setup.

Quarkus 2.0 has just seen the light of dawn and presents a lot of improvements in different areas.

Live reload (Development mode)

Quarkus offers since previously a ’development mode’ which is a hot redeploy mechanism through which code changes in an application will be recompiled and reloaded when refreshed in the browser. Apart from extensively being used locally, one can use on remote containers as well, providing a remote development mode.

$ ./mvnw quarkus:dev

With version 2.0 as presented above, we are offered several new options. When pressing [r] the tests are run in the suite with the current development loaded classes.

All 1 tests are passing (0 skipped), 1 tests were run in 1547ms. Tests completed at 22:05:53.
Press [r] to re-run, [v] to view full results, [p] to pause, [h] for more options>

Pressing [h] the developer is offered the following options to either rerun the suite with just the failing tests or continue the debugging from previous failure if there are any.

The following commands are available:
[r] — Re-run all tests
[f] — Re-run failed tests
[b] — Toggle ‘broken only’ mode, where only failing tests are run (disabled)
[v] — Print failures from the last test run
[o] — Toggle test output (disabled)
[p] — Pause tests
[i] — Toggle instrumentation based reload (disabled)
[l] — Toggle live reload (enabled)
[s] — Force live reload scan
[h] — Display this help
[q] — Quit

If we are building an application we that is reliant on a database, one might think of setting up a database programmatically for the tests. However, there is a seamless way to have Quarkus provide a database layer for you by applying a so called zero configuration setup.

Zero Config Setup (DevServices)

When testing or running in development mode Quarkus can provide you with a zero config database out of the box, a feature they refer to as DevServices. Depending on the database type you may need docker installed in order to use this feature. DevServices is supported for the following open source databases:

  • Postgresql (container)
  • MySQL (container)
  • MariaDB (container)
  • H2 (in-process)
  • Apache Derby (in-process)
  • DB2 (container) (requires license acceptance)
  • MSSQL (container) (requires license acceptance)

If you want to use DevServices then all you need to do is include the relevant extension for the type of database you want (either reactive or JDBC, or both), and don’t configure a database URL, username and password, Quarkus will provide the database and you can just start coding without worrying about config. This way you can have a local db running and there is no need to clutter it or vice versa which gives the developer the test isolation. This essentially uses TestContainer as a framework to allow developer to rapidly test their application without focusing to much on the database layer setup and more on the business logic of things.

Quarkus CLI

With version 2.0 the Quarkus CLI, lets you create projects, manage extensions and do essential build and development commands using the underlying project’s build tool.

curl -Ls https://sh.jbang.dev | bash -s - app install --fresh --force quarkus@quarkusio

With the CLI we can do the following command.

$ quarkus --version
Client Version {quarkus-version}

Creating a Quarkus Project

$ quarkus create
-----------

applying codestarts...
📚 java
🔨 maven
📦 quarkus
📝 config-properties
🔧 dockerfiles
🔧 maven-wrapper
🚀 resteasy-codestart

-----------
[SUCCESS] ✅ quarkus project has been successfully generated in:
--> /<output-dir>/code-with-quarkus

We can also apply the revelant artifact and project configuration.

$ quarkus create app --group-id com.foo --artifact-id bar --version 1.0
version 1.0
-----------

applying codestarts...
📚 java
🔨 maven
📦 quarkus
📝 config-properties
🔧 dockerfiles
🔧 maven-wrapper
🚀 resteasy-codestart

-----------
[SUCCESS] ✅ quarkus project has been successfully generated in:
--> /<output-dir>/bar
-----------

From here on we can start adding and removing our extensions depending on the nature of our application. Firstly by listing the ones we already have.

Listing extension(s)

Listing the installed extensions in the project.

$ quarkus ext ls

Search extension(s)

Use the --installable or -i option to list extensions that can be installed from the Quarkus platform the project is using.

The searach can be narrowed or filtered using search (--search or -s).

$ quarkus ext list --concise -i -s jdbcJDBC Driver - DB2                                  quarkus-jdbc-db2
JDBC Driver - PostgreSQL quarkus-jdbc-postgresql
JDBC Driver - H2 quarkus-jdbc-h2
JDBC Driver - MariaDB quarkus-jdbc-mariadb
JDBC Driver - Microsoft SQL Server quarkus-jdbc-mssql
JDBC Driver - MySQL quarkus-jdbc-mysql
JDBC Driver - Oracle quarkus-jdbc-oracle
JDBC Driver - Derby quarkus-jdbc-derby
Elytron Security JDBC quarkus-elytron-security-jdbc
Agroal - Database connection pool quarkus-agroal

Adding extension(s)

The Quarkus CLI can add Quarkus one or more extensions to your project with the ‘add’ command:

$ quarkus ext add kubernetes health
[SUCCESS] ✅ Extension io.quarkus:quarkus-kubernetes has been installed
[SUCCESS] ✅ Extension io.quarkus:quarkus-smallrye-health has been installed

Removing extension(s)

The Quarkus CLI can remove one or more extensions from your project with the ‘remove’ command:

$ quarkus ext rm kubernetes
[SUCCESS] ✅ Extension io.quarkus:quarkus-kubernetes has been uninstalled

Build your project

To build your project using the Quarkus CLI (using the default configuration in this example):

$ quarkus build

Entering Development Mode with CLI

$ quarkus dev --help

To start dev mode from the Quarkus CLI do:

$ quarkus dev         

[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< org.acme:code-with-quarkus >---------------------
[INFO] Building code-with-quarkus 1.0.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
...
Listening for transport dt_socket at address: 5005
__ ____ __ _____ ___ __ ____ ______
--/ __ \/ / / / _ | / _ \/ //_/ / / / __/
-/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
2021-05-27 10:15:56,032 INFO [io.quarkus] (Quarkus Main Thread) code-with-quarkus 1.0.0-SNAPSHOT on JVM (powered by Quarkus 999-SNAPSHOT) started in 1.387s. Listening on: http://localhost:8080
2021-05-27 10:15:56,035 INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2021-05-27 10:15:56,035 INFO [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy, smallrye-context-propagation]
--
Tests paused, press [r] to resume

Having these build tools and features enables developers to focus more on application side of things without any cumbersome process of setting up the project skeleton or databases. If one prefers the online wizard way, they can always refer to this link where the project can be bootstrapped to your GitHub account.

Good luck!

--

--