Skip to content

Commit 48106e7

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

File tree

5 files changed

+157
-0
lines changed

5 files changed

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

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)