Skip to content

Commit

Permalink
Migrate extension Config to @ConfigMapping
Browse files Browse the repository at this point in the history
Moving the config interface to runtime
  • Loading branch information
gastaldi committed Sep 18, 2024
1 parent dbee2bb commit 33d9c46
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
<version>${quarkus.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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());
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 33d9c46

Please sign in to comment.