当前位置: 首页 > 知识库问答 >
问题:

如何访问Quarkus本机映像中的资源?

罗寒
2023-03-14

我开始玩quarkus和graalvm。我将文件(txt和jpg)添加到项目中的资源中(src/main/resources/)。为了确保我可以在控制器中访问该文件,我显示了该文件的大小:

URL url = Thread.currentThread().getContextClassLoader().getResource("/Resource2.txt");
File file = new File(url.toURI());
return "Hello My Friend! File size in bytes = " + file.length();

当我用maven(mvn-quarkus:dev)运行它时,它可以工作。控制器代码在这里。

当我创建本机Quarkus应用程序并尝试在docker中运行时出现了问题。为了确保该文件包含在本机映像中,我添加了一个大的jpg文件(3.3MB),创建了resources-config.json

{ "resources": 
 { "includes": [
        { "pattern": "IMG_3_3M\\.jpg$"},
        { "pattern": "Resources2\\.txt$"}
       ]
}}

并在application.properties中添加:quarkus.native.additional-build-args=-H:ResourceConfigurationFiles=resources-config.json。本机运行程序大小从以下位置增加:

  • 39米3月21日12:48 hello-quarkus-1.0-SNAPSHOT-runner
  • 收件人:44M Mar 21 12:19 hello-quarkus-1.0-SNAPSHOT-runner

所以我假设包含了jpg文件,但当在docker映像中运行本机应用程序时,我得到了NPE:

 Caused by: java.lang.NullPointerException
  at it.tostao.quickstart.GreetingResource.hello(GreetingResource.java:24)

其中第24行:是url.toURI()

知道我如何阅读本机图像中的资源吗?配置中是否缺少某些内容?

这是重现问题的示例图像,构建和运行本机图像所需的所有命令都可以在README. MD中找到:

https://github.com/sleski/hello-quarkus

到目前为止,我检查了这个网址,仍然无法在本地图像中找到资源:

•如何在Quarkus本机映像中包含类路径资源?

•如何读取Quarkus原生映像中的类路径资源?

• https://quarkus.io/guides/writing-native-applications-tips

从Docker容器中的maven Quarkus项目的资源文件夹中读取txt文件


共有1个答案

杨超
2023-03-14

首先修复json

{
    "resources": [
      {
        "pattern": "Resource2.txt"
      }
    ]
}

或者你可以有*。txt作为图案。就像医生说的那样

https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/Resources.md表示使用

InputStream resource = ModuleLayer.boot().findModule(moduleName).getResourceAsStream(resourcePath);

当我尝试时,我有问题。您可以在下面看到项目的工作代码

@Path("/hello")
public class GreetingResource {


    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() throws IOException {
        String moduleName = "java.base";
        String resourcePath = "/Resource2.txt";
        Module resource = ModuleLayer.boot().findModule(moduleName).get();
        InputStream ins = resource.getResourceAsStream(resourcePath);
        if (ins == null) {
            System.out.println("module came empty, now trying to load from GreetingResource");
            ins = GreetingResource.class.getResourceAsStream(resourcePath);
        }
        if (ins != null) {
            StringBuilder sb = new StringBuilder();
            for (int ch; (ch = ins.read()) != -1; ) {
                sb.append((char) ch);
            }
            return "Hello My Friend! File size in bytes = " + sb;
        }
        return "empty";
    }

}

<代码>问候资源。班getResourceAsStream(资源路径) 实际上将资源带到了这里。我认为这个特性将来可能会改变,所以我在代码中也留下了ModuleLayer。我使用了graalvm 17-21.3.0

您可以在下面找到构建日志

[INFO] [io.quarkus.deployment.pkg.steps.NativeImageBuildRunner] C:\Program Files\GraalVM\graalvm-ce-java17-21.3.0\bin\native-image.cmd -J-Dsun.nio.ch.maxUpdateArraySize=100 -J-Djava.util.logging.manager=org.jboss.logmanager.LogManager -J-Dvertx.logger-delegate-factory-class-name=io.quarkus.vertx.core.runtime.VertxLogDelegateFactory -J-Dvertx.disableDnsResolver=true -J-Dio.netty.leakDetection.level=DISABLED -J-Dio.netty.allocator.maxOrder=3 -J-Duser.language=en -J-Duser.country=GB -J-Dfile.encoding=UTF-8 -H:-ParseOnce -J--add-exports=java.security.jgss/sun.security.krb5=ALL-UNNAMED -J--add-opens=java.base/java.text=ALL-UNNAMED -H:ResourceConfigurationFiles=resources-config.json -H:+PrintAnalysisCallTree -H:Log=registerResource:verbose -H:InitialCollectionPolicy=com.oracle.svm.core.genscavenge.CollectionPolicy\$BySpaceAndTime -H:+JNI -H:+AllowFoldMethods -J-Djava.awt.headless=true -H:FallbackThreshold=0 -H:+ReportExceptionStackTraces -H:-AddAllCharsets -H:EnableURLProtocols=http -H:-UseServiceLoaderFeature -H:+StackTrace -J--add-exports=java.management/sun.management=ALL-UNNAMED hello-quarkus-1.0-SNAPSHOT-runner -jar hello-quarkus-1.0-SNAPSHOT-runner.jar
[hello-quarkus-1.0-SNAPSHOT-runner:20428]    classlist:   2,920.35 ms,  0.94 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428]        (cap):   1,493.84 ms,  0.94 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428]        setup:   2,871.07 ms,  0.94 GB
[Use -Dgraal.LogFile=<path> to redirect Graal log output to a file.]
[thread:1] scope: main
  [thread:1] scope: main.registerResource
  ResourcesFeature: registerResource: Resource2.txt
14:23:38,709 INFO  [org.jbo.threads] JBoss Threads version 3.4.2.Final
  [thread:1] scope: main.registerResource
  ResourcesFeature: registerResource: java/lang/uniName.dat
[hello-quarkus-1.0-SNAPSHOT-runner:20428]     (clinit):     475.20 ms,  5.14 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428]   (typeflow):   2,931.83 ms,  5.14 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428]    (objects):  24,294.27 ms,  5.14 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428]   (features):   2,979.07 ms,  5.14 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428]     analysis:  32,083.24 ms,  5.14 GB
# Printing call tree to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\call_tree_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142406.txt
# Printing list of used methods to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\used_methods_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142407.txt
# Printing list of used classes to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\used_classes_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142407.txt
# Printing list of used packages to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\used_packages_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142407.txt
# Printing call tree for vm entry point to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\csv_call_tree_vm_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142408.csv
# Printing call tree for methods to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\csv_call_tree_methods_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142408.csv
# Printing call tree for virtual methods to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\csv_call_tree_virtual_methods_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142408.csv
# Printing call tree for entry points to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\csv_call_tree_entry_points_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142408.csv
# Printing call tree for direct edges to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\csv_call_tree_direct_edges_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142408.csv
# Printing call tree for overriden by edges to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\csv_call_tree_override_by_edges_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142408.csv
# Printing call tree for virtual edges to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\reports\csv_call_tree_virtual_edges_hello-quarkus-1.0-SNAPSHOT-runner_20220324_142408.csv
[hello-quarkus-1.0-SNAPSHOT-runner:20428]     universe:   1,547.28 ms,  5.14 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428]      (parse):   4,919.32 ms,  4.87 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428]     (inline):   7,013.78 ms,  5.83 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428]    (compile):  27,387.04 ms,  5.56 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428]      compile:  41,595.59 ms,  5.56 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428]        image:   2,515.22 ms,  5.56 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428]        write:     858.79 ms,  5.56 GB
[hello-quarkus-1.0-SNAPSHOT-runner:20428]      [total]:  90,068.97 ms,  5.56 GB
# Printing build artifacts to: C:\Users\ozkan\tmp\hello-quarkus\target\hello-quarkus-1.0-SNAPSHOT-native-image-source-jar\hello-quarkus-1.0-SNAPSHOT-runner.build_artifacts.txt
[INFO] [io.quarkus.deployment.QuarkusAugmentor] Quarkus augmentation completed in 94323ms
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:37 min
[INFO] Finished at: 2022-03-24T14:24:56Z
[INFO] ------------------------------------------------------------------------
 类似资料:
  • 我尝试过这些变体,但同样返回null quuarkus 0.13.3和0.14.0版本和Graal rc14、rc15、rc16版本

  • 我已经成功地用Quarkus/Graal构建了一个本机映像,我可以在终端中运行它(还没有容器)。但是,它会因“分段错误(堆芯转储)”消息而崩溃。如何获得有关错误的更多信息?是否有标志要传递给本机可执行文件或在构建映像时传递? 我使用的是GraalVM版本20.2.0(Java版本11.0.8) 任何帮助都是非常感激的。 谢谢

  • 我想使用“Image Asset Studio”操作我的应用程序的图标,通常我按照以下说明开始操作: 1-在Project窗口中,选择Android视图。 2-右键单击res文件夹并选择New>Image Asset... Android Studio 3.5 Build#AI-191.8026.42.35.5791312,2019年8月9日构建JRE:1.8.0_202-Release-1483

  • 我正在试图弄清楚如何通过将GraalVM本机映像代理与Quarkus应用程序一起使用。 我试着跑步: 但是我得到了一个错误: 有什么提示吗? 我的Java版本:

  • 当我建立一个本土形象时,我面临着一个反思的问题 通用域名格式。神谕svm。主持。代替DeletedElementException:不支持的java字段。朗,同学们。newInstanceCallerCache是可访问的:此元素的声明类已被替换,但替换类中不存在此元素 我该怎么修?我的环境GraalVM 20和Zulu OpenJdk11 我的pom

  • Quarkus文档解释了如何使用Maven构建docker映像。但我在利用Gradle和Kotlin。我正在想办法做这些步骤: 首先,我想我想要一个包含gradle而不是maven的图像(Quay.io似乎没有)。然后,我必须考虑我还需要复制什么(而不是pom.xml),也许???其次,我相信这个图像也包含了GraalVM。所以我不确定我能在那里做些什么。 我必须使用Maven吗?我真的真的不想。