Configuring your Quarkus applications
Abstract
Preface Copy linkLink copied to clipboard!
As an application developer, you can use Red Hat build of Quarkus to create microservices-based applications written in Java that run on OpenShift and serverless environments. Applications compiled to native executables have small memory footprints and fast startup times.
This guide describes how to configure a Quarkus application using the Eclipse MicroProfile Config method or YAML format. The procedures include configuration examples created using the Quarkus config-quickstart exercise.
Prerequisites
Have OpenJDK (JDK) 11 installed and the
JAVA_HOMEenvironment variable specifies the location of the Java SDK.- Log in the Red Hat Customer Portal to download the Red Hat build of Open JDK from the Software Downloads page.
Have Apache Maven 3.8.1 or higher installed.
- Download Maven from the Apache Maven Project website.
Have Maven configured to use artifacts from the Quarkus Maven repository.
- To learn how to configure Maven settings see Getting started with Quarkus.
Providing feedback on Red Hat documentation Copy linkLink copied to clipboard!
We appreciate your feedback on our technical content and encourage you to tell us what you think. If you’d like to add comments, provide insights, correct a typo, or even ask a question, you can do so directly in the documentation.
You must have a Red Hat account and be logged in to the customer portal.
To submit documentation feedback from the customer portal, do the following:
- Select the Multi-page HTML format.
- Click the Feedback button at the top-right of the document.
- Highlight the section of text where you want to provide feedback.
- Click the Add Feedback dialog next to your highlighted text.
- Enter your feedback in the text box on the right of the page and then click Submit.
We automatically create a tracking issue each time you submit feedback. Open the link that is displayed after you click Submit and start watching the issue or add more comments.
Thank you for the valuable feedback.
Making open source more inclusive Copy linkLink copied to clipboard!
Red Hat is committed to replacing problematic language in our code, documentation, and web properties. We are beginning with these four terms: master, slave, blacklist, and whitelist. Because of the enormity of this endeavor, these changes will be implemented gradually over several upcoming releases. For more details, see our CTO Chris Wright’s message.
Chapter 1. Red Hat build of Quarkus configuration options Copy linkLink copied to clipboard!
Configuration options enable you to change the settings of your application in a single configuration file. Quarkus supports configuration profiles that let you group related properties and switch between profiles as required.
You can use the MicroProfile Config specification from the Eclipse MicroProfile project to inject configuration properties into your application and configure them using a method defined in your code. By default, Quarkus reads properties from the application.properties file located in the src/main/resources directory.
By adding the config-yaml dependency to your project pom.xml file you can add your application properties in the application.yaml file using the YAML format.
Quarkus can also read application properties from different sources, such as the file system, database, or any source that can be loaded by a Java application.
Chapter 2. Creating the configuration quickstart project Copy linkLink copied to clipboard!
The config-quickstart project lets you get up and running with a simple Quarkus application using Apache Maven and the Quarkus Maven plug-in. The following procedure demonstrates how you can create a Quarkus Maven project.
Procedure
Verify that Maven is using JDK 11 and that the Maven version is 3.8.1 or higher:
mvn --version
mvn --versionCopy to Clipboard Copied! Toggle word wrap Toggle overflow If this command does not return JDK 11, ensure that the directory where JDK 11 is installed on your system is included in the
PATHenvironment variable:export PATH=$PATH:/path/to/jdk-11
export PATH=$PATH:/path/to/jdk-11Copy to Clipboard Copied! Toggle word wrap Toggle overflow Enter the following command to generate the project:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This command creates the following items in the
config-quickstartdirectory:- The Maven project directory structure
-
An
org.acme.config.GreetingResourceresource -
A landing page that you can access at
http://localhost:8080after you start the application - Associated unit tests for testing your application in native mode and JVM mode
-
Example
Dockerfile.jvm,Dockerfile.nativeandDockerfile.fast-jarfiles insrc/main/docker The application configuration file
NoteAlternatively, you can download a Quarkus Maven project to use in this tutorial from the Quarkus quickstart archive or clone the
Quarkus QuickstartsGit repository. The exercise is located in theconfig-quickstartdirectory.
Chapter 3. Generating an example configuration file for your Quarkus application Copy linkLink copied to clipboard!
You can create an application.properties.example file with all of the available configuration values and documentation for the extensions that your application is configured to use. You can repeat this procedure after you install a new extension to see what additional configuration options have been added.
Prerequisites
- Have a Quarkus Maven project.
Procedure
To create an
application.properties.examplefile, enter the following command:./mvnw quarkus:generate-config
./mvnw quarkus:generate-configCopy to Clipboard Copied! Toggle word wrap Toggle overflow This command creates the
application.properties.examplefile in thesrc/main/resources/directory. The file contains all of the configuration options exposed through the extensions that you installed. These options are commented out and have a default value where applicable.The following example shows the HTTP port configuration entry from the
application.properties.examplefile:#quarkus.http.port=8080
#quarkus.http.port=8080Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Chapter 4. Injecting configuration values into your Quarkus application Copy linkLink copied to clipboard!
Red Hat build of Quarkus uses the MicroProfile Config feature to inject configuration data into the application. You can access the configuration through context and dependency injection (CDI) or by using a method defined in your code.
You can use the @ConfigProperty annotation to map an object property to a key in the MicroProfile ConfigSources file of your application. This procedure shows you how to inject an individual property configuration into a Quarkus config-quickstart project.
Prerequisites
-
You have created the Quarkus
config-quickstartproject.
Procedure
-
Open the
src/main/resources/application.propertiesfile. Add configuration properties to your configuration file where
<key>is the property name and<value>is the value of the property:<key>=<value>
<key>=<value>Copy to Clipboard Copied! Toggle word wrap Toggle overflow The following example shows how to set the values for the
greeting.messageand thegreeting.nameproperties in the Quarkusconfig-quickstartproject:src/main/resources/application.properties
greeting.message = hello greeting.name = quarkus
greeting.message = hello greeting.name = quarkusCopy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantUse
quarkusas a prefix to Quarkus properties.Review the
GreetingResource.javafile and make sure it includes the following import statements:import org.eclipse.microprofile.config.inject.ConfigProperty; import java.util.Optional;
import org.eclipse.microprofile.config.inject.ConfigProperty; import java.util.Optional;Copy to Clipboard Copied! Toggle word wrap Toggle overflow Define the corresponding properties by annotating them with
@ConfigPropertyas shown in the following example:src/main/java/org/acme/config/GreetingResource.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- If you do not provide a value for this property, the application will fail and throw the following exception message:
javax.enterprise.inject.spi.DeploymentException: No config value of type [class java.lang.String] exists for: greeting.message - 2
- If you do not provide a value for the
greeting.suffix, Quarkus resolves it to the default value. - 3
- If the
Optionalparameter does not have a value, it returns no value forgreeting.name.
NoteTo inject a configured value, you can use
@ConfigProperty. The@Injectannotation is not necessary for members annotated with@ConfigProperty.Edit your
hellomethod to return the following message:src/main/java/org/acme/config/GreetingResource.java
@GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return message + " " + name.orElse("world") + suffix; }@GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return message + " " + name.orElse("world") + suffix; }Copy to Clipboard Copied! Toggle word wrap Toggle overflow Compile and start your application in development mode:
./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enter the following command in a new terminal window to verify that the endpoint returns the message:
curl http://localhost:8080/greeting
curl http://localhost:8080/greetingCopy to Clipboard Copied! Toggle word wrap Toggle overflow This command returns the following output:
hello quarkus!
hello quarkus!Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Press CTRL+C to stop the application.
4.1. Annotating a class with @ConfigProperties Copy linkLink copied to clipboard!
As an alternative to injecting multiple related configuration values individually, you can use the @io.quarkus.arc.config.ConfigProperties annotation to group configuration properties. The following procedure demonstrates the use of @ConfigProperties annotation on the Quarkus config-quickstart project.
Prerequisites
-
You have created the Quarkus
config-quickstartproject.
Procedure
Review the
GreetingResource.javafile and make sure it includes the following import statements:src/main/java/org/acme/config/GreetingResource.java
import java.util.Optional; import javax.inject.Inject;
import java.util.Optional; import javax.inject.Inject;Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Create a file
GreetingConfiguration.javain thesrc/main/java/org/acme/configdirectory. Add the
ConfigPropertiesandOptionalimports to theGreetingConfiguration.javafile:src/main/java/org/acme/config/GreetingConfiguration.java
import io.quarkus.arc.config.ConfigProperties; import java.util.Optional; import javax.inject.Inject;
import io.quarkus.arc.config.ConfigProperties; import java.util.Optional; import javax.inject.Inject;Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
GreetingConfigurationclass for thegreetingproperties in yourGreetingConfiguration.javafile:src/main/java/org/acme/config/GreetingConfiguration.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Inject the
GreetingConfigurationclass into theGreetingResourceclass using the@Injectannotation:src/main/java/org/acme/config/GreetingResource.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Compile and start your application in development mode:
./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantIf you do not provide values for the class properties, the application fails to compile and you receive a
javax.enterprise.inject.spi.DeploymentExceptionthat indicates a missing value. This does not apply toOptionalfields and fields with a default value.Enter the following command in a new terminal window to verify that the endpoint returns the message:
curl http://localhost:8080/greeting
curl http://localhost:8080/greetingCopy to Clipboard Copied! Toggle word wrap Toggle overflow You receive the following message:
hello quarkus!
hello quarkus!Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Press CTRL+C to stop the application.
4.2. Using nested object configuration Copy linkLink copied to clipboard!
You can define a nested class inside an existing class. This procedure demonstrates how to create a nested class configuration in the Quarkus config-quickstart project.
Prerequisites
-
You have created the Quarkus
config-quickstartproject.
Procedure
Review the
GreetingConfiguration.javafile and make sure it includes the following import statements:src/main/java/org/acme/config/GreetingConfiguration.java
import io.quarkus.arc.config.ConfigProperties; import java.util.Optional; import java.util.List;
import io.quarkus.arc.config.ConfigProperties; import java.util.Optional; import java.util.List;Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add the configuration in your
GreetingConfiguration.javafile using the@ConfigPropertiesannotation.The following example shows the configuration of the
GreetingConfigurationclass and its properties:src/main/java/org/acme/config/GreetingConfiguration.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add a nested class inside the
GreetingConfigurationclass as shown in the following example:src/main/java/org/acme/config/GreetingConfiguration.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This example shows a nested class
ContentConfig. The name of the field, in this casecontent, determines the name of the properties bound to the object.Set the
greeting.content.prize-amountandgreeting.content.recipientsconfiguration properties in yourapplication.propertiesfile.The following example shows the values of properties for the
GreetingConfigurationandContentConfigclasses:src/main/resources/application.properties
greeting.message = hello greeting.name = quarkus greeting.content.prize-amount=10 greeting.content.recipients=Jane,John
greeting.message = hello greeting.name = quarkus greeting.content.prize-amount=10 greeting.content.recipients=Jane,JohnCopy to Clipboard Copied! Toggle word wrap Toggle overflow Inject the
GreetingConfigurationclass into theGreetingResourceclass using the@Injectannotation, and update the message string that the/greetingendpoint returns to have the message show the values that you set for the newgreeting.content.prize-amountandgreeting.content.recipientsproperties that you added:src/main/java/org/acme/config/GreetingResource.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Compile and start your application in development mode:
./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantIf you do not provide values for the class properties, the application fails to compile and you receive a
javax.enterprise.inject.spi.DeploymentExceptionthat indicates a missing value. This does not apply toOptionalfields and fields with a default value.Enter the following command in a new terminal window to verify that the endpoint returns the message:
curl http://localhost:8080/greeting
curl http://localhost:8080/greetingCopy to Clipboard Copied! Toggle word wrap Toggle overflow You receive the message that contains the greeting on the first line and the recipients of the prize and the prize amount on the second line:
hello quarkus! Jane,John receive total of candies: 10
hello quarkus! Jane,John receive total of candies: 10Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Press CTRL+C to stop the application.
Classes annotated with @ConfigProperties can be annotated with Bean Validation annotations similar to the following example:
Your project must include the quarkus-hibernate-validator dependency.
4.3. Annotating an interface with @ConfigProperties Copy linkLink copied to clipboard!
An alternative method for managing properties is to define them as an interface. If you annotate an interface with @ConfigProperties, the interface can extend other interfaces, and you can use methods from the entire interface hierarchy to bind properties.
This procedure shows an implementation of the GreetingConfiguration class as an interface in the Quarkus config-quickstart project.
Prerequisites
-
You have created the Quarkus
config-quickstartproject.
Procedure
Review the
GreetingConfiguration.javafile and make sure it includes the following import statements:src/main/java/org/acme/config/GreetingConfiguration.java
import io.quarkus.arc.config.ConfigProperties; import org.eclipse.microprofile.config.inject.ConfigProperty; import java.util.Optional;
import io.quarkus.arc.config.ConfigProperties; import org.eclipse.microprofile.config.inject.ConfigProperty; import java.util.Optional;Copy to Clipboard Copied! Toggle word wrap Toggle overflow Add a
GreetingConfigurationclass as an interface to yourGreetingConfiguration.javafile:src/main/java/org/acme/config/GreetingConfiguration.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- You must set the
@ConfigPropertyannotation because the name of the configuration property does not follow the getter method naming conventions. - 2
- In this example,
namewas not set so the corresponding property will begreeting.suffix. - 3
- You do not need to specify the
@ConfigPropertyannotation because the method name follows the getter method naming conventions (greeting.namebeing the corresponding property) and no default value is needed.
Inject the
GreetingConfigurationclass into theGreetingResourceclass using the@Injectannotation:src/main/java/org/acme/config/GreetingResource.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Compile and start your application in development mode:
./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow ImportantIf you do not provide values for the class properties, the application fails to compile and you receive a
javax.enterprise.inject.spi.DeploymentExceptionthat indicates a missing value. This does not apply toOptionalfields and fields with a default value.Enter the following command in a new terminal window to verify that the endpoint returns the message:
curl http://localhost:8080/greeting
curl http://localhost:8080/greetingCopy to Clipboard Copied! Toggle word wrap Toggle overflow You receive the following message:
hello quarkus!
hello quarkus!Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Press CTRL+C to stop the application.
Chapter 5. Accessing the configuration from code Copy linkLink copied to clipboard!
You can access the configuration by using a method defined in your code. You can achieve dynamic lookups or retrieve configured values from classes that are either CDI beans or JAX-RS resources.
You can access the configuration using the org.eclipse.microprofile.config.ConfigProvider.getConfig() method. The getValue method of the Config object returns the values of the configuration properties.
Prerequisites
- You have a Quarkus Maven project.
Procedure
Access the configuration using one of the following options:
To access a configuration of a property that is defined already in your
application.propertiesfile, use the following syntax whereDATABASE.NAMEis the name of a property that is assigned to adatabaseNamevariable:String databaseName = ConfigProvider.getConfig().getValue("DATABASE.NAME", String.class);String databaseName = ConfigProvider.getConfig().getValue("DATABASE.NAME", String.class);Copy to Clipboard Copied! Toggle word wrap Toggle overflow To access a configuration of a property that might not be defined in your
application.propertiesfile, use the following syntax:Optional<String> maybeDatabaseName = ConfigProvider.getConfig().getOptionalValue("DATABASE.NAME", String.class);Optional<String> maybeDatabaseName = ConfigProvider.getConfig().getOptionalValue("DATABASE.NAME", String.class);Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Chapter 6. Setting configuration properties Copy linkLink copied to clipboard!
By default, Quarkus reads properties from the application.properties file located in the src/main/resources directory. If you change build properties, make sure to repackage your application.
Quarkus configures most properties during build time. Extensions can define properties as overridable at run time, for example the database URL, a user name, and a password which can be specific to your target environment.
Prerequisites
- You have a Quarkus Maven project.
Procedure
To package your Quarkus project, enter the following command:
./mvnw clean package
./mvnw clean packageCopy to Clipboard Copied! Toggle word wrap Toggle overflow Use one of the following methods to set the configuration properties:
Setting system properties:
Enter the following command where
<key>is the name of the configuration property you want to add and<value>is the value of the property:java -D<key>=<value> -jar target/myapp-runner.jar
java -D<key>=<value> -jar target/myapp-runner.jarCopy to Clipboard Copied! Toggle word wrap Toggle overflow For example, to set the value of the
quarkus.datasource.passwordproperty, enter the following command:java -Dquarkus.datasource.password=youshallnotpass -jar target/myapp-runner.jar
java -Dquarkus.datasource.password=youshallnotpass -jar target/myapp-runner.jarCopy to Clipboard Copied! Toggle word wrap Toggle overflow Setting environment variables:
Enter the following command where
<key>is the name of the configuration property you want to set and<value>is the value of the property:export <key>=<value> ; java -jar target/myapp-runner.jar
export <key>=<value> ; java -jar target/myapp-runner.jarCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteEnvironment variable names follow the conversion rules of Eclipse MicroProfile. Convert the name to upper case and replace any character that is not alphanumeric with an underscore (
_).Using an environment file:
Create an
.envfile in your current working directory and add configuration properties where<PROPERTY_KEY>is the property name and<value>is the value of the property:<PROPERTY_KEY>=<value>
<PROPERTY_KEY>=<value>Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteFor development mode, this file can be located in the root directory of your project, but it is advised to not track the file in version control. If you create an
.envfile in the root directory of your project, you can define keys and values that the program reads as properties.Using the
application.propertiesfile.Place the configuration file in
$PWD/config/application.propertiesdirectory where the application runs so any runtime properties defined in that file will override the default configuration.NoteYou can also use the
config/application.propertiesfeatures in development mode. Place theconfig/application.propertiesinside thetargetdirectory. Any cleaning operation from the build tool, for examplemvn clean, will remove theconfigdirectory as well.
Chapter 7. Property expressions Copy linkLink copied to clipboard!
Property expressions are combinations of property references and plain text strings that you can use to substitute values of properties in your configuration.
Much like a variable, you can use a property expression in Quarkus to substitute a value of a configuration property instead of hardcoding it. A property expression is resolved when java.util.Properties reads the value of the property from a configuration source in your application.
This means that if a configuration property is read form your configuration at compile time, the property expression is also resolved at compile time. If the configuration property is overriden at runtime, its value is resolved at runtime.
Property expressions can be resolved using more than one configuration source. This means that you can use a value of a property that is defined in one configuration source to expand a property expression that you use in another configuration source.
If the value of a property in an expression cannot be resolved, and you do not set a default value for the expression, your application encounters a NoSuchElementException.
7.1. Example usage of property expressions Copy linkLink copied to clipboard!
In this section you can find examples of how you can use property expressions to achieve greater flexibility when configuring of your Quarkus application.
Substituting the value of a configuration property:
You can use a property expression to avoid hardcoding property values in you configuration. Use the
${<property_name>}syntax to write an expression that references a configuration property, as shown in the following example:application.properties
remote.host=quarkus.io callable.url=https://${remote.host}/remote.host=quarkus.io callable.url=https://${remote.host}/Copy to Clipboard Copied! Toggle word wrap Toggle overflow The value of the
callable.urlproperty resolves tohttps://quarkus.io/.Setting a property value that is specific to a particular configuration profile:
In the following example, the
%devconfiguration profile and the default configuration profile are set to use data source connection URLs with different host addresses. Depending on the configuration profile with which you start your application, your data source driver uses the database URL that you set for the profile:application.properties
%dev.quarkus.datasource.jdbc.url=jdbc:mysql://localhost:3306/mydatabase?useSSL=false quarkus.datasource.jdbc.url=jdbc:mysql://remotehost:3306/mydatabase?useSSL=false
%dev.quarkus.datasource.jdbc.url=jdbc:mysql://localhost:3306/mydatabase?useSSL=false quarkus.datasource.jdbc.url=jdbc:mysql://remotehost:3306/mydatabase?useSSL=falseCopy to Clipboard Copied! Toggle word wrap Toggle overflow You can achieve the same result in a simplified way by setting a different value of the custom
application.serverproperty for each configuration profile. You can then reference the property in the database connection URL of your application, as shown in the example:application.properties
%dev.application.server=localhost application.server=remotehost quarkus.datasource.jdbc.url=jdbc:mysql://${application.server}:3306/mydatabase?useSSL=false%dev.application.server=localhost application.server=remotehost quarkus.datasource.jdbc.url=jdbc:mysql://${application.server}:3306/mydatabase?useSSL=falseCopy to Clipboard Copied! Toggle word wrap Toggle overflow The
application.serverproperty resolves to the appropriate value depending on the profile that you choose when you run your application.Setting a default value of a property expression:
You can define a default value for a property expression. Quarkus uses the default value if the value of the property that is required to expand the expression is not resolved from any of your configuration sources. You can set a default value of an expression using the following syntax:
${<expression>:<default_value>}${<expression>:<default_value>}Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the following example, the property expression in the data source URL uses
mysql.db.serveras the default value of theapplication.serverproperty:application.properties
quarkus.datasource.jdbc.url=jdbc:mysql://${application.server:mysql.db.server}:3306/mydatabase?useSSL=falsequarkus.datasource.jdbc.url=jdbc:mysql://${application.server:mysql.db.server}:3306/mydatabase?useSSL=falseCopy to Clipboard Copied! Toggle word wrap Toggle overflow Nesting property expressions:
You can compose property expressions by nesting a property expression inside another property expression. When nested property expressions are expanded, the inner expression is expanded first:
${<outer_property_expression>${<inner_property_expression>}}${<outer_property_expression>${<inner_property_expression>}}Copy to Clipboard Copied! Toggle word wrap Toggle overflow Multiple property expressions:
You can join two or more property expression together as shown below:
${<first_property>}${<second_property>}${<first_property>}${<second_property>}Copy to Clipboard Copied! Toggle word wrap Toggle overflow Combining property expressions with environment variables:
You can use property expressions to substitute the values of environment variables. The expression in the following example substitutes the value that is set for the
HOSTenvironment variable as the value of theapplication.hostproperty. WhenHOSTenvironment variable is not set,application.hostuses the value of theremote.hostproperty as the default:application.properties
remote.host=quarkus.io application.host=${HOST:${remote.host}}remote.host=quarkus.io application.host=${HOST:${remote.host}}Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Chapter 8. Using configuration profiles Copy linkLink copied to clipboard!
You can use different configuration profiles depending on your environment. Configuration profiles enable you to have multiple configurations in the same file and select between them using a profile name. Red Hat build of Quarkus has three configuration profiles. In addition, you can create your own custom profiles.
Quarkus default profiles:
- dev: Activated in development mode
- test: Activated when running tests
- prod: The default profile when not running in development or test mode
Prerequisites
- You have a Quarkus Maven project.
Procedure
Open your Java resource file and add the following import statement:
import io.quarkus.runtime.configuration.ProfileManager;
import io.quarkus.runtime.configuration.ProfileManager;Copy to Clipboard Copied! Toggle word wrap Toggle overflow To display the current configuration profile, add a log invoking the
ProfileManager.getActiveProfile()method:LOGGER.infof("The application is starting with profile `%s`", ProfileManager.getActiveProfile());LOGGER.infof("The application is starting with profile `%s`", ProfileManager.getActiveProfile());Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteIt is not possible to access the current profile using the
@ConfigProperty("quarkus.profile")method.
8.1. Setting a custom configuration profile Copy linkLink copied to clipboard!
You can create as many configuration profiles as you want. You can have multiple configurations in the same file and you can select between them using a profile name.
Procedure
To set a custom profile, create a configuration property with the profile name in the
application.propertiesfile, where<key>is the name of the property,<value>is the property value, and<profile>is the name of a profile:%<profile>.<key>=<value>
%<profile>.<key>=<value>Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the following example configuration, the value of
quarkus.http.portis 9090 by default, and becomes 8181 when thedevprofile is activated:quarkus.http.port=9090 %dev.quarkus.http.port=8181
quarkus.http.port=9090 %dev.quarkus.http.port=8181Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use one of the following methods to enable a profile:
Set the
quarkus.profilesystem property.To enable a profile using the
quarkus.profilesystem property, enter the following command:mvn -Dquarkus.profile=<value> quarkus:dev
mvn -Dquarkus.profile=<value> quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Set the
QUARKUS_PROFILEenvironment variable.To enable profile using an environment variable, enter the following command:
export QUARKUS_PROFILE=<profile>
export QUARKUS_PROFILE=<profile>Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThe system property value takes precedence over the environment variable value.
To repackage the application and change the profile, enter the following command:
./mvnw package -Dquarkus.profile=<profile> java -jar target/myapp-runner.jar
./mvnw package -Dquarkus.profile=<profile> java -jar target/myapp-runner.jarCopy to Clipboard Copied! Toggle word wrap Toggle overflow The following example shows a command that activates the
prod-awsprofile:./mvnw package -Dquarkus.profile=prod-aws java -jar target/myapp-runner.jar
./mvnw package -Dquarkus.profile=prod-aws java -jar target/myapp-runner.jarCopy to Clipboard Copied! Toggle word wrap Toggle overflow
The default Quarkus application runtime profile is set to the profile used to build the application. Red Hat build of Quarkus automatically selects a profile depending on your environment mode. For example, when your application is running as a JAR, Quarkus is in prod mode.
Chapter 9. Setting custom configuration sources Copy linkLink copied to clipboard!
By default, your Quarkus application reads properties from the application.properties file in the src/main/resources subdirectory of your project. However, because Quarkus supports MicroProfile Config, you can also load the configuration of your application from other sources. You can introduce custom configuration sources for your configured values by providing classes that implement the org.eclipse.microprofile.config.spi.ConfigSource and the org.eclipse.microprofile.config.spi.ConfigSourceProvider interfaces. This procedure demonstrates how you can implement a custom configuration source in your Quarkus project.
Prerequisite
-
Have the Quarkus
config-quickstartproject.
Procedure
Create an
ExampleConfigSourceProvider.javafile in your project and add the following imports:src/main/java/org/acme/config/ExampleConfigSourceProvider.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a class that implements the
org.eclipse.microprofile.config.spi.ConfigSourceProviderinterface. You must override itsgetConfigSourcesmethod to return a list ofConfigSourceobjects:The following example shows a custom implementation of the
ConfigSourceProviderand theConfigSourceinterfaces:src/main/java/org/acme/config/ExampleConfigSourceProvider.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a file named
org.eclipse.microprofile.config.spi.ConfigSourceProviderin thesrc/main/resources/META-INF/services/subdirectory of your project, and enter the fully qualified name of the class that implements theConfigSourceProviderin the file that you created:src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSourceProvider
org.acme.config.ExampleConfigSourceProvider
org.acme.config.ExampleConfigSourceProviderCopy to Clipboard Copied! Toggle word wrap Toggle overflow You must perform this step to ensure that the
ConfigSourceProviderthat you created is registered and installed when you compile and start your application.Enter the following command to compile and start your application in development mode:
./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enter the following command in a new terminal window to verify that the
/greetingendpoint returns the expected message:curl http://localhost:8080/greeting
curl http://localhost:8080/greetingCopy to Clipboard Copied! Toggle word wrap Toggle overflow When your application reads the custom configuration properly, you receive the following response.
hello quarkus!
hello quarkus!Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Chapter 10. Using custom configuration converters as configuration values Copy linkLink copied to clipboard!
You can store custom types as configuration values by implementing org.eclipse.microprofile.config.spi.Converter<T> and adding its fully qualified class name into the META-INF/services/org.eclipse.microprofile.config.spi.Converter file.
Prerequisites
-
You have created the Quarkus
config-quickstartproject.
Procedure
Include the fully qualified class name of the converter in your
META-INF/services/org.eclipse.microprofile.config.spi.Converterservice file as shown in the following example:org.acme.config.MicroProfileCustomValueConverter org.acme.config.SomeOtherConverter org.acme.config.YetAnotherConverter
org.acme.config.MicroProfileCustomValueConverter org.acme.config.SomeOtherConverter org.acme.config.YetAnotherConverterCopy to Clipboard Copied! Toggle word wrap Toggle overflow Implement the converter class to override the convert method:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteYour custom converter class must be
publicand must have apublicno-argument constructor. Your custom converter class cannot beabstract.Use your custom type as a configuration value:
@ConfigProperty(name = "configuration.value.name") MicroProfileCustomValue value;
@ConfigProperty(name = "configuration.value.name") MicroProfileCustomValue value;Copy to Clipboard Copied! Toggle word wrap Toggle overflow
10.1. Setting custom converters priority Copy linkLink copied to clipboard!
The default priority for all Quarkus core converters is 200 and for all other converters it is 100. However, you can set a higher priority for your custom converters using the javax.annotation.Priority annotation.
The following procedure demonstrates an implementation of a custom converter MicroProfileCustomValue that is assigned a priority of 150 and will take precedence over MicroProfileCustomValueConverter which has a value of 100.
Prerequisites
-
You have created the Quarkus
config-quickstartproject.
Procedure
Add the following import statements to your service file:
package org.acme.config; import javax.annotation.Priority; import org.eclipse.microprofile.config.spi.Converter;
package org.acme.config; import javax.annotation.Priority; import org.eclipse.microprofile.config.spi.Converter;Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set a priority for your custom converter by annotating the class with the
@Priorityannotation and passing it a priority value:Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteIf you add a new converter, you must list it in the
META-INF/services/org.eclipse.microprofile.config.spi.Converterservice file.
Chapter 11. Adding YAML configuration support Copy linkLink copied to clipboard!
Red Hat build of Quarkus supports YAML configuration files through the SmallRye Config implementation of Eclipse MicroProfile Config. You can add the Quarkus Config YAML extension and use YAML over properties for configuration. Quarkus supports using application.yml as well as application.yaml as the name of the YAML file.
The YAML configuration file takes precedence over the application.properties file. The recommended approach is to delete the application.properties file and use only one type of configuration file to avoid errors.
Procedure
Use one of the following methods to add the YAML extension in your project:
Open the
pom.xmlfile and add thequarkus-config-yamlextension as a dependency:<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-config-yaml</artifactId> </dependency><dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-config-yaml</artifactId> </dependency>Copy to Clipboard Copied! Toggle word wrap Toggle overflow To add the
quarkus-config-yamlextension from the command line, enter the following command from your project directory:./mvnw quarkus:add-extension -Dextensions="quarkus-config-yaml"
./mvnw quarkus:add-extension -Dextensions="quarkus-config-yaml"Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.1. Using nested object configuration with YAML Copy linkLink copied to clipboard!
You can define a nested class inside an already existing class. This procedure shows how you can set nested configuration properties for your Quarkus application using a configuration file in YAML format.
Prerequisites
- Have a Quarkus Maven project.
- Have a PostgreSQL data source.
Have the following extensions as dependencies in the
pom.xmlfile of your project:-
quarkus-rest-client, -
quarkus-jdbc-postgresql -
quarkus-config-yaml
-
Procedure
-
Open the
src/main/resources/application.yamlconfiguration file. Add the nested class configuration properties to your
application.yamlfile as shown in the example:src/main/resources/application.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Note, that you can use comments to describe your configuration properties in a similar way as you use them in
application.properties.NoteAlways use spaces to indent the properties in your YAML configuration file. YAML does not allow using tabs for indentation.
11.2. Setting custom configuration profiles with YAML Copy linkLink copied to clipboard!
With Quarkus you can set configuration properties and values that are specific to different configuration profiles of your application. You can start your application with a specific profile to access a particular configuration. This procedure demonstrates how you can provide a configuration for a specific profile in YAML format.
Prerequisites
- Have a Quarkus Maven project that is configured to use a PostgreSQL data source with a JDBC data source driver.
-
Have the
quarkus-jdbc-postgresqlandquarkus-config-yamlextensions as dependencies in thepom.xmlfile of your project.
Procedure
-
Open the
src/main/resources/application.yamlconfiguration file. To set a profile dependent configuration, add the profile name before defining the key-value pairs using the
"%<profile_name>"syntax. Ensure that you place the profile name inside quotation marks. In YAML, all strings that begin with a special character must be placed inside quotation marks.In the following example the PostgreSQL database is configured to be available at the
jdbc:postgresql://localhost:5432/some-databaseURL when you start your Quarkus application in development mode:src/main/resources/application.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
11.3. Managing configuration key conflicts Copy linkLink copied to clipboard!
Structured formats such as YAML only support a subset of the possible configuration namespace. The following procedure shows a solution of a conflict between two configuration properties, quarkus.http.cors and quarkus.http.cors.methods, where one property is the prefix of another.
Prerequisites
- You have a Quarkus project configured to read YAML configuration files.
Procedure
- Open your YAML configuration file.
To define a YAML property as a prefix of another property, add a tilde (
~) in the scope of the property as shown in the following example:quarkus: http: cors: ~: true methods: GET,PUT,POSTquarkus: http: cors: ~: true methods: GET,PUT,POSTCopy to Clipboard Copied! Toggle word wrap Toggle overflow To compile your Quarkus application in development mode, enter the following command from the project directory:
./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteYou can use YAML keys for conflicting configuration keys at any level because they are not included in the assembly of configuration property name.
Chapter 12. Updating the functional test to validate configuration changes Copy linkLink copied to clipboard!
Before you test the functionality of your application, you must update the functional test to reflect the changes you made to the endpoint of your application. The following procedure shows how you can update your testHelloEndpoint method on the Quarkus config-quickstart project.
Procedure
-
Open the
GreetingResourceTest.javafile. Update the content of the
testHelloEndpointmethod:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Chapter 13. Packaging and running your Quarkus application Copy linkLink copied to clipboard!
After you compile your Quarkus project, you can package it in a JAR file and run it from the command line.
Prerequisites
- You have compiled your Quarkus project.
Procedure
To package your Quarkus project, enter the following command in the
rootdirectory:./mvnw clean package
./mvnw clean packageCopy to Clipboard Copied! Toggle word wrap Toggle overflow This command produces the following JAR files in the
/targetdirectory:-
config-quickstart-1.0-SNAPSHOT.jar: Contains the classes and resources of the projects. This is the regular artifact produced by the Maven build. -
config-quickstart-1.0-SNAPSHOT-runner.jar: Is an executable JAR file. Be aware that this file is not an uber-JAR file because the dependencies are copied into thetarget/libdirectory.
-
- If development mode is running, press CTRL+C to stop development mode. If you do not do this, you will have a port conflict.
To run the application, enter the following command:
java -jar target/config-quickstart-1.0-SNAPSHOT-runner.jar
java -jar target/config-quickstart-1.0-SNAPSHOT-runner.jarCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThe
Class-Pathentry of theMANIFEST.MFfile from therunnerJAR file explicitly lists the JAR files from thelibdirectory. If you want to deploy your application from another location, you must copy therunnerJAR file as well as thelibdirectory.