You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _versions/main/guides/conditional-extension-dependencies.adoc
+76-23Lines changed: 76 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,21 +9,21 @@ include::_attributes.adoc[]
9
9
:summary: Trigger the inclusion on additional extensions based on certain conditions.
10
10
:topics: extensions
11
11
12
-
Quarkus extension dependencies are usually configured in the same way as any other project dependencies in the project's build file, e.g. the Maven `pom.xml` or the Gradle build scripts. However, there are dependency types that aren't yet supported out-of-the-box by Maven and Gradle. What we refer here to as "conditional dependencies" is one example.
12
+
Quarkus extension dependencies are usually configured in the same way as any other project dependencies in a project's build file, e.g. the Maven `pom.xml` or the Gradle build scripts. However, Quarkus also supports types of dependencies that aren't supported out-of-the-box by Maven and Gradle. Conditional Quarkus extension dependencies falls into that category.
13
13
14
14
== Conditional Dependencies
15
15
16
-
The idea behind the notion of the conditional dependency is that such a dependency must be activated only if a certain condition is satisfied. If the condition is not satisfied then the dependency **must not** be activated. In that regard, conditional dependencies can be categorized as optional, i.e. they may or may not appear in the resulting set of project dependencies.
16
+
A conditional dependency is a dependency that is activated only if a certain condition is satisfied. If the condition is not satisfied then the dependency **must not** be activated. In that regard, conditional dependencies can be categorized as optional, meaning they may or may not appear in the resulting dependency graph.
17
17
18
-
In which cases could conditional dependencies be useful? A typical example would be a component that should be activated **only** in case all of its required dependencies are available. If one or more of the component's required dependencies aren't available, instead of failing, the component should simply not be activated.
18
+
A typical example of a conditional dependency would be a component that should be added to the classpath **only** in case all of its required dependencies are present on the classpath. If one or more of the component's required dependencies aren't available, instead of failing, the component should simply not be added.
19
19
20
-
== Quarkus Conditional Extension Dependencies
20
+
== Conditional Quarkus Extension Dependencies
21
21
22
-
Quarkus supports conditional extension dependencies. I.e. one Quarkus extension may declare one or more conditional dependencies on other Quarkus extensions. Conditional dependencies on and from non-extension artifacts aren't supported.
22
+
A Quarkus extension may declare one or more conditional dependencies on other Quarkus extensions. Conditional dependencies on and from non-extension artifacts aren't supported.
23
23
24
-
Let's take the following scenario as an example: `quarkus-extension-a` has an optional dependency on `quarkus-extension-b` which should be included in a Quarkus application only if `quarkus-extension-c` is found among its dependencies (direct or transitive). In other words, the presence of `quarkus-extension-c` is the condition which, if satisfied, enables `quarkus-extension-b` during the build of a Quarkus application.
24
+
Let's consider the following scenario as an example: `quarkus-extension-a` has an optional dependency on `quarkus-extension-b` which should be included in a Quarkus application only if `quarkus-extension-c` is found among the application dependencies (direct or transitive). In this case, the presence of `quarkus-extension-c` is the condition which, if satisfied, will trigger inclusion of the `quarkus-extension-b` when Quarkus application dependencies are resolved.
25
25
26
-
The condition which triggers activation of an extension is configured in the extension's descriptor, which is included into the runtime artifact of the extension as `META-INF/quarkus-extension.properties`. Given that extension descriptor is generated by the Quarkus plugin at extension build time, extension developers can add the following configuration to express the condition which would have to be satisfied for the extension to be activated:
26
+
The condition which triggers activation of an extension is configured in the extension's `META-INF/quarkus-extension.properties`, which is included in the runtime artifact of the extension.Extension developers can add the following configuration to express the condition which would have to be satisfied for the extension to be activated:
27
27
28
28
[source,xml]
29
29
----
@@ -61,20 +61,22 @@ The condition which triggers activation of an extension is configured in the ext
61
61
<!-- SKIPPED CONTENT -->
62
62
----
63
63
64
-
<1> runtime Quarkus extension artifact ID, in our example `quarkus-extension-b`;
64
+
<1> runtime Quarkus extension artifact ID;
65
65
<2> the goal that generates the extension descriptor which every Quarkus runtime extension project should be configured with;
66
-
<3> configuration of the condition which will have to be satisfied for this extension to be included into a Quarkus application expressed as a list of artifacts that must be present among the application dependencies;
66
+
<3> configuration of the dependency condition which will have to be satisfied for this extension to be added to a Quarkus application expressed as a list of artifacts that must be present among the application dependencies;
67
67
<4> an artifact key (in the format of `groupId:artifactId[:<classifier>:<extension>]` but typically simply `<groupId>:<artifactId>`) of the artifact that must be present among the application dependencies for the condition to be satisfied.
68
68
69
-
NOTE: In the example above the `artifact` used in the condition configuration happens to be a runtime Quarkus extension artifact but it could as well be any other artifact. There could also be more than one `artifact` element in the body of `dependencyCondition`.
69
+
NOTE: In the example above the `artifact` used in the condition configuration happens to be a runtime Quarkus extension artifact but it could as well be any other artifact. There could also be more than one `artifact` element in the body of the `dependencyCondition`.
70
70
71
-
Now, having a dependency activating condition in the descriptor of `quarkus-extension-b`, other extensions may declare a conditional dependency on it.
71
+
Now, having a dependency condition recorded in the descriptor of the `quarkus-extension-b`, other extensions may declare a conditional dependency on it.
72
72
73
-
A conditional dependency is configured in the runtime artifact of a Quarkus extension. In our example, it's the `quarkus-extension-a` that has a conditional dependency on `quarkus-extension-b`, which can be expressed in two ways.
73
+
NOTE: extensions with dependency conditions present in their metadata could still appear as regular dependencies in Maven `pom.xml` and Gradle build scripts.
74
+
75
+
A conditional dependency is configured in the runtime artifact of a Quarkus extension. In this example, the `quarkus-extension-a` will declare a conditional dependency on the `quarkus-extension-b`, which can be done in the following two ways.
74
76
75
77
=== Declaring a dependency as `optional`
76
78
77
-
If an extension was configured with a dependency condition in its descriptor, other extensions may configure a conditional dependency on it by simply adding `<optional>true</optional>` to the dependency configuration. In our example it would look like this:
79
+
If an extension includes a dependency condition in its descriptor, other extensions may configure a conditional dependency on it by simply adding `<optional>true</optional>` to the dependency configuration. In our example it would look like this:
78
80
79
81
[source,xml]
80
82
----
@@ -101,7 +103,9 @@ If an extension was configured with a dependency condition in its descriptor, ot
101
103
<1> the runtime extension artifact `quarkus-extension-a`
102
104
<2> declares an optional Maven dependency on the runtime extension artifact `quarkus-extension-b`
103
105
104
-
IMPORTANT: In general, for every runtime extension artifact dependency on another runtime extension artifact there must be a corresponding deployment extension artifact dependency on the other deployment extension artifact. And if the runtime dependency is declared as optional then the corresponding deployment dependency **must** also be configured as optional.
106
+
Given that `quarkus-extension-b` includes a dependency condition, Quarkus will interpret an optional dependency on the `quarkus-extension-b` as conditional.
107
+
108
+
IMPORTANT: In general, for every runtime extension artifact dependency on another runtime extension artifact there must be the corresponding deployment extension artifact dependency on the other deployment extension artifact. And if the runtime dependency is declared as optional then the corresponding deployment dependency **must** also be configured as optional.
105
109
106
110
[source,xml]
107
111
----
@@ -128,13 +132,11 @@ IMPORTANT: In general, for every runtime extension artifact dependency on anothe
128
132
<1> the deployment extension artifact `quarkus-extension-a-deployment`
129
133
<2> declares an optional Maven dependency on the deployment extension artifact `quarkus-extension-b-deployment`
130
134
131
-
Normally, optional Maven extension dependencies are ignored by the Quarkus dependency resolver at build time. In this case though, the optional dependency `quarkus-extension-b` includes a dependency condition in its extension descriptor, which turns this optional Maven dependency into a Quarkus conditional extension dependency.
132
-
133
-
IMPORTANT: If `quarkus-extension-b` wasn't declared as `<optional>true</optional>` that would make `quarkus-extension-b` a required dependency of `quarkus-extension-a` and its dependency condition would be ignored.
135
+
IMPORTANT: If the `quarkus-extension-b` dependency wasn't declared as `<optional>true</optional>` it would make the `quarkus-extension-b` a required dependency of the `quarkus-extension-a` and its dependency condition would be ignored by the application dependency resolver.
134
136
135
137
=== Declaring a conditional dependency in the Quarkus extension descriptor
136
138
137
-
Conditional dependencies can also be configured in the Quarkus extension descriptor. The conditional dependency configured above could be expressed in the extension descriptor of `quarkus-extension-a` as:
139
+
Conditional dependencies can also be configured in the Quarkus extension descriptor directly. Here is an example of how it can be done in the Quarkus extension plugin configuration of the `quarkus-extension-a`:
138
140
139
141
[source,xml]
140
142
----
@@ -171,10 +173,61 @@ Conditional dependencies can also be configured in the Quarkus extension descrip
171
173
172
174
<!-- SKIPPED CONTENT -->
173
175
----
176
+
<1> the runtime Quarkus extension artifact ID;
177
+
<2> the goal that generates the extension descriptor which every Quarkus runtime extension project should be configured with;
178
+
<3> the conditional dependency configuration element;
179
+
<4> the artifact coordinates of conditional dependencies on other extensions.
180
+
181
+
In this case, the Maven dependency is not at all required in the `pom.xml` file.
182
+
183
+
== Dev mode-only extension dependencies
184
+
185
+
Extensions can also declare conditional dependencies on other extensions using Dev mode as the condition or one of the conditions for those dependencies to be activated.
186
+
187
+
Dev mode-only extension dependencies can be configured in the Quarkus extension plugin in the following way:
<2> the goal that generates the extension descriptor which every Quarkus runtime extension project should be configured with;
226
+
<3> the Dev mode conditional dependency configuration element;
227
+
<4> the artifact coordinates of conditional dependencies on extensions that should be evaluated only if an application is launched in Dev mode.
228
+
229
+
The `quarkus-extension-b`, in this example, may or may not define its own condition to be evaluated.
174
230
175
-
<1> runtime Quarkus extension artifact ID, in our example `quarkus-extension-a`
176
-
<2> the goal that generates the extension descriptor which every Quarkus runtime extension project should be configured with
177
-
<3> conditional dependency configuration element
178
-
<4> artifact coordinates of conditional dependencies on other extensions.
231
+
If the `quarkus-extension-b` does not define a dependency condition on its own (there is no dependency condition recorded in its `META-INF/quarkus-extension.properties`), the `quarkus-extension-b` will only be added as a dependency of the `quarkus-extension-a` in Dev mode but not in other modes (prod or test).
179
232
180
-
In this case, the Maven dependency is not at all required in the `pom.xml`.
233
+
If the `quarkus-extension-b` does define a dependency condition on its own (a dependency condition recorded in its `META-INF/quarkus-extension.properties`), the `quarkus-extension-b` will be added as a dependency of the `quarkus-extension-a` in Dev mode only if its condition is satisfied (the artifacts it requires are present in the application dependency graph).
Qute is a templating engine designed specifically to meet the Quarkus needs.
14
-
The usage of reflection is minimized to reduce the size of native images.
13
+
Qute is a templating engine developed specifically for Quarkus.
14
+
Reflection usage is minimized to reduce the size of native images.
15
15
The API combines both the imperative and the non-blocking reactive style of coding.
16
-
In the development mode, all files located in `src/main/resources/templates` are watched for changes and modifications are immediately visible.
17
-
Furthermore, we try to detect most of the template problems at build time.
16
+
In development mode, all files located in `src/main/resources/templates` are monitored for changes, and modifications become visible immediately.
17
+
Furthermore, we aim to detect most template issues at build time.
18
18
In this guide, you will learn how to easily render templates in your application.
19
19
20
20
== Solution
@@ -65,7 +65,7 @@ Let's start with a Hello World template:
65
65
66
66
NOTE: Templates located in the `pub` directory are served via HTTP. This behavior is built-in, no controllers are needed. For example, the template `src/main/resources/templates/pub/foo.html` will be served from the paths `/foo` and `/foo.html` by default.
67
67
68
-
If your application is running, you can open your browser and hit: http://localhost:8080/hello?name=Martin
68
+
Once your application is running, you can open your browser and navigate to: http://localhost:8080/hello?name=Martin
69
69
70
70
For more information about Qute Web options, see the https://docs.quarkiverse.io/quarkus-qute-web/dev/index.html[Qute Web guide].
71
71
@@ -144,7 +144,7 @@ Hello Martin!
144
144
145
145
There's an alternate way to declare your templates in your Java code, which relies on the following convention:
146
146
147
-
- Organise your template files in the `/src/main/resources/templates` directory, by grouping them into one directory per resource class. So, if
147
+
- Organize your template files in the `/src/main/resources/templates` directory, by grouping them into one directory per resource class. So, if
148
148
your `ItemResource` class references two templates `hello` and `goodbye`, place them at `/src/main/resources/templates/ItemResource/hello.txt`
149
149
and `/src/main/resources/templates/ItemResource/goodbye.txt`. Grouping templates per resource class makes it easier to navigate to them.
150
150
- In each of your resource class, declare a `@CheckedTemplate static class Template {}` class within your resource class.
@@ -389,7 +389,7 @@ public class ItemResource {
389
389
*Template extension methods* are used to extend the set of accessible properties of data objects.
390
390
391
391
Sometimes, you're not in control of the classes that you want to use in your template, and you cannot add methods
392
-
to them. Template extension methods allows you to declare new method for those classes that will be available
392
+
to them. Template extension methods allow you to declare new methods for those classes that will be available
393
393
from your templates just as if they belonged to the target class.
394
394
395
395
Let's keep extending on our simple HTML page that contains the item name, price and add a discounted price.
@@ -442,7 +442,7 @@ grouped by target type, or in a single `TemplateExtensions` class by convention.
442
442
443
443
== Rendering Periodic Reports
444
444
445
-
Templating engine could be also very useful when rendering periodic reports.
445
+
The templating engine can also be very useful for rendering periodic reports.
446
446
You'll need to add the `quarkus-scheduler` and `quarkus-qute` extensions first.
0 commit comments