Skip to content

Commit fedb4ff

Browse files
committed
Introduce @RegisterResourceBundle and @RegisterResources
These annotations are used to register resources in native image
1 parent 587ee70 commit fedb4ff

File tree

5 files changed

+160
-0
lines changed

5 files changed

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

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,27 @@ 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(includePatterns = {"^resource-file.txt$", ".*/Resource.*txt$"}, excludePatterns = {".*/Resource2.txt$"})
281+
public class NativeConfiguration {
282+
}
283+
----
284+
264285
[[delay-class-init-in-your-app]]
265286
=== Delaying class initialization
266287

0 commit comments

Comments
 (0)