Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Configuration and profiles

Configuration means values that should not be hard-coded: the blog title, the number of posts per page, the database address. They may differ on a developer's machine and on a server. In Quarkus all settings live in one file, src/main/resources/application.properties, and are read through the MicroProfile Config standard.

The configuration file

The generator has already created an empty file. Add your keys to it:

# src/main/resources/application.properties
blog.title=Anna's blog
blog.page-size=10

The format is simple: key=value, one setting per line. Your own keys usually share a prefix, here blog.. Quarkus's own keys start with quarkus., for example quarkus.http.port=8080.

Reading a setting in code

A value is injected into a bean field with @ConfigProperty. Let's create a resource with information about the blog:

// src/main/java/com/example/blog/InfoResource.java
package com.example.blog;
 
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import org.eclipse.microprofile.config.inject.ConfigProperty;
 
@Path("/info")
public class InfoResource {
 
    @ConfigProperty(name = "blog.title")
    String title;
 
    @ConfigProperty(name = "blog.page-size", defaultValue = "20")
    int pageSize;
 
    @GET
    public String info() {
        return title + ", " + pageSize + " posts per page";
    }
}

Quarkus converts the text from the file into the right type itself: int, boolean, Duration and others. defaultValue is used when the key is missing from the file.

curl http://localhost:8080/info

The answer is Anna's blog, 10 posts per page.

Profiles

A profile is the mode the application runs in. There are three:

ProfileWhen it is active
dev./mvnw quarkus:dev
testWhile tests run
prodWhen the built application runs

To make a value apply in one profile only, write %profile-name. before the key:

# src/main/resources/application.properties
blog.title=Anna's blog
blog.page-size=10
%dev.blog.title=Anna's blog (dev)

In dev mode /info returns Anna's blog (dev), 10 posts per page, and the built application returns Anna's blog, .... This is how different databases for development and for the server are usually configured.

Environment variables

Any setting can be overridden with an environment variable without touching the file. The variable name is the key in upper case with dots and dashes replaced by _: blog.title becomes BLOG_TITLE, quarkus.http.port becomes QUARKUS_HTTP_PORT.

BLOG_TITLE="Oleg's blog" java -jar target/quarkus-app/quarkus-run.jar

Now /info answers Oleg's blog, .... Environment variables win over application.properties, so passwords and server addresses are set this way, while the file keeps safe values for development.

Common mistake

If a key is neither in the file nor in the environment and has no defaultValue, the application does not start. For example, for a field with @ConfigProperty(name = "blog.owner"):

ConfigurationException: Failed to load config value of type class java.lang.String for: blog.owner
...
SRCFG00014: The config property blog.owner is required but it could not be found in any config source

This is a safeguard: it is better to fail at startup than to run with an empty value. Add the key to the file, set a defaultValue, or use the Optional<String> type for an optional setting.

Thanks for reading!

Donate to the site — we work hard for you.