Skip to content

Commit 2b4342a

Browse files
committed
Introduce @RegisterResourceBundle and @RegisterResources
1 parent cb6ba92 commit 2b4342a

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.quarkus.deployment.steps;
2+
3+
import org.jboss.jandex.DotName;
4+
5+
import io.quarkus.deployment.annotations.BuildProducer;
6+
import io.quarkus.deployment.annotations.BuildStep;
7+
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
8+
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBundleBuildItem;
9+
import io.quarkus.runtime.annotations.RegisterResourceBundle;
10+
11+
public class RegisterResourceBundleBuildStep {
12+
13+
@BuildStep
14+
public void build(CombinedIndexBuildItem combinedIndexBuildItem,
15+
BuildProducer<NativeImageResourceBundleBuildItem> resourceBundle) {
16+
for (var annotationInstance : combinedIndexBuildItem.getIndex()
17+
.getAnnotations(DotName.createSimple(RegisterResourceBundle.class.getName()))) {
18+
var bundleNameValue = annotationInstance.value("bundleName");
19+
var moduleNameValue = annotationInstance.value("moduleName");
20+
if (moduleNameValue == null || moduleNameValue.asString().isEmpty()) {
21+
resourceBundle.produce(new NativeImageResourceBundleBuildItem(bundleNameValue.asString()));
22+
} else {
23+
resourceBundle.produce(new NativeImageResourceBundleBuildItem(bundleNameValue.asString(),
24+
moduleNameValue.asString()));
25+
}
26+
}
27+
}
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.quarkus.deployment.steps;
2+
3+
import org.jboss.jandex.DotName;
4+
5+
import io.quarkus.deployment.annotations.BuildProducer;
6+
import io.quarkus.deployment.annotations.BuildStep;
7+
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
8+
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourcePatternsBuildItem;
9+
import io.quarkus.runtime.annotations.RegisterResources;
10+
11+
public class RegisterResourcesBuildStep {
12+
13+
@BuildStep
14+
public void build(CombinedIndexBuildItem combinedIndexBuildItem,
15+
BuildProducer<NativeImageResourcePatternsBuildItem> resources) {
16+
for (var annotationInstance : combinedIndexBuildItem.getIndex()
17+
.getAnnotations(DotName.createSimple(RegisterResources.class.getName()))) {
18+
var builder = NativeImageResourcePatternsBuildItem.builder();
19+
var globsValue = annotationInstance.value("globs");
20+
if (globsValue != null) {
21+
builder.includeGlobs(globsValue.asStringArray());
22+
}
23+
resources.produce(builder.build());
24+
}
25+
}
26+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.quarkus.runtime.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Repeatable;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
/**
10+
* Annotation that can be used to register a resource bundle to be included in the native image.
11+
*/
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@Target(ElementType.TYPE)
14+
@Repeatable(RegisterResourceBundle.List.class)
15+
public @interface RegisterResourceBundle {
16+
17+
/**
18+
* The bundle name.
19+
*/
20+
String bundleName();
21+
22+
/**
23+
* The module name (optional).
24+
*/
25+
String moduleName() default "";
26+
27+
/**
28+
* The repeatable holder for {@link RegisterResourceBundle}.
29+
*/
30+
@Retention(RetentionPolicy.RUNTIME)
31+
@Target(ElementType.TYPE)
32+
@interface List {
33+
/**
34+
* The {@link RegisterResourceBundle} instances.
35+
*
36+
* @return the instances
37+
*/
38+
RegisterResourceBundle[] value();
39+
}
40+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.quarkus.runtime.annotations;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Repeatable;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
/**
10+
* Annotation that can be used to register resource files to be included in the native image.
11+
*/
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@Target(ElementType.TYPE)
14+
@Repeatable(RegisterResources.List.class)
15+
public @interface RegisterResources {
16+
17+
/**
18+
* Add an array of glob patterns for matching resource paths that should be added to the native image.
19+
* <p>
20+
* Use slash ({@code /}) as a path separator on all platforms. Globs must not start with slash.
21+
*/
22+
String[] globs() default {};
23+
24+
/**
25+
* The repeatable holder for {@link RegisterResources}.
26+
*/
27+
@Retention(RetentionPolicy.RUNTIME)
28+
@Target(ElementType.TYPE)
29+
@interface List {
30+
/**
31+
* The {@link RegisterResources} instances.
32+
*
33+
* @return the instances
34+
*/
35+
RegisterResources[] value();
36+
}
37+
}

docs/src/main/asciidoc/writing-native-applications-tips.adoc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,28 @@ public class MyReflectionConfiguration {
261261
Note that the order of the specified proxy interfaces is significant. For more information, see link:https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/reflect/Proxy.html[Proxy javadoc].
262262
====
263263

264+
=== Registering resource bundles and resource files
265+
266+
Resource bundles can be registered to be included in the native image by using `@RegisterResourceBundle`:
267+
268+
[source,java]
269+
----
270+
@RegisterResourceBundle(bundleName = "messages")
271+
@RegisterResourceBundle(bundleName = "errors", moduleName = "foo")
272+
public class NativeConfiguration {
273+
}
274+
----
275+
276+
Other resources files can be registered to be included in the native image by using `@RegisterResources`:
277+
278+
[source,java]
279+
----
280+
@RegisterResources(globs = ["path1/level*/**", "path2/level5/**"])
281+
@RegisterResources(globs = ["**.txt"])
282+
public class NativeConfiguration {
283+
}
284+
----
285+
264286
[[delay-class-init-in-your-app]]
265287
=== Delaying class initialization
266288

0 commit comments

Comments
 (0)