Getting started with Quarkus profile

Luiz Gustavo De O. Costa
5 min readAug 15, 2021

To prevent working with production resources in a local/dev environment the use of profiles is highly important, in this article let’s see how Quarkus helps us to achieve the environment configuration.

1. Definition

  • Profile → Provide a way to segregate parts of your application configuration and make it only available in certain environments [Spring].
  • Environment → An environment is a user-defined collection of resources that hosts an application. An environment is the application’s mechanism for bringing together components with the agent that deploys them. Environments are typically modeled on some stage of the software project lifecycle, such as development, QA, or production. [IBM]

2. README 📕

Before reading the next section, please read this section.

From the left ⬅️ is the highest and the right ➡️ the lowest, ie, if you have the configuration into your .env the configuration inside your application.properties will take the .env values.

Priority — from Quarkus site

3. Benefits

These are some benefits:

  • Don’t use production data to test your application,
  • Change the database to in-memory (if applicable)
  • Isole application parts and apply to get the best result in each environment

4. How

In this section will be shown ways to work with profile, let’s dive in.

4.1. Project structure

The intent of this project is to show how to work with profiles, because we’ve only one resource (GreetingResource).

Let’s check the items

1 — GreetingResource, returns a message when the endpoint http://localhost:<port>/hello is called.

2 — Message, entity JPA used for staging profile

3 — index.html, default html page that contains the endpoint link.

4— web.xml, deployment descriptor that contains the h2 console configuration

5 — application.yml, where the runtime parameters are set.

6 — application-staging.yml, specific runtime configuration for staging profile.

7 — .env, general runtime configuration.

Project structure

4.1.1. .env

Using an .env file add the desired configuration, below there is an example.

.env file

4.1.2. application.yml

In this file, the following profiles: default (line 1), dev (line 6), test (line 14), demo (line 20), and prod (line 26).

As the file has few configurations it is easy to handle any change, otherwise the best approach is create one file for each environment. Let’s check how to do it in the next section.

application.yml

4.1.3. application-”profile”.yml

Another way to work with profiles is to have one file for each environment. This is a good practice to avoid changing values and impact some environment, such as, accessing an important environment, either the demo or prod 🙈.

The example below shows a new environment, the staging. A new one, to show the flexibility to work using profiles.

application-staging.yml

5. Hands on

To run the application, since it is a Quarkus application, using the console type the command mvn quarkus:dev and the magic happens 😉.

In order to access the endpoint, visit http://localhost:<port>/hello

Note! quarkus:dev is different to choose a profile.

mvn quarkus:dev
.....
Listening for transport dt_socket at address: 5005
__ ____ __ _____ ___ __ ____ ______
--/ __ \/ / / / _ | / _ \/ //_/ / / / __/
-/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
2021-08-15 13:12:43,762 INFO [io.quarkus] (Quarkus Main Thread) profile 0.0.1-SNAPSHOT on JVM (powered by Quarkus 2.1.1.Final) started in 2.346s. Listening on: http://localhost:8181
2021-08-15 13:12:43,767 INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2021-08-15 13:12:43,768 INFO [io.quarkus] (Quarkus Main Thread) Installed features: [agroal, cdi, config-yaml, hibernate-orm, jdbc-h2, narayana-jta, resteasy, resteasy-jackson, smallrye-context-propagation]

In order to change the profile the instruction also should be changed and the -Dquarkus.profile="profile name" should be provided.

mvn quarkus:dev -Dquarkus.profile=test....
__ ____ __ _____ ___ __ ____ ______
--/ __ \/ / / / _ | / _ \/ //_/ / / / __/
-/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
2021-08-15 13:14:34,832 INFO [io.quarkus] (Quarkus Main Thread) profile 0.0.1-SNAPSHOT on JVM (powered by Quarkus 2.1.1.Final) started in 2.286s. Listening on: http://localhost:9090
2021-08-15 13:14:34,838 INFO [io.quarkus] (Quarkus Main Thread) Profile test activated. Live Coding activated.
2021-08-15 13:14:34,839 INFO [io.quarkus] (Quarkus Main Thread) Installed features: [agroal, cdi, config-yaml, hibernate-orm, jdbc-h2, narayana-jta, resteasy, resteasy-jackson, smallrye-context-propagation]

Now it’s time to use a profile that is another application file, the staging profile. Using the staging profile, and copying the h2 url we can see the message table created.

mvn quarkus:dev -Dquarkus.profile=staging
...
__ ____ __ _____ ___ __ ____ ______
--/ __ \/ / / / _ | / _ \/ //_/ / / / __/
-/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
2021-08-15 20:23:17,077 INFO [io.quarkus] (Quarkus Main Thread) profile 0.0.1-SNAPSHOT on JVM (powered by Quarkus 2.1.1.Final) started in 4.106s. Listening on: http://localhost:9090
2021-08-15 20:23:17,084 INFO [io.quarkus] (Quarkus Main Thread) Profile staging activated. Live Coding activated.
2021-08-15 20:23:17,085 INFO [io.quarkus] (Quarkus Main Thread) Installed features: [agroal, cdi, config-yaml, hibernate-orm, jdbc-h2, narayana-jta, resteasy, servlet, smallrye-context-propagation, vertx]
Message from endpoint /hello

And now let’s access the h2 console using http://localhost:9090/h2. The web.xml enables the h2 url.

h2 console

Now the message is created, as declared into application-staging.yml file

h2 tables

6.1. Multiple profiles

Not supported, only a single profile may be active at a time.

7. Code

The complete code is available in my GitHub.

8. Conclusion

Using the Quarkus profile is possible to take advantage of configuration. The only point to me in comparison with Spring is not allowing multiple profiles.

The configuration is simple and direct 👍.

References

--

--

Luiz Gustavo De O. Costa

Hey friend!! I’m Luiz Gustavo, a Java developer and I’m here to learn and write about Java, tests and good practices