Skip to content

Commit e9acaaf

Browse files
authored
Merge pull request #600 from gastaldi/extension_config
Migrate extension Config to `@ConfigMapping`
2 parents 2252bd0 + af16fa3 commit e9acaaf

File tree

6 files changed

+44
-37
lines changed

6 files changed

+44
-37
lines changed

quarkus-workshop-super-heroes/docs/src/docs/asciidoc/optional-quarkus-extension/quarkus-extension.adoc

+21-15
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,29 @@ For this, what do we need:
135135
The _runtime_ part of an extension contains only the classes and resources required at runtime.
136136
For the version extension, it would be a single class that prints the version.
137137

138+
First, let's create the configuration class that will allow users to dynamically configure whether the version should be printed:
139+
[example, role="cta"]
140+
--
141+
142+
Under the `runtime` module, create the `src/main/java` directory, and, in this directory, create a new interface named `io.quarkus.workshop.superheroes.version.runtime.VersionConfig` with the following content:
143+
144+
[source,java]
145+
----
146+
include::{code-root}/extension-version/runtime/src/main/java/io/quarkus/workshop/superheroes/version/runtime/VersionConfig.java[]
147+
----
148+
--
149+
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.
150+
151+
The `@ConfigRoot` annotation declares that the configuration is available during runtime.
152+
153+
The `@WithDefault("true")` is used to declare the default value returned when `enabled()` is called.
154+
155+
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`.
156+
138157
[example, role="cta"]
139158
--
140159

141-
In the runtime module, create the `src/main/java` directory, and, in this directory, the `io.quarkus.workshop.superheroes.version.runtime.VersionRecorder` class with the following content:
160+
Still in the `runtime` module, create the `io.quarkus.workshop.superheroes.version.runtime.VersionRecorder` class with the following content:
142161

143162
[source,java]
144163
----
@@ -160,27 +179,14 @@ The version extension consists of a single build step that extracts the version
160179

161180
[example, role="cta"]
162181
--
163-
First, let's create the configuration class that will allow users to dynamically configure whether the version should be printed:
164-
165-
Under the `deployment` module, create new class `io.quarkus.workshop.superheroes.version.deployment.VersionConfig` with the following content:
166-
167-
[source,java]
168-
----
169-
include::{code-root}/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/VersionConfig.java[]
170-
----
171-
--
172-
173-
The `@ConfigRoot` annotation declares `VersionConfig` as a configuration class which configuration properties are prefixed with `quarkus.` prefix.
174-
175-
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`.
176182

177183
Next, open the `ExtensionVersionProcessor` class, and update the content to be:
178184

179185
[source, java]
180186
----
181187
include::{code-root}/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/ExtensionVersionProcessor.java[]
182188
----
183-
189+
--
184190
This class is the core of the extension.
185191
It contains a set of methods annotated with `@BuildStep`.
186192

quarkus-workshop-super-heroes/super-heroes/extension-version/deployment/pom.xml

-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@
3737
<version>${quarkus.version}</version>
3838
</path>
3939
</annotationProcessorPaths>
40-
<compilerArgs>
41-
<arg>-AlegacyConfigRoot=true</arg>
42-
</compilerArgs>
4340
</configuration>
4441
</plugin>
4542
</plugins>

quarkus-workshop-super-heroes/super-heroes/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/ExtensionVersionProcessor.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.quarkus.deployment.annotations.Record;
66
import io.quarkus.deployment.builditem.ApplicationInfoBuildItem;
77
import io.quarkus.deployment.builditem.FeatureBuildItem;
8+
import io.quarkus.workshop.superheroes.version.runtime.VersionConfig;
89
import io.quarkus.workshop.superheroes.version.runtime.VersionRecorder;
910

1011
class ExtensionVersionProcessor {
@@ -19,8 +20,6 @@ FeatureBuildItem feature() {
1920
@BuildStep
2021
@Record(ExecutionTime.RUNTIME_INIT)
2122
void recordVersion(ApplicationInfoBuildItem app, VersionConfig versionConfig, VersionRecorder recorder) {
22-
if (versionConfig.enabled) {
23-
recorder.printVersion(app.getVersion());
24-
}
23+
recorder.printVersion(versionConfig, app.getVersion());
2524
}
2625
}

quarkus-workshop-super-heroes/super-heroes/extension-version/deployment/src/main/java/io/quarkus/workshop/superheroes/version/deployment/VersionConfig.java

-14
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.quarkus.workshop.superheroes.version.runtime;
2+
3+
import io.quarkus.runtime.annotations.ConfigPhase;
4+
import io.quarkus.runtime.annotations.ConfigRoot;
5+
import io.smallrye.config.ConfigMapping;
6+
import io.smallrye.config.WithDefault;
7+
8+
@ConfigMapping(prefix = "quarkus.version")
9+
@ConfigRoot(phase = ConfigPhase.RUN_TIME)
10+
public interface VersionConfig {
11+
12+
/**
13+
* Enables or disables the version printing at startup.
14+
*/
15+
@WithDefault("true")
16+
boolean enabled();
17+
}

quarkus-workshop-super-heroes/super-heroes/extension-version/runtime/src/main/java/io/quarkus/workshop/superheroes/version/runtime/VersionRecorder.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
@Recorder
77
public class VersionRecorder {
88

9-
public void printVersion(String version) {
10-
Logger.getLogger(VersionRecorder.class.getName()).infof("Version: %s", version);
9+
public void printVersion(VersionConfig versionConfig, String version) {
10+
if (versionConfig.enabled()) {
11+
Logger.getLogger(VersionRecorder.class.getName()).infof("Version: %s", version);
12+
}
1113
}
1214

1315
}

0 commit comments

Comments
 (0)