diff --git a/docs/src/main/asciidoc/config-reference.adoc b/docs/src/main/asciidoc/config-reference.adoc index c96912d08e7c56..b531f42d23c148 100644 --- a/docs/src/main/asciidoc/config-reference.adoc +++ b/docs/src/main/asciidoc/config-reference.adoc @@ -152,9 +152,9 @@ is to use Quarkus `application.properties`. === Additional Config Sources -Quarkus provides additional extensions which cover other configuration formats and stores: +Quarkus provides additional extensions that cover other configuration formats and stores: -* xref:config-yaml.adoc[YAML] +* xref:config-yaml-howto.adoc[YAML] * xref:vault.adoc[HashiCorp Vault] * xref:consul-config.adoc[Consul] * xref:spring-cloud-config-client.adoc[Spring Cloud] @@ -632,7 +632,7 @@ If you are in the rare situation that you need to change the build time configur [[additional-information]] == Additional Information -* xref:config-yaml.adoc[YAML ConfigSource Extension] +* xref:config-yaml-howto.adoc[YAML ConfigSource Extension] // Moved to Quarkiverse. There is a redirect to resolve the link * xref:vault.adoc[HashiCorp Vault ConfigSource Extension] // Moved to Quarkiverse. There is a redirect to resolve the link diff --git a/docs/src/main/asciidoc/config-yaml-howto.adoc b/docs/src/main/asciidoc/config-yaml-howto.adoc new file mode 100644 index 00000000000000..053825e09defc6 --- /dev/null +++ b/docs/src/main/asciidoc/config-yaml-howto.adoc @@ -0,0 +1,394 @@ +//// +This document is maintained in the main Quarkus repository, and pull requests should be submitted there: +https://github.com/quarkusio/quarkus/tree/main/docs/src/main/asciidoc +//// +// meta:assemblies/assembly_configuring-your-quarkus-applications-using-a-yaml-file.adoc +[id="config-yaml-howto"] += Configuring your Quarkus applications by using a YAML file +include::_attributes.adoc[] +:categories: core +:summary: Use `application.yaml` instead of `application.properties`. + +Optionally, you can configure your application with a YAML-formatted file, `application.yaml`, instead of the default `application.properties` file. + +// meta:include::modules/con_overview-quarkus-configuration.adoc[leveloffset=+1] +[id="con_overview-quarkus-configuration"] +== Overview + +You can use the `application.yaml` file to set your application's properties in a single place. + +{project-name} supports configuration profiles that you can use to group related properties and switch between profiles as required. + +By default, {project-name} reads properties from the `application.properties` file located in the `src/main/resources` directory. +Instead, you can configure {project-name} to read properties from a YAML file. + +When you add the `quarkus-config-yaml` extension to your project's `pom.xml` or `build.gradle` file, you can configure and manage your application's properties in the `application.yaml` file. + +// WAS include::modules/proc_adding-yaml-configuration-support.adoc[leveloffset=+1] +// meta:include::modules/proc_enabling_yaml_configuration.adoc[leveloffset=+1] +[id="proc_enabling_yaml_configuration"] +== Enabling YAML configuration + +To enable support for a `application.yaml` file, add the `quarkus-config-yaml` extension to your project's `pom.xml` or `build.gradle` file. + +.Prerequisites + +include::{includes}/prerequisites.adoc[] +* A {project-name} Maven project. + +.Procedure + +Add the `quarkus-config-yaml` extension to your project: + +:add-extension-extensions: quarkus-config-yaml +include::{includes}/devtools/extension-add.adoc[] + +Or, add the following dependency to your project: + +[source,xml,role="primary asciidoc-tabs-target-sync-cli asciidoc-tabs-target-sync-maven"] +.pom.xml +---- + + io.quarkus + quarkus-config-yaml + +---- + +[source,gradle,role="secondary asciidoc-tabs-target-sync-gradle"] +.build.gradle +---- +implementation("io.quarkus:quarkus-config-yaml") +---- + +Create the `src/main/resources/application.yaml` file. + +TIP: You can use the `yml` or `yaml` file extension. + +To avoid potential confusion, remove the `src/main/resources/application.properties` file. + +NOTE: If both files are present, the values of properties in `application.yaml` override any duplicate properties in `application.properties`. + +// meta:include::modules/proc_using-nested-object-configuration-with-yaml.adoc[leveloffset=+2] +[id="proc_using-nested-object-configuration-with-yaml"] +== Using nested object configuration with YAML + +In the YAML configuration file, you can define a nested element inside an existing element to set configuration properties for your {project-name} application. + +.Prerequisites + +include::{includes}/prerequisites.adoc[] +* A {project-name} Maven project. +* A PostgreSQL data source. +* The following extensions as dependencies in your project's `pom.xml` or `build.gradle` file: +** `quarkus-rest-client` +** `quarkus-jdbc-postgresql` +** `quarkus-config-yaml` + +.Procedure + +. Open the `src/main/resources/application.yaml` configuration file. + +. Add the nested configuration properties to your `application.yaml` file, as shown in the following example: ++ +.Example `application.yaml` file +[source,yaml] +---- +# Properties that configure the JDBC data source driver of your PostgreSQL data source +quarkus: + datasource: + url: jdbc:postgresql://localhost:5432/some-database + driver: org.postgresql.Driver + username: quarkus + password: quarkus + +# Property that configures the URL of the endpoint to which the rest client sends requests +org: + acme: + restclient: + CountriesService/mp-rest/url: https://restcountries.eu/rest + +# Property that configures the log message level for your application +quarkus: + log: + category: + +# Do not use spaces in names of configuration properties that you place inside quotation marks + "io.quarkus.category": + level: INFO +---- ++ +You can use comments to describe configuration properties. ++ +[NOTE] +==== +Always use spaces to indent the properties in your YAML configuration file. +YAML does not support using tabs for indentation. +==== + +// CREATE include::modules/conc_configuration-profiles-with-yaml.adoc[leveloffset=+2] +[id="conc_configuration-profiles-with-yaml"] +== Configuration profiles with YAML + +You can use xref:config-reference.adoc#profiles[profiles] in YAML. + +Because YAML does not support names that start with `%`, you must enclose the names of the profiles' configuration keys between double quotes. + +For example, in `application.yaml`, you must use `"%dev"` or `"%test"` as the configuration keys to identify properties that you want your Quarkus project to use when you build and run it using the development or test profiles. + +.Example `application.yaml` with `prod` and `dev` profiles +[source, yaml] +---- +quarkus: + http: + port: 8081 + +"%dev": + quarkus: + http: + port: 8082 +---- + +With the preceding example, if you don't specify a profile, Quarkus runs the `prod` profile by default, and the HTTP port number is `8081`. +If you enable the `dev` profile, the HTTP port number is `8082`. + +The YAML configuration also supports profile-aware files. +In this case, you keep the default `prod` properties in `application.yaml` and move the profile-specific properties to a separate file that you name `application-{profile}.yaml`; for example: + +.`application.yaml` +[source, yaml] +---- +quarkus: + http: + port: 8081 +---- + +.`application-dev.yaml` +[source, yaml] +---- +quarkus: + http: + port: 8082 +---- + +// meta:include::modules/proc_setting-custom-configuration-profiles-with-yaml.adoc[leveloffset=+2] +[id="proc_setting-custom-configuration-profiles-with-yaml"] +== Setting custom configuration profiles with YAML + +With {project-name}, you can set configuration properties and values specific to your application's different configuration profiles. +You can start your application with a specific profile to access a particular configuration. +This procedure shows how you can configure a specific profile in YAML format. + +[TIP] +==== +In YAML, you must place all strings that begin with a special character inside quotation marks. +==== + +.Prerequisites + +include::{includes}/prerequisites.adoc[] +* A {project-name} Maven project configured to use a PostgreSQL data source with a JDBC data source driver. +* The `quarkus-jdbc-postgresql` and `quarkus-config-yaml` extensions as dependencies in your project's `pom.xml` or `build.gradle` file. + +.Procedure + +. Open the `src/main/resources/application.yaml` configuration file. + +. To add a profile-specific configuration, enclose the profile's configuration key between double quotes, and then list the key-value pairs. ++ +.`src/main/resources/application.yaml` +[source,yaml] +---- +"%dev": + # Properties that configure the JDBC data source driver of your PostgreSQL data source + quarkus: + datasource: + url: jdbc:postgresql://localhost:5432/some-database + driver: org.postgresql.Driver + username: quarkus + password: quarkus +---- + +// meta:include::modules/con_property-expressions.adoc[leveloffset=+1] +[id="con_property-expressions"] +== Property expressions + +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 {project-name} to substitute a configuration property value instead of hardcoding it. +A property expression is resolved when the configuration system reads the property's value from a configuration source in your application. + +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 overridden at run time, its value is resolved at run time. + +You can resolve property expressions by using more than one configuration source. +This means that you can use the value of a property in one configuration source to expand a property expression that you use in another. + +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`. + +Property expressions use the `${}` syntax. + +// meta:include::modules/ref_example-usage-of-property-expressions-using-a-yaml-file.adoc[leveloffset=+2] + +[id="ref_example-usage-of-property-expressions-using-a-yaml-file"] +== Example usage of property expressions using a YAML file + +This section shows examples of using property expressions to achieve flexibility when configuring your {project-name} application. + +.Example `application.yaml` file +[source,properties,options="nowrap"] +---- +mach: 3 +x: + factor: 2.23694 + +display: + mach: ${mach} + unit: + name: "mph" + factor: ${x.factor} +---- + +[NOTE] +==== +You can reference nested properties by using the `.` (dot) separator, as in `{x.factor}`. +==== + +.Additional resources + +* For more information about property expressions, see xref:con_property-expressions[Property expressions]. +// * For an example of property expressions using a properties file, see link:{URL_CONFIGURATION_QUARKUS}#ref_example-usage-of-property-expressions_quarkus-configuration-guide[Example usage of property expressions]. // TBD update and enable this ^ link later when this guide becomes available + +// meta:include::modules/con_external-application-yaml-file-for-configuring-properties-at-runtime.adoc[leveloffset=+1] +[id="con_external-application-yaml-file-for-configuring-properties-at-runtime"] +== External application.yaml file for configuring properties at run time + +To configure your application at run time, add your `application.yaml` file to the `config` directory. + +[NOTE] +==== +The values in the `config/application.yaml` file override any values from the regular `application.yaml` file. +==== + +Ensure that the `config/application.yaml` file is in the root of the working directory relative to the {project-name} application runner, as outlined in the following example: + +[source,properties,options="nowrap"] +---- +├── config +│ └── application.yaml +├── my-app-runner +---- + +.Additional resources + +* xref:proc_enabling_yaml_configuration[Enabling YAML configuration]. + +//include::modules/proc_managing-configuration-key-conflicts.adoc[leveloffset=+1] +[id="proc_managing-configuration-key-conflicts"] +== Managing configuration key conflicts + +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 + +include::{includes}/prerequisites.adoc[] +* The `quarkus-config-yaml` extensions as a dependency in your project's `pom.xml` or `build.gradle` file. + +.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: ++ +.Example of defining a YAML property as a prefix +[source,yaml] +---- +quarkus: + http: + cors: + ~: true + methods: GET,PUT,POST +---- ++ +. To compile your {project-name} application in development mode, enter the following command from the project directory: ++ +.Compile your {project-name} application +[source,bash] +---- +./mvnw quarkus:dev +---- ++ +[NOTE] +==== +You can use YAML keys for conflicting configuration keys at any level because they are not included in the assembly of the configuration property name. +==== + +// == Additional resources +// +// * link:{URL_CONFIGURATION_QUARKUS}[Configuring your {project-name} applications by using a properties file] // TBD update link +// TBD update and enable this ^ link later when this guide becomes available + +=== Other example configurations + +The following snippets provide examples of YAML configurations: + +[source,yaml] +---- +# Data persistence configuration property +quarkus: + datasource: + db-kind: postgresql + jdbc: + url: jdbc:postgresql://localhost:5432/some-database + +# REST Client configuration property +quarkus: + rest-client: + org.acme.rest.client.ExtensionsService: + url: https://stage.code.quarkus.io/api +---- + +[source,yaml] +---- +# For configuration property names that use quotes, do not split the string inside the quotes +quarkus: + log: + category: + "io.quarkus.category": + level: INFO +---- + +[source, yaml] +---- +quarkus: + datasource: + url: jdbc:postgresql://localhost:5432/quarkus_test + + hibernate-orm: + database: + generation: drop-and-create + + oidc: + enabled: true + auth-server-url: http://localhost:8180/auth/realms/quarkus + client-id: app + + +app: + frontend: + oidc-realm: quarkus + oidc-app: app + oidc-server: http://localhost:8180/auth + +# With configuration profiles +"%test": + quarkus: + oidc: + enabled: false + security: + users: + file: + enabled: true + realm-name: quarkus + plain-text: true +---- diff --git a/docs/src/main/asciidoc/config-yaml.adoc b/docs/src/main/asciidoc/config-yaml.adoc deleted file mode 100644 index 33109f1b38aaaa..00000000000000 --- a/docs/src/main/asciidoc/config-yaml.adoc +++ /dev/null @@ -1,209 +0,0 @@ -//// -This guide is maintained in the main Quarkus repository -and pull requests should be submitted there: -https://github.com/quarkusio/quarkus/tree/main/docs/src/main/asciidoc -//// -= YAML Configuration -include::_attributes.adoc[] -:categories: core -:summary: YAML as a Configuration Source. - -https://en.wikipedia.org/wiki/YAML[YAML] is a very popular format. Kubernetes relies heavily on the YAML format to -write the various resource descriptors. - -Quarkus offers the possibility to use YAML in addition to the standard Java Properties file. - -== Enabling YAML Configuration - -To enable YAML configuration, add the `quarkus-config-yaml` extension: - -:add-extension-extensions: quarkus-config-yaml -include::{includes}/devtools/extension-add.adoc[] - -You can also just add the following dependency into your project: - -[source,xml,role="primary asciidoc-tabs-target-sync-cli asciidoc-tabs-target-sync-maven"] -.pom.xml ----- - - io.quarkus - quarkus-config-yaml - ----- - -[source,gradle,role="secondary asciidoc-tabs-target-sync-gradle"] -.build.gradle ----- -implementation("io.quarkus:quarkus-config-yaml") ----- - -Remove the `src/main/resources/application.properties` and create a `src/main/resources/application.yaml` file. - -NOTE: If both are present, Quarkus prioritizes configuration properties from the YAML file first and then from the -Properties file. However, to avoid confusion, we recommend removing the Properties file. - -TIP: Quarkus supports both the `yml` and `yaml` file extensions. - -=== Example - -The following snippets provide examples of YAML configuration: - -[source,yaml] ----- -# YAML supports comments -quarkus: - datasource: - db-kind: postgresql - jdbc: - url: jdbc:postgresql://localhost:5432/some-database - -# REST Client configuration property -quarkus: - rest-client: - org.acme.rest.client.ExtensionsService: - url: https://stage.code.quarkus.io/api ----- - -[source,yaml] ----- -# For configuration property names that use quotes, do not split the string inside the quotes -quarkus: - log: - category: - "io.quarkus.category": - level: INFO ----- - -[source, yaml] ----- -quarkus: - datasource: - url: jdbc:postgresql://localhost:5432/quarkus_test - - hibernate-orm: - database: - generation: drop-and-create - - oidc: - enabled: true - auth-server-url: http://localhost:8180/auth/realms/quarkus - client-id: app - - -app: - frontend: - oidc-realm: quarkus - oidc-app: app - oidc-server: http://localhost:8180/auth - -# With profiles -"%test": - quarkus: - oidc: - enabled: false - security: - users: - file: - enabled: true - realm-name: quarkus - plain-text: true ----- - -== Profiles - -As you can see in the previous snippet, you can use xref:config-reference.adoc#profiles[profiles] in YAML. The profile -key requires double quotes: `"%test"`. This is because YAML does not support keys starting with `%`. - -Everything under the `"%test"` key is only enabled when the `test` profile is active. For example, in the previous -snippet it disables OIDC (`quarkus.oidc.enabled: false`), whereas without the `test` profile, it would be enabled. - -As for the Java Properties format, you can define your own profile: - -[source, yaml] ----- -quarkus: - http: - port: 8081 - -"%staging": - quarkus: - http: - port: 8082 ----- - -If you enable the `staging` profile, the HTTP port will be 8082, whereas it would be 8081 otherwise. - -The YAML configuration also support profile aware files. In this case, properties for a specific profile may reside in -an `application-{profile}.yaml` named file. The previous example may be expressed as: - -.application.yaml -[source, yaml] ----- -quarkus: - http: - port: 8081 ----- - -.application-staging.yaml -[source, yaml] ----- -quarkus: - http: - port: 8082 ----- - -== Expressions - -The YAML format also supports xref:config-reference.adoc#property-expressions[property expressions], using the same format as Java -Properties: - -[source, yaml] ----- -mach: 3 -x: - factor: 2.23694 - -display: - mach: ${mach} - unit: - name: "mph" - factor: ${x.factor} ----- - -Note that you can reference nested properties using the `.` (dot) separator as in `${x.factor}`. - -== External application.yaml file - -The `application.yaml` file may also be placed in `config/application.yaml` to specialize the runtime configuration. The -file has to be present in the root of the working directory relative to the Quarkus application runner: - -[source, text] ----- -. -├── config -│ └── application.yaml -├── my-app-runner ----- - -The values from this file override any values from the regular `application.yaml` file if exists. - -== Configuration key conflicts - -The MicroProfile Config specification defines configuration keys as an arbitrary `.`-delimited string. However, -structured formats like YAML only support a subset of the possible configuration namespace. For example, consider the -two configuration properties `quarkus.http.cors` and `quarkus.http.cors.methods`. One property is the prefix of another, -so it may not be immediately evident how to specify both keys in your YAML configuration. - -This is solved by using a `null` key (represented by `~`) for any YAML property which is a prefix of another one: - -[source,yaml] ----- -quarkus: - http: - cors: - ~: true - methods: GET,PUT,POST ----- - -YAML `null` keys are not included in the assembly of the configuration property name, allowing them to be used -in any level for disambiguating configuration keys. diff --git a/docs/src/main/asciidoc/config.adoc b/docs/src/main/asciidoc/config.adoc index 9c560a7fc4f638..bf2d827982f3af 100644 --- a/docs/src/main/asciidoc/config.adoc +++ b/docs/src/main/asciidoc/config.adoc @@ -221,7 +221,7 @@ application behaviour at runtime. == Additional Information * xref:config-reference.adoc[Configuration Reference Guide] -* xref:config-yaml.adoc[YAML ConfigSource Extension] +* xref:config-yaml-howto.adoc[YAML ConfigSource Extension] * xref:vault.adoc[HashiCorp Vault ConfigSource Extension] * xref:consul-config.adoc[Consul ConfigSource Extension] * xref:spring-cloud-config-client.adoc[Spring Cloud ConfigSource Extension]