I started playing with quarkus and graalvm. I added files (txt and jpg) to resources in the project (src/main/resources/
). To be sure that I have access to this file in controller I display size of it:
URL url = Thread.currentThread().getContextClassLoader().getResource("/Resource2.txt");
File file = new File(url.toURI());
return "Hello My Friend! File size in bytes = " + file.length();
and when I run it with maven (mvn quarkus:dev
) it works. Controller code is here.
Problem occurred when I created native Quarkus application and try to run inside docker.
To be sure that file is included in native image, I added a big jpg file (3.3MB), created resources-config.json
:
{ "resources":
{ "includes": [
{ "pattern": "IMG_3_3M\.jpg$"},
{ "pattern": "Resources2\.txt$"}
]
}}
and in application.properties
added:
quarkus.native.additional-build-args = -H:ResourceConfigurationFiles=resources-config.json
. The native runner size was increased from:
39M Mar 21 12:48 hello-quarkus-1.0-SNAPSHOT-runner
- to:
44M Mar 21 12:19 hello-quarkus-1.0-SNAPSHOT-runner
So I assume that jpg file was included, but still when run native application inside docker image, I got NPE:
Caused by: java.lang.NullPointerException
at it.tostao.quickstart.GreetingResource.hello(GreetingResource.java:24)
where line 24: is url.toURI()
.
Any idea how I can read resources in native image? Is something missing in the configuration?
here is sample image to reproduce the problem, all commands needed to build and run native image you can find in README.MD:
https://github.com/sleski/hello-quarkus
So far I checked this urls and still was not able to find resources in native image:
• How to include classpath resources in a Quarkus native image?
• How to read classpath resources in Quarkus native image?
• https://quarkus.io/guides/writing-native-applications-tips
• Read txt file from resources folder on maven Quarkus project From Docker Container
2
Answers
This is how I solved the problem - example with changes are in this branch: https://github.com/sleski/hello-quarkus/tree/solution_for_native
Explanations:
Image is in:
src/main/resources/images
and name is:IMG_3_3M.jpg
. Inapplication.properties
I addedimages.location
variable:and in the java controller I added:
in
docker.native
added:COPY target/classes/images/*.jpg /work/images/
When I start application with
querkus:dev
it is getting image fromsrc/main/resources/images
and when I run narive image:/work/images
.In both cases work:
File size is = 3412177
.First fix json
or you can have *.txt as pattern. like in the doc
https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/Resources.md says use
when I tried I had issues. you can see the working code below for your project
GreetingResource.class.getResourceAsStream(resourcePath);
is actually bringing the resource here. I think this feature may change in the future so I left ModuleLayer in the code too. I used graalvm 17-21.3.0you can find the build log below