此内容没有您所选择的语言版本。
Chapter 10. Adding YAML configuration support
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
10.1. Using nested object configuration with YAML 复制链接链接已复制到粘贴板!
You can define a nested class inside an already existing class. The following YAML configuration file example shows how you can set nested properties in your Quarkus application using the YAML format.
Prerequisites
- You have an existing Quarkus project that can read YAML configuration files.
Procedure
To start Quarkus in development mode, enter the following command in the directory that contains your Quarkus application
pom.xmlfile:./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Open your YAML configuration file.
- Add a nested class configuration similar to the following syntax:
10.2. Setting custom configuration profiles with YAML 复制链接链接已复制到粘贴板!
Red Hat build of Quarkus lets you create profile dependent configurations and switch between them as required.The following procedure demonstrates how you can provide a profile dependent configuration with YAML.
Prerequisites
- You have a Quarkus project configured to read YAML configuration files.
Procedure
- Open your YAML configuration file.
To set a profile dependent configuration, add the profile name before defining the key-value pairs using the
”%profile”syntax:In the following example the PostgreSQL database is configured to be available at the
jdbc:postgresql://localhost:5432/some-databaseURL when Quarkus runs in the development mode:Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you stopped the application, enter the following command to restart it:
./mvnw quarkus:dev
./mvnw quarkus:devCopy to Clipboard Copied! Toggle word wrap Toggle overflow
10.3. Managing configuration key conflicts 复制链接链接已复制到粘贴板!
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.