From 33d9c4659f7c8333545dbacbd7ff3e0596e3d561 Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Wed, 18 Sep 2024 12:27:04 -0300 Subject: [PATCH] Migrate extension Config to `@ConfigMapping` Moving the config interface to runtime --- .../quarkus-extension.adoc | 11 +++++++---- .../extension-version/deployment/pom.xml | 3 --- .../deployment/ExtensionVersionProcessor.java | 3 ++- .../version/deployment/VersionConfig.java | 14 -------------- .../version/runtime/VersionConfig.java | 17 +++++++++++++++++ 5 files changed, 26 insertions(+), 22 deletions(-) delete mode 100644 quarkus-workshop-super-heroes/super-heroes/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/VersionConfig.java create mode 100644 quarkus-workshop-super-heroes/super-heroes/extension-version/runtime/src/main/java/io/quarkus/workshop/superheroes/version/runtime/VersionConfig.java diff --git a/quarkus-workshop-super-heroes/docs/src/docs/asciidoc/optional-quarkus-extension/quarkus-extension.adoc b/quarkus-workshop-super-heroes/docs/src/docs/asciidoc/optional-quarkus-extension/quarkus-extension.adoc index 44961f0f2..cb7608dee 100644 --- a/quarkus-workshop-super-heroes/docs/src/docs/asciidoc/optional-quarkus-extension/quarkus-extension.adoc +++ b/quarkus-workshop-super-heroes/docs/src/docs/asciidoc/optional-quarkus-extension/quarkus-extension.adoc @@ -162,17 +162,20 @@ The version extension consists of a single build step that extracts the version -- First, let's create the configuration class that will allow users to dynamically configure whether the version should be printed: -Under the `deployment` module, create new class `io.quarkus.workshop.superheroes.version.deployment.VersionConfig` with the following content: +Under the `runtime` module, create a new interface named `io.quarkus.workshop.superheroes.version.runtime.VersionConfig` with the following content: [source,java] ---- -include::{code-root}/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/VersionConfig.java[] +include::{code-root}/extension-version/runtime/src/main/java/io/quarkus/workshop/superheroes/version/runtime/VersionConfig.java[] ---- -- +The `@ConfigMapping` annotation declares `VersionConfig` as a configuration interface which configuration properties are prefixed with `quarkus.version` prefix. It is also used in CDI aware environments to scan and register Config Mappings. -The `@ConfigRoot` annotation declares `VersionConfig` as a configuration class which configuration properties are prefixed with `quarkus.` prefix. +The `@ConfigRoot` annotation declares that the configuration is available during runtime. -The `@ConfigItem` is used to declare individual configuration properties. By default, the name of the property is derived from the name of the field. In our case, the user-configured property will be `quarkus.version.enabled`. +The `@WithDefault("true")` is used to declare the default value returned when `enabled()` is called. + +By default, the name of the property is derived from what's declared in the `prefix` attribute inside `@ConfigMapping` plus the name of the method. In our case, the user-configured property will be `quarkus.version.enabled`. Next, open the `ExtensionVersionProcessor` class, and update the content to be: diff --git a/quarkus-workshop-super-heroes/super-heroes/extension-version/deployment/pom.xml b/quarkus-workshop-super-heroes/super-heroes/extension-version/deployment/pom.xml index 10eb7f4ee..597edbe7d 100644 --- a/quarkus-workshop-super-heroes/super-heroes/extension-version/deployment/pom.xml +++ b/quarkus-workshop-super-heroes/super-heroes/extension-version/deployment/pom.xml @@ -37,9 +37,6 @@ ${quarkus.version} - - -AlegacyConfigRoot=true - diff --git a/quarkus-workshop-super-heroes/super-heroes/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/ExtensionVersionProcessor.java b/quarkus-workshop-super-heroes/super-heroes/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/ExtensionVersionProcessor.java index 3c5f62f42..abc6f377a 100644 --- a/quarkus-workshop-super-heroes/super-heroes/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/ExtensionVersionProcessor.java +++ b/quarkus-workshop-super-heroes/super-heroes/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/ExtensionVersionProcessor.java @@ -5,6 +5,7 @@ import io.quarkus.deployment.annotations.Record; import io.quarkus.deployment.builditem.ApplicationInfoBuildItem; import io.quarkus.deployment.builditem.FeatureBuildItem; +import io.quarkus.workshop.superheroes.version.runtime.VersionConfig; import io.quarkus.workshop.superheroes.version.runtime.VersionRecorder; class ExtensionVersionProcessor { @@ -19,7 +20,7 @@ FeatureBuildItem feature() { @BuildStep @Record(ExecutionTime.RUNTIME_INIT) void recordVersion(ApplicationInfoBuildItem app, VersionConfig versionConfig, VersionRecorder recorder) { - if (versionConfig.enabled) { + if (versionConfig.enabled()) { recorder.printVersion(app.getVersion()); } } diff --git a/quarkus-workshop-super-heroes/super-heroes/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/VersionConfig.java b/quarkus-workshop-super-heroes/super-heroes/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/VersionConfig.java deleted file mode 100644 index 16ae7af3c..000000000 --- a/quarkus-workshop-super-heroes/super-heroes/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/VersionConfig.java +++ /dev/null @@ -1,14 +0,0 @@ -package io.quarkus.workshop.superheroes.version.deployment; - -import io.quarkus.runtime.annotations.ConfigItem; -import io.quarkus.runtime.annotations.ConfigRoot; - -@ConfigRoot(name = "version") -public class VersionConfig { - - /** - * Enables or disables the version printing at startup. - */ - @ConfigItem(defaultValue = "true") - boolean enabled; -} diff --git a/quarkus-workshop-super-heroes/super-heroes/extension-version/runtime/src/main/java/io/quarkus/workshop/superheroes/version/runtime/VersionConfig.java b/quarkus-workshop-super-heroes/super-heroes/extension-version/runtime/src/main/java/io/quarkus/workshop/superheroes/version/runtime/VersionConfig.java new file mode 100644 index 000000000..44617c70b --- /dev/null +++ b/quarkus-workshop-super-heroes/super-heroes/extension-version/runtime/src/main/java/io/quarkus/workshop/superheroes/version/runtime/VersionConfig.java @@ -0,0 +1,17 @@ +package io.quarkus.workshop.superheroes.version.runtime; + +import io.quarkus.runtime.annotations.ConfigPhase; +import io.quarkus.runtime.annotations.ConfigRoot; +import io.smallrye.config.ConfigMapping; +import io.smallrye.config.WithDefault; + +@ConfigMapping(prefix = "quarkus.version") +@ConfigRoot(phase = ConfigPhase.RUN_TIME) +public interface VersionConfig { + + /** + * Enables or disables the version printing at startup. + */ + @WithDefault("true") + boolean enabled(); +}