Chapter 1. Configuring your Quarkus applications
This guide describes how to configure your Quarkus application. You can configure your Quarkus application by using either of the following methods:
-
Setting properties in the
application.propertiesfile -
Applying structured configuration in YAML format by updating the
application.yamlfile
This guide also describes how you can extend and customize the configuration for your application by doing the following:
- Substituting and composing configuration property values using property expressions
- Implementing MicroProfile-compliant classes with custom configuration source converters that read configuration values from different external sources
- Using configuration profiles to maintain separate sets of configuration values for your development, test, and production environments
The procedures include configuration examples that are created by using the Quarkus config-quickstart exercise.
Prerequisites
Have OpenJDK (JDK) 11 or 17 installed and the
JAVA_HOMEenvironment variable set to specify the location of the Java SDK.- Log in to 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.
1.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. Red Hat build of Quarkus supports configuration profiles that you can use to group related properties and switch between profiles as required.
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. For more information, see Adding YAML configuration support.
Red Hat build of Quarkus also supports MicroProfile Config, which enables you to load the configuration of your application from other sources.
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.
Quarkus can also read application properties from different origins, including the following sources:
- The file system
- A database
-
A Kubernetes or OpenShift
ConfigMapobject - Any source that can be loaded by a Java application
1.2. Creating the configuration quickstart project Copy linkLink copied to clipboard!
With the config-quickstart project, you can 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 Verification
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.jvmandDockerfile.nativefiles in thesrc/main/dockersubdirectory - 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.
1.3. 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 either through context and dependency injection (CDI) or by using a method that is defined in your code.
Use the @ConfigProperty annotation to map an object property to a key in the MicroProfile Config Sources file of your application.
The following procedure shows how you can inject an individual property configuration into a Quarkus config-quickstart project. The procedure and examples use the application.properties file configuration method.
Prerequisites
-
You have created the Quarkus
config-quickstartproject.
Procedure
-
Open the
src/main/resources/application.propertiesfile. Add configuration properties to your configuration file where
<property_name>is the property name and<value>is the value of the property:<property_name>=<value>
<property_name>=<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 ImportantWhen you are configuring your applications, do not prefix application-specific properties with the string
quarkus. Thequarkusprefix is reserved for configuring Quarkus at the framework level. Usingquarkusas a prefix for application-specific properties might lead to unexpected results when your application runs.Review the
GreetingResource.javaJava file in your project. The file contains theGreetingResourceclass with thehello()method that returns a message when you send an HTTP request on the/greetingendpoint:src/main/java/org/acme/config/GreetingResource.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the example provided, the values of the
messageandnamestrings in thehello()method are unconfigured. The application fails to compile and run successfully in this state.Define the
message,name, andsuffixfields, and annotate them with@ConfigProperty, matching the values that you defined for thegreeting.messageandgreeting.nameproperties. Use the@ConfigPropertyannotation to inject the configuration value for each string, as 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 configure a value for the
messagestring, the application will fail and throw the following exception:javax.enterprise.inject.spi.DeploymentException: Failed to load config value of type class java.lang.String for: greeting.message - 2
- If you do not configure a value for the
greeting.suffix, Quarkus resolves it to the default value. - 3
- If you do not define the
greeting.nameproperty, the value of thenamestring is blank. Your application still runs even when this value is blank because you set theOptionalparameter on thenamestring.
NoteTo inject a configured value, you can use
@ConfigProperty. You do not need to include the@Injectannotation for members that you annotate with@ConfigProperty.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 - To stop the application, press Ctrl+C.
1.4. 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 that 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
1.5. Setting configuration properties Copy linkLink copied to clipboard!
By default, Quarkus reads properties from the application.properties file that is located in the src/main/resources directory. If you change build properties, ensure that you 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
<property_name>is the name of the configuration property that you want to add and<value>is the value of the property:java -D<property_name>=<value> -jar target/myapp-runner.jar
java -D<property_name>=<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
<property_name>is the name of the configuration property that you want to set and<value>is the value of the property:export <property_name>=<value> ; java -jar target/myapp-runner.jar
export <property_name>=<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_NAME>is the property name and<value>is the value of the property:<PROPERTY_NAME>=<value>
<PROPERTY_NAME>=<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 recommended 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 the
$PWD/config/application.propertiesdirectory where the application runs so that any runtime properties that are defined in that file will override the default configuration.NoteYou can also use the
config/application.propertiesfeatures in development mode. Place theconfig/application.propertiesfile inside thetargetdirectory. Any cleaning operation from the build tool, for example,mvn clean, also removes theconfigdirectory.
1.6. Advanced Configuration Mapping Copy linkLink copied to clipboard!
Information and examples are provided to help you with advanced configuration mapping use cases.
The following advanced mapping procedures are extensions specific to Red Hat build of Quarkus and are outside of the 'MicroProfile Config' specification.
1.6.1. Annotating an interface with @ConfigMapping Copy linkLink copied to clipboard!
Instead of individually injecting multiple related configuration values, use the @io.smallrye.config.ConfigMapping annotation to group configuration properties. The following procedure demonstrates how you can use the @ConfigMapping annotation on the Quarkus config-quickstart project.
Prerequisites
-
You have created the Quarkus
config-quickstartproject. -
You have defined the
greeting.messageandgreeting.nameproperties in theapplication.propertiesfile of your project.
Procedure
Review the
GreetingResource.javafile in your project and ensure that it contains the contents that are shown in this example. To use the@ConfigPopertiesannotation to inject configuration properties from another configuration source into this class, you must import thejava.util.Optionalandorg.eclipse.microprofile.config.inject.ConfigPropertypackages.src/main/java/org/acme/config/GreetingResource.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
GreetingConfiguration.javafile in thesrc/main/java/org/acme/configdirectory. Add the import statements forConfigMappingandOptionalto the file:src/main/java/org/acme/config/GreetingConfiguration.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - The
prefixproperty is optional. For example, in this scenario, the prefix isgreeting. - If
greeting.suffixis not set,!is used as the default value.
Inject the
GreetingConfigurationinstance into theGreetingResourceclass by using the@Injectannotation, as follows:NoteThis snippet replaces the three fields that are annotated with
@ConfigPropertythat are in the initial version of theconfig-quickstartproject.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 a
io.smallrye.config.ConfigValidationExceptionerror is returned to indicate that a value is missing. This does not apply to fields that are optional or fields with a default value.To verify that the endpoint returns the message, enter the following command in a new terminal window:
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 - To stop the application, press Ctrl+C.
1.6.2. Using nested object configuration Copy linkLink copied to clipboard!
You can define an interface that is nested inside another interface. This procedure demonstrates how to create and configure a nested interface in the Quarkus config-quickstart project.
Prerequisites
-
You have created the Quarkus
config-quickstartproject. -
You have defined the
greeting.messageandgreeting.nameproperties in theapplication.propertiesfile of your project.
Procedure
Review the
GreetingResource.javain your project. The file contains theGreetingResourceclass with thehello()method that returns a message when you send an HTTP request on the/greetingendpoint:src/main/java/org/acme/config/GreetingResource.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
GreetingConfiguration.javaclass file with theGreetingConfigurationinstance. This class contains the externalized configuration for thehello()method that is defined in theGreetingResourceclass:src/main/java/org/acme/config/GreetingConfiguration.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the
ContentConfigclass that is nested inside theGreetingConfigurationinstance, as shown in the following example:src/main/java/org/acme/config/GreetingConfiguration.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThe method name of the
ContentConfigclass iscontent. To ensure that you bind the properties to the correct interface, when you define configuration properties for this class, usecontentin the prefix. Doing this can also prevent property name conflicts and unexpected application behavior.Define the
greeting.content.prize-amountandgreeting.content.recipientsconfiguration properties in yourapplication.propertiesfile.The following example shows the properties defined for the
GreetingConfigurationinstance and theContentConfigclasses: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 Instead of the three
@ConfigPropertyfield annotation, inject theGreetingConfigurationinstance into theGreetingResourceclass by using the@Injectannotation, as outlined in the following example. You must also update the message string that the/greetingendpoint returns with the values that you set for the newgreeting.content.prize-amountand also thegreeting.content.recipientsproperties that you have 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.To verify that the endpoint returns the message, enter the following command in a new terminal window:
curl http://localhost:8080/greeting
curl http://localhost:8080/greetingCopy to Clipboard Copied! Toggle word wrap Toggle overflow A message displays containing two lines of output. The first line displays the greeting, and the second line reports the recipients of the prize together with the prize amount, as follows:
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 - To stop the application, press Ctrl+C.
Classes annotated with @ConfigMapping can be annotated with Bean Validation annotations similar to the following example:
Your project must include the quarkus-hibernate-validator dependency.
1.7. Accessing the configuration programmatically Copy linkLink copied to clipboard!
You can define a method in your code to retrieve the values of the configuration properties in your application. By using this mechanism, you can dynamically look up configuration property values or retrieve configuration property values from classes that are either CDI beans or JAX-RS resources.
You can access the configuration by 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
Use a method to access the value of a configuration property of any class or object in your application code. Depending on whether or not the value that you want to retrieve is set in a configuration source in your project, you can use one of the following methods:
To access the value of a property that is set in a configuration source in your project, for example, in the
application.propertiesfile, use thegetValue()method:String <variable-name> = ConfigProvider.getConfig().getValue("<property-name>", <data-type-class-name>.class);String <variable-name> = ConfigProvider.getConfig().getValue("<property-name>", <data-type-class-name>.class);Copy to Clipboard Copied! Toggle word wrap Toggle overflow For example, to retrieve the value of the
greeting.messageproperty that has the data typeString, and is assigned to themessagevariable in your code, use the following syntax:String message = config.getValue("greeting.message", String.class);String message = config.getValue("greeting.message", String.class);Copy to Clipboard Copied! Toggle word wrap Toggle overflow When you want to retrieve a value that is optional or default and might not be defined in your
application.propertiesfile or another configuration source in your application, use thegetOptionalValue()method:String <variable-name> = ConfigProvider.getConfig().getOptionalValue("<property-name>", <data-type-class-name>.class);String <variable-name> = ConfigProvider.getConfig().getOptionalValue("<property-name>", <data-type-class-name>.class);Copy to Clipboard Copied! Toggle word wrap Toggle overflow For example, to retrieve the value of the
greeting.nameproperty that is optional, has the data typeString, and is assigned to thenamevariable in your code, use the following syntax:Optional<String> name = config.getOptionalValue("greeting.name", String.class);Optional<String> name = config.getOptionalValue("greeting.name", String.class);Copy to Clipboard Copied! Toggle word wrap Toggle overflow
The following snippet shows a variant of the aforementioned GreetingResource class by using the programmatic access to the configuration:
src/main/java/org/acme/config/GreetingResource.java
1.8. 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 from 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.
You can resolve property expressions by 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.
1.8.1. Example usage of property expressions Copy linkLink copied to clipboard!
This section shows examples of how you can use property expressions to achieve greater flexibility when configuring your Quarkus application.
Substituting the value of a configuration property:
To avoid hardcoding property values in your configuration, you can use a property expression. 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 for 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 following 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 for an expression by using the following syntax:
${<property_name>:<default_value>}${<property_name>:<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_name>${<inner_property_name>}}${<outer_property_name>${<inner_property_name>}}Copy to Clipboard Copied! Toggle word wrap Toggle overflow Combining multiple property expressions:
You can join two or more property expressions together by using the following syntax:
${<first_property_name>}${<second_property_name>}${<first_property_name>}${<second_property_name>}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. When theHOSTenvironment variable is not set, theapplication.hostproperty uses 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
1.9. 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 the YAML configuration file over the properties file for configuration. Quarkus supports the use of application.yml and 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
1.9.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 by 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 following example:src/main/resources/application.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow In a similar way to how you use comments in the
application.propertiesfile, you can use comments to describe your configuration properties in YAML format.NoteAlways use spaces to indent the properties in your YAML configuration file. YAML does not allow you to use tabs for indentation.
1.9.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 shows 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 by using the
"%<profile_name>"syntax. Ensure that you place the profile name inside quotation marks.TipIn 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
1.9.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 how to resolve a conflict between two configuration properties, quarkus.http.cors and quarkus.http.cors.methods, where one property is the prefix of another.
Prerequisites
- Have a Quarkus project that is 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.
1.10. 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 to select between them by 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 by using the
@ConfigProperty("quarkus.profile")method.
1.10.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 by using a profile name.
Procedure
To set a custom profile, create a configuration property with the profile name in the
application.propertiesfile, where<property_name>is the name of the property,<value>is the property value, and<profile>is the name of a profile:%<profile>.<property_name>=<value>
%<profile>.<property_name>=<value>Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the following example configuration, the value of
quarkus.http.portis9090by default, and becomes8181when 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.
1.11. Setting custom configuration sources Copy linkLink copied to clipboard!
By default, a Quarkus application reads properties from the application.properties file in the src/main/resources subdirectory of your project. Quarkus also allows you to load application configuration properties from other sources according to the MicroProfile Config specification for externalized configuration. You can enable your application to load configuration properties from other sources by defining 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 a class file in your project that implements the
org.eclipse.microprofile.config.spi.ConfigSourceProviderinterface. To return a list ofConfigSourceobjects, you must override itsgetConfigSourcesmethod.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 complete the previous step to ensure that the
ConfigSourceProviderthat you created is registered and installed when you compile and start your application.To compile and start your application in development mode, enter the following command:
./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow To verify that the
/greetingendpoint returns the expected message, in a terminal window enter the following command:curl http://localhost:8080/greeting
curl http://localhost:8080/greetingCopy to Clipboard Copied! Toggle word wrap Toggle overflow When your application successfully reads the custom configuration, you receive the following response:
hello quarkus!
hello quarkus!Copy to Clipboard Copied! Toggle word wrap Toggle overflow
1.12. 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
1.12.1. Setting custom converters priority Copy linkLink copied to clipboard!
The default priority for all Quarkus core converters is 200. For all other converters, the default priority is 100. You can increase the priority of your custom converters by using the javax.annotation.Priority annotation.
The following procedure demonstrates an implementation of a custom converter MicroProfileCustomValue that is assigned a priority of 150, which takes precedence over the MicroProfileCustomValueConverter, whose value is set to 100.
Prerequisites
-
You have created the Quarkus
config-quickstartproject. - You have created a custom configuration converter for your application.
Procedure
Set a priority for your custom converter by annotating the class with the
@Priorityannotation and passing it a priority value. In the following example, the priority value is set to150.ExampleCustomConverter.java
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a file named
org.eclipse.microprofile.config.spi.Converterin thesrc/main/resources/META-INF/services/subdirectory of your project, and enter the fully qualified name of the class that implements theConverterin the file that you created:src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.Converter
org.acme.config.ExampleCustomConverter
org.acme.config.ExampleCustomConverterCopy to Clipboard Copied! Toggle word wrap Toggle overflow You must complete the previous step to ensure that the
Converterthat you created is registered and installed when you compile and start your application.
1.13. 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 were 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
Verification
After you have completed the required configuration, the next step is to compile and then package your Quarkus application. For more information and examples, see the compiling and packaging sections of the Getting started with Quarkus guide.