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

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

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

问题

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

Dockerfile.Native:

####
# This Dockerfile is used in order to build a container that runs the Quarkus application in native (no JVM) mode
#
# Before building the docker image run:
#
# mvn package -Pnative -Dquarkus.native.container-build=true
#
# Then, build the image with:
#
# docker build -f src/main/docker/Dockerfile.native -t quarkus/file-parser .
#
# Then run the container using:
#
# docker run -i --rm -p 8080:8080 quarkus/file-parser
#
###
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.1
WORKDIR /work/
COPY target/*-runner /work/application

# set up permissions for user `1001`
RUN chmod 775 /work /work/application \
  && chown -R 1001 /work \
  && chmod -R "g+rwX" /work \
  && chown -R 1001:root /work

EXPOSE 8080
USER 1001

CMD ["./application", "-Dquarkus.http.host=0.0.0.0"]

DockerFile.jvm:

####
# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode
#
# Before building the docker image run:
#
# mvn package
#
# Then, build the image with:
#
# docker build -f src/main/docker/Dockerfile.jvm -t quarkus/file-parser-jvm .
#
# Then run the container using:
#
# docker run -i --rm -p 8080:8080 quarkus/file-parser-jvm
#
###
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.1

ARG JAVA_PACKAGE=java-1.8.0-openjdk-headless
ARG RUN_JAVA_VERSION=1.3.5

ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en'

# Install java and the run-java script
# Also set up permissions for user `1001`
RUN microdnf install openssl curl ca-certificates ${JAVA_PACKAGE} \
    && microdnf update \
    && microdnf clean all \
    && mkdir /deployments \
    && chown 1001 /deployments \
    && chmod "g+rwX" /deployments \
    && chown 1001:root /deployments \
    && 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 \
    && chown 1001 /deployments/run-java.sh \
    && chmod 540 /deployments/run-java.sh \
    && echo "securerandom.source=file:/dev/urandom" >> /etc/alternatives/jre/lib/security/java.security

# Configure the JAVA_OPTIONS, you can add -XshowSettings:vm to also display the heap size.
ENV JAVA_OPTIONS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager"

COPY target/lib/* /deployments/lib/
COPY target/*-runner.jar /deployments/app.jar

EXPOSE 8080
USER 1001

ENTRYPOINT ["/deployments/run-java.sh"]

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


<details>
<summary>英文:</summary>
I have one project to parse some info from a large file. 
The project uses maven and java:
[![enter image description here][1]][1]
And the structure bellow:
[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/zELbF.png
[2]: https://i.stack.imgur.com/tJi5i.png
When I run the application from my IDEA, I can read the file with:
public  void buffer() throws IOException {
try (InputStream inputStream = getClass().getResourceAsStream(&quot;/151279.txt&quot;);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String contents = reader.lines()
.collect(Collectors.joining(System.lineSeparator()));
}
}
Then, if I run:
./mvnw package 
java -jar target/file-parser-1.0-SNAPSHOT-runner.jar
Everything goes well.
Even when I generate the GraalNative jar and run the application from the native generate jar  with:
./mvnw package -Pnative -Dquarkus.native.container-build=true
java -jar target/file-parser-1.0-SNAPSHOT-native-image-source-jar/file-parser-1.0-SNAPSHOT-runner.jar
it all works Well.
But then, when I run the commands to build and run with docker, is where I got my error:
docker build -f src/main/docker/Dockerfile.native -t quarkus/file-parser 
docker run -i --rm -p 8080:8080 quarkus/file-parser
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
at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:106)
at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:372)
at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:209)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:496)
at org.jboss.resteasy.core.SynchronousDispatcher.lambda$invoke$4(SynchronousDispatcher.java:252)
at org.jboss.resteasy.core.SynchronousDispatcher.lambda$preprocess$0(SynchronousDispatcher.java:153)
at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:363)
at org.jboss.resteasy.core.SynchronousDispatcher.preprocess(SynchronousDispatcher.java:156)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:238)
at io.quarkus.resteasy.runtime.standalone.RequestDispatcher.service(RequestDispatcher.java:73)
at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.dispatch(VertxRequestHandler.java:120)
at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler.access$000(VertxRequestHandler.java:36)
at io.quarkus.resteasy.runtime.standalone.VertxRequestHandler$1.run(VertxRequestHandler.java:85)
at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2011)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1535)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1395)
at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
at java.lang.Thread.run(Thread.java:748)
at org.jboss.threads.JBossThread.run(JBossThread.java:479)
at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:460)
at com.oracle.svm.core.posix.thread.PosixJavaThreads.pthreadStartRoutine(PosixJavaThreads.java:193)
Caused by: java.lang.NullPointerException
at java.io.Reader.&lt;init&gt;(Reader.java:78)
at java.io.InputStreamReader.&lt;init&gt;(InputStreamReader.java:72)
at com.erickmob.fileparser.service.ParseService.buffer(ParseService.java:74)
at com.erickmob.fileparser.service.ParseService_ClientProxy.buffer(ParseService_ClientProxy.zig:98)
at com.erickmob.fileparser.resource.ParseResource.hello(ParseResource.java:27)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:151)
at org.jboss.resteasy.core.MethodInjectorImpl.lambda$invoke$3(MethodInjectorImpl.java:122)
at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:616)
at java.util.concurrent.CompletableFuture.uniApplyStage(CompletableFuture.java:628)
at java.util.concurrent.CompletableFuture.thenApply(CompletableFuture.java:1996)
at java.util.concurrent.CompletableFuture.thenApply(CompletableFuture.java:110)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:122)
at org.jboss.resteasy.core.ResourceMethodInvoker.internalInvokeOnTarget(ResourceMethodInvoker.java:594)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTargetAfterFilter(ResourceMethodInvoker.java:468)
at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invokeOnTarget$2(ResourceMethodInvoker.java:421)
at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:363)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:423)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:391)
at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invoke$1(ResourceMethodInvoker.java:365)
at java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:995)
at java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2137)
at java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:110)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:365)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:477)
... 19 more
Does anyone can help me with this?
How can I read a txt file on src/main/resources on a Docker Container?
Dockerfile.Native:
####
# This Dockerfile is used in order to build a container that runs the Quarkus application in native (no JVM) mode
#
# Before building the docker image run:
#
# mvn package -Pnative -Dquarkus.native.container-build=true
#
# Then, build the image with:
#
# docker build -f src/main/docker/Dockerfile.native -t quarkus/file-parser .
#
# Then run the container using:
#
# docker run -i --rm -p 8080:8080 quarkus/file-parser
#
###
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.1
WORKDIR /work/
COPY target/*-runner /work/application
# set up permissions for user `1001`
RUN chmod 775 /work /work/application \
&amp;&amp; chown -R 1001 /work \
&amp;&amp; chmod -R &quot;g+rwX&quot; /work \
&amp;&amp; chown -R 1001:root /work
EXPOSE 8080
USER 1001
CMD [&quot;./application&quot;, &quot;-Dquarkus.http.host=0.0.0.0&quot;]
DockerFile.jvm
####
# This Dockerfile is used in order to build a container that runs the Quarkus application in JVM mode
#
# Before building the docker image run:
#
# mvn package
#
# Then, build the image with:
#
# docker build -f src/main/docker/Dockerfile.jvm -t quarkus/file-parser-jvm .
#
# Then run the container using:
#
# docker run -i --rm -p 8080:8080 quarkus/file-parser-jvm
#
###
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.1
ARG JAVA_PACKAGE=java-1.8.0-openjdk-headless
ARG RUN_JAVA_VERSION=1.3.5
ENV LANG=&#39;en_US.UTF-8&#39; LANGUAGE=&#39;en_US:en&#39;
# Install java and the run-java script
# Also set up permissions for user `1001`
RUN microdnf install openssl curl ca-certificates ${JAVA_PACKAGE} \
&amp;&amp; microdnf update \
&amp;&amp; microdnf clean all \
&amp;&amp; mkdir /deployments \
&amp;&amp; chown 1001 /deployments \
&amp;&amp; chmod &quot;g+rwX&quot; /deployments \
&amp;&amp; chown 1001:root /deployments \
&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 \
&amp;&amp; chown 1001 /deployments/run-java.sh \
&amp;&amp; chmod 540 /deployments/run-java.sh \
&amp;&amp; echo &quot;securerandom.source=file:/dev/urandom&quot; &gt;&gt; /etc/alternatives/jre/lib/security/java.security
# Configure the JAVA_OPTIONS, you can add -XshowSettings:vm to also display the heap size.
ENV JAVA_OPTIONS=&quot;-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager&quot;
COPY target/lib/* /deployments/lib/
COPY target/*-runner.jar /deployments/app.jar
EXPOSE 8080
USER 1001
ENTRYPOINT [ &quot;/deployments/run-java.sh&quot; ]
Refs:
https://www.baeldung.com/java-classpath-resource-cannot-be-opened
</details>
# 答案1
**得分**: 11
你需要确保该资源已包含在本地镜像中(默认情况下不包含)。
添加一个 `src/main/resources/resources-config.json` 文件,内容类似于:

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


你还需要设置以下属性:

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


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

确定