Basics and Fundamentals

Creating a Quarkus Application

  • Maven

  • Quarkus CLI

mvn "io.quarkus:quarkus-maven-plugin:create" \
  -DprojectGroupId="com.redhat.developers" \
  -DprojectArtifactId="tutorial-app" \
  -DprojectVersion="1.0-SNAPSHOT" \
  -DclassName="GreetingResource" \
  -Dpath="hello" \
  -Dextensions="resteasy"
cd tutorial-app

The quarkus command lets you create projects, manage extensions and do essential build and dev commands using the underlying project build tool.

Follow these instructions to install Quarkus CLI: https://quarkus.io/guides/cli-tooling

Then create the application:

quarkus create app \
  -x resteasy \
  com.redhat.developers:tutorial-app:1.0-SNAPSHOT
cd tutorial-app
All the remaining parts of this tutorial assume that you’ll be working inside the project folder that was just created. In this case, tutorial-app.

Build and Run the Quarkus Application

  • JVM mode

  • Native mode

  • Native container-build mode

Maven Quarkus CLI
 mvn package
quarkus build

To run the application in jvm mode

java -jar target/quarkus-app/quarkus-run.jar
Maven Quarkus CLI
 mvn package -Dnative
quarkus build --native
Native compilation will take few minutes to complete.

To run the application in native mode

./target/tutorial-app-1.0-SNAPSHOT-runner
Maven Quarkus CLI
 mvn quarkus:image-build \
  -Dnative
quarkus image build --native
Native compilation will take few minutes to complete.

The above command built a native binary of the Java code and also built it into a container image. To run the application (in native mode) you will need to start up a container:

docker run -it --rm -p 8080:8080 tutorial-app:1.0-SNAPSHOT

You’ll see an output like this one:

__  ____  __  _____   ___  __ ____  ______
 --/ __ \/ / / / _ | / _ \/ //_/ / / / __/
 -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
2020-05-10 22:37:10,161 INFO  [io.quarkus] (main) tutorial-app 1.0-SNAPSHOT (powered by Quarkus 1.10.2.Final) started in 0.016s. Listening on: http://0.0.0.0:8080
2020-05-10 22:37:10,162 INFO  [io.quarkus] (main) Profile prod activated.
2020-05-10 22:37:10,162 INFO  [io.quarkus] (main) Installed features: [cdi, resteasy]

Notice how fast Quarkus was ready to serve your requests. In this particular example Quarkus only required 0.016s to start.

You can open your browser with the url http://localhost:8080/hello and you should see a response like hello.

Or else if you’re a CLI person, you can run a curl command like:

curl localhost:8080/hello
hello

Stopping the application

You can stop your running application by issuing a CTRL+C in your terminal.

^C2020-05-10 23:11:03,419 INFO  [io.quarkus] (main) tutorial-app stopped in 0.007s

Quarkus was able to stop in just 0.007s. Fast to start, fast to stop. Fast always!

Testing your application

The scaffolding process also creates a test case for the example endpoint.

Check the file GreetingResourceTest.java in the folder src/test/java to see what a Quarkus integration test looks like.

When running this test, the application is started once, then all tests are executed, and finally, the application stops. Although it is not mandatory, by default the RestAssured project is used to test Rest endpoints but it is up to you to change that.

You can run the test in your IDE or by running the following:

  • Maven

  • Quarkus CLI

mvn clean test
quarkus test
quarkus test starts tests in continuous testing mode (more about this below) so to return to the command line you will need to press (q)

Open the Project in your IDE

Open or import the generated project into your IDE.

If you are using vscode you can open the project with:

code .
If you use tutorial-tools, your host quarkus-tutorial/work contains the application code; point your IDE to this directory.

Live Coding (Development mode)

Stop the Quarkus application that might be running in jvm or native mode.

Live Coding or Development mode enables hot deployment with background compilation, which means that when you modify your Java files and/or your resource files and refresh your browser, these changes will automatically take effect.

This is probably one of the best features of Quarkus: enabling a very fast and productive iterative feedback loop.

Let’s start the Live Coding mode by invoking mvn. You probably won’t need to stop/start Quarkus again during this tutorial:

  • Maven

  • Quarkus CLI

mvn quarkus:dev
quarkus dev

Dev UI

Quarkus ships with a Dev UI, which is available in dev mode at /q/dev by default. It allows you to quickly visualize all the extensions currently loaded, see their status and go directly to their documentation.

Dev UI

Continuous Testing (Development mode)

Quarkus supports continuous testing, meaning that your tests will run immediately after code changes were saved. Let’s start again in Live Coding mode by invoking quarkus dev mode:

  • Maven

  • Quarkus CLI

mvn quarkus:dev
quarkus dev

Quarkus will start in development mode as normal, but down the bottom of the screen you should see the following:

Tests paused
Press [r] to resume testing, [o] Toggle test output, [h] for more options>

Press r and the tests will start running. You should see the status change down the bottom of the screen as they are running, and it should finish similar to this:

All 1 test is passing (0 skipped), 1 test was run in 11705ms. Tests completed at 16:02:32.
If you don’t want to have continuous testing enabled, you can change this by adding quarkus.test.continuous-testing=disabled in application.properties.

You can find more details about controlling continuous testing in this guide.