从Maven Quarkus项目的资源文件夹中读取txt文件,来自Docker容器。

huangapple go评论98阅读模式
英文:

Read txt file from resources folder on maven Quarkus project From Docker Container

问题

  1. public void buffer() throws IOException {
  2. try (InputStream inputStream = getClass().getResourceAsStream("/151279.txt");
  3. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
  4. String contents = reader.lines()
  5. .collect(Collectors.joining(System.lineSeparator()));
  6. }
  7. }
  1. ./mvnw package
  2. java -jar target/file-parser-1.0-SNAPSHOT-runner.jar
  1. ./mvnw package -Pnative -Dquarkus.native.container-build=true
  2. java -jar target/file-parser-1.0-SNAPSHOT-native-image-source-jar/file-parser-1.0-SNAPSHOT-runner.jar
  1. docker build -f src/main/docker/Dockerfile.native -t quarkus/file-parser
  2. docker run -i --rm -p 8080:8080 quarkus/file-parser
  1. docker build -f src/main/docker/Dockerfile.jvm -t quarkus/file-parser-jvm
  2. docker run -i --rm -p 8080:8080 quarkus/file-parser-jvm

Dockerfile.Native:

  1. ####
  2. # This Dockerfile is used in order to build a container that runs the Quarkus application in native (no JVM) mode
  3. #
  4. # Before building the docker image run:
  5. #
  6. # mvn package -Pnative -Dquarkus.native.container-build=true
  7. #
  8. # Then, build the image with:
  9. #
  10. # docker build -f src/main/docker/Dockerfile.native -t quarkus/file-parser .
  11. #
  12. # Then run the container using:
  13. #
  14. # docker run -i --rm -p 8080:8080 quarkus/file-parser
  15. #
  16. ###
  17. FROM registry.access.redhat.com/ubi8/ubi-minimal:8.1
  18. WORKDIR /work/
  19. COPY target/*-runner /work/application
  20. # set up permissions for user `1001`
  21. RUN chmod 775 /work /work/application \
  22. && chown -R 1001 /work \
  23. && chmod -R "g+rwX" /work \
  24. && chown -R 1001:root /work
  25. EXPOSE 8080
  26. USER 1001
  27. CMD ["./application", "-Dquarkus.http.host=0.0.0.0"]

DockerFile.jvm:

  1. ####
  2. # This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode
  3. #
  4. # Before building the docker image run:
  5. #
  6. # mvn package
  7. #
  8. # Then, build the image with:
  9. #
  10. # docker build -f src/main/docker/Dockerfile.jvm -t quarkus/file-parser-jvm .
  11. #
  12. # Then run the container using:
  13. #
  14. # docker run -i --rm -p 8080:8080 quarkus/file-parser-jvm
  15. #
  16. ###
  17. FROM registry.access.redhat.com/ubi8/ubi-minimal:8.1
  18. ARG JAVA_PACKAGE=java-1.8.0-openjdk-headless
  19. ARG RUN_JAVA_VERSION=1.3.5
  20. ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en'
  21. # Install java and the run-java script
  22. # Also set up permissions for user `1001`
  23. RUN microdnf install openssl curl ca-certificates ${JAVA_PACKAGE} \
  24. && microdnf update \
  25. && microdnf clean all \
  26. && mkdir /deployments \
  27. && chown 1001 /deployments \
  28. && chmod "g+rwX" /deployments \
  29. && chown 1001:root /deployments \
  30. && curl https://repo1.maven.org/maven2/io/fabric8/run-java-sh/${RUN_JAVA_VERSION}/run-java-sh-${RUN_JAVA_VERSION}-sh.sh -o /deployments/run-java.sh \
  31. && chown 1001 /deployments/run-java.sh \
  32. && chmod 540 /deployments/run-java.sh \
  33. && echo "securerandom.source=file:/dev/urandom" >> /etc/alternatives/jre/lib/security/java.security
  34. # Configure the JAVA_OPTIONS, you can add -XshowSettings:vm to also display the heap size.
  35. ENV JAVA_OPTIONS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager"
  36. COPY target/lib/* /deployments/lib/
  37. COPY target/*-runner.jar /deployments/app.jar
  38. EXPOSE 8080
  39. USER 1001
  40. ENTRYPOINT ["/deployments/run-java.sh"]

Refs:
https://www.baeldung.com/java-classpath-resource-cannot-be-opened

  1. <details>
  2. <summary>英文:</summary>
  3. I have one project to parse some info from a large file.
  4. The project uses maven and java:
  5. [![enter image description here][1]][1]
  6. And the structure bellow:
  7. [![enter image description here][2]][2]
  8. [1]: https://i.stack.imgur.com/zELbF.png
  9. [2]: https://i.stack.imgur.com/tJi5i.png
  10. When I run the application from my IDEA, I can read the file with:
  11. public void buffer() throws IOException {
  12. try (InputStream inputStream = getClass().getResourceAsStream(&quot;/151279.txt&quot;);
  13. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
  14. String contents = reader.lines()
  15. .collect(Collectors.joining(System.lineSeparator()));
  16. }
  17. }
  18. Then, if I run:
  19. ./mvnw package
  20. java -jar target/file-parser-1.0-SNAPSHOT-runner.jar
  21. Everything goes well.
  22. Even when I generate the GraalNative jar and run the application from the native generate jar with:
  23. ./mvnw package -Pnative -Dquarkus.native.container-build=true
  24. java -jar target/file-parser-1.0-SNAPSHOT-native-image-source-jar/file-parser-1.0-SNAPSHOT-runner.jar
  25. it all works Well.
  26. But then, when I run the commands to build and run with docker, is where I got my error:
  27. docker build -f src/main/docker/Dockerfile.native -t quarkus/file-parser
  28. docker run -i --rm -p 8080:8080 quarkus/file-parser
  29. 2020-03-16 17:48:04,908 ERROR [io.qua.ver.htt.run.QuarkusErrorHandler] (executor-thread-1) HTTP Request to /init failed, error id: 8471ff6c-f124-4e0f-9d83-afe7f066b3a8-1: org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
  30. at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:106)
  31. at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:372)
  32. at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:209)
  33. at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:496)
  34. at org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:252)
  35. at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:153)
  36. at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:363)
  37. at org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:156)
  38. at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:238)
  39. at io.quarkus.resteasy.runtime.standalone.RequestDispatcher.service(RequestDispatcher.java:73)
  40. at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.dispatch(VertxRequestHandler.java:120)
  41. at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.access$000(VertxRequestHandler.java:36)
  42. at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler$1.run(VertxRequestHandler.java:85)
  43. at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
  44. at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2011)
  45. at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1535)
  46. at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1395)
  47. at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
  48. at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
  49. at java.lang.Thread.run(Thread.java:748)
  50. at org.jboss.threads.JBossThread.run(JBossThread.java:479)
  51. at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:460)
  52. at com.oracle.svm.core.posix.thread.PosixJavaThreads.pthreadStartRoutine(PosixJavaThreads.java:193)
  53. Caused by: java.lang.NullPointerException
  54. at java.io.Reader.&lt;init&gt;(Reader.java:78)
  55. at java.io.InputStreamReader.&lt;init&gt;(InputStreamReader.java:72)
  56. at com.erickmob.fileparser.service.ParseService.buffer(ParseService.java:74)
  57. at com.erickmob.fileparser.service.ParseService_ClientProxy.buffer(ParseService_ClientProxy.zig:98)
  58. at com.erickmob.fileparser.resource.ParseResource.hello(ParseResource.java:27)
  59. at java.lang.reflect.Method.invoke(Method.java:498)
  60. at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:151)
  61. at org.jboss.resteasy.core.MethodInjectorImpl.lambda$invoke$3(MethodInjectorImpl.java:122)
  62. at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:616)
  63. at java.util.concurrent.CompletableFuture.uniApplyStage(CompletableFuture.java:628)
  64. at java.util.concurrent.CompletableFuture.thenApply(CompletableFuture.java:1996)
  65. at java.util.concurrent.CompletableFuture.thenApply(CompletableFuture.java:110)
  66. at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:122)
  67. at org.jboss.resteasy.core.ResourceMethodInvoker.internalInvokeOnTarget(ResourceMethodInvoker.java:594)
  68. at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTargetAfterFilter(ResourceMethodInvoker.java:468)
  69. at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invokeOnTarget$2(ResourceMethodInvoker.java:421)
  70. at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:363)
  71. at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:423)
  72. at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:391)
  73. at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invoke$1(ResourceMethodInvoker.java:365)
  74. at java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:995)
  75. at java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2137)
  76. at java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:110)
  77. at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:365)
  78. at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:477)
  79. ... 19 more
  80. Does anyone can help me with this?
  81. How can I read a txt file on src/main/resources on a Docker Container?
  82. Dockerfile.Native:
  83. ####
  84. # This Dockerfile is used in order to build a container that runs the Quarkus application in native (no JVM) mode
  85. #
  86. # Before building the docker image run:
  87. #
  88. # mvn package -Pnative -Dquarkus.native.container-build=true
  89. #
  90. # Then, build the image with:
  91. #
  92. # docker build -f src/main/docker/Dockerfile.native -t quarkus/file-parser .
  93. #
  94. # Then run the container using:
  95. #
  96. # docker run -i --rm -p 8080:8080 quarkus/file-parser
  97. #
  98. ###
  99. FROM registry.access.redhat.com/ubi8/ubi-minimal:8.1
  100. WORKDIR /work/
  101. COPY target/*-runner /work/application
  102. # set up permissions for user `1001`
  103. RUN chmod 775 /work /work/application \
  104. &amp;&amp; chown -R 1001 /work \
  105. &amp;&amp; chmod -R &quot;g+rwX&quot; /work \
  106. &amp;&amp; chown -R 1001:root /work
  107. EXPOSE 8080
  108. USER 1001
  109. CMD [&quot;./application&quot;, &quot;-Dquarkus.http.host=0.0.0.0&quot;]
  110. DockerFile.jvm
  111. ####
  112. # This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode
  113. #
  114. # Before building the docker image run:
  115. #
  116. # mvn package
  117. #
  118. # Then, build the image with:
  119. #
  120. # docker build -f src/main/docker/Dockerfile.jvm -t quarkus/file-parser-jvm .
  121. #
  122. # Then run the container using:
  123. #
  124. # docker run -i --rm -p 8080:8080 quarkus/file-parser-jvm
  125. #
  126. ###
  127. FROM registry.access.redhat.com/ubi8/ubi-minimal:8.1
  128. ARG JAVA_PACKAGE=java-1.8.0-openjdk-headless
  129. ARG RUN_JAVA_VERSION=1.3.5
  130. ENV LANG=&#39;en_US.UTF-8&#39; LANGUAGE=&#39;en_US:en&#39;
  131. # Install java and the run-java script
  132. # Also set up permissions for user `1001`
  133. RUN microdnf install openssl curl ca-certificates ${JAVA_PACKAGE} \
  134. &amp;&amp; microdnf update \
  135. &amp;&amp; microdnf clean all \
  136. &amp;&amp; mkdir /deployments \
  137. &amp;&amp; chown 1001 /deployments \
  138. &amp;&amp; chmod &quot;g+rwX&quot; /deployments \
  139. &amp;&amp; chown 1001:root /deployments \
  140. &amp;&amp; curl https://repo1.maven.org/maven2/io/fabric8/run-java-sh/${RUN_JAVA_VERSION}/run-java-sh-${RUN_JAVA_VERSION}-sh.sh -o /deployments/run-java.sh \
  141. &amp;&amp; chown 1001 /deployments/run-java.sh \
  142. &amp;&amp; chmod 540 /deployments/run-java.sh \
  143. &amp;&amp; echo &quot;securerandom.source=file:/dev/urandom&quot; &gt;&gt; /etc/alternatives/jre/lib/security/java.security
  144. # Configure the JAVA_OPTIONS, you can add -XshowSettings:vm to also display the heap size.
  145. ENV JAVA_OPTIONS=&quot;-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager&quot;
  146. COPY target/lib/* /deployments/lib/
  147. COPY target/*-runner.jar /deployments/app.jar
  148. EXPOSE 8080
  149. USER 1001
  150. ENTRYPOINT [ &quot;/deployments/run-java.sh&quot; ]
  151. Refs:
  152. https://www.baeldung.com/java-classpath-resource-cannot-be-opened
  153. </details>
  154. # 答案1
  155. **得分**: 11
  156. 你需要确保该资源已包含在本地镜像中(默认情况下不包含)。
  157. 添加一个 `src/main/resources/resources-config.json` 文件,内容类似于:

{
"resources": [
{
"pattern": "151279\.txt$"
}
]
}

  1. 你还需要设置以下属性:

quarkus.native.additional-build-args =-H:ResourceConfigurationFiles=resources-config.json

  1. 详见[此链接][1]以获取更多详情。
  2. [1]: https://quarkus.io/guides/writing-native-applications-tips#including-resources
  3. <details>
  4. <summary>英文:</summary>
  5. You need to make sure that the resource is included in the native image (it isn&#39;t by default).
  6. Add a `src/main/resources/resources-config.json` that includes something like:
  7. {
  8. &quot;resources&quot;: [
  9. {
  10. &quot;pattern&quot;: &quot;151279\\.txt$&quot;
  11. }
  12. ]
  13. }
  14. You will also need to set the following property:
  15. quarkus.native.additional-build-args =-H:ResourceConfigurationFiles=resources-config.json
  16. See [this][1] for more details.
  17. [1]: https://quarkus.io/guides/writing-native-applications-tips#including-resources
  18. </details>

huangapple
  • 本文由 发表于 2020年3月17日 02:02:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/60711034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定