带有 micronaut 和 aws sdk-v2 的 GraalVM 本机图像

GraalVM native image with micronaut and aws sdk-v2

我在使用 micronaut 和 aws sdk-v2 构建 GraalVM 本机映像时遇到问题。在详细说明错误之前,我想指出,在使用 sdk-v2 之前,本机图像已正确构建。

一旦我更新到 aws sdk-v2(通过 micronaut 依赖为):

...
implementation "io.micronaut.aws:micronaut-aws-sdk-v2:2.0.2"
...

我在尝试构建图像时遇到错误

Fatal error:java.lang.NoSuchMethodError
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at java.base/java.util.concurrent.ForkJoinTask.getThrowableException(ForkJoinTask.java:603)
    at java.base/java.util.concurrent.ForkJoinTask.get(ForkJoinTask.java:1006)
    at com.oracle.svm.hosted.NativeImageGenerator.run(NativeImageGenerator.java:480)
    at com.oracle.svm.hosted.NativeImageGeneratorRunner.buildImage(NativeImageGeneratorRunner.java:349)
    at com.oracle.svm.hosted.NativeImageGeneratorRunner.build(NativeImageGeneratorRunner.java:508)
    at com.oracle.svm.hosted.NativeImageGeneratorRunner.main(NativeImageGeneratorRunner.java:114)
    at com.oracle.svm.hosted.NativeImageGeneratorRunner$JDK9Plus.main(NativeImageGeneratorRunner.java:537)
Caused by: java.lang.NoSuchMethodError: 'void io.micronaut.core.graal.AutomaticFeatureUtils.initializeAtBuildTime(org.graalvm.nativeimage.hosted.Feature$BeforeAnalysisAccess, java.lang.String)'
    at io.micronaut.aws.sdk.v2.graal.AwsSdkAutomaticFeature.beforeAnalysis(AwsSdkAutomaticFeature.java:41)
    at com.oracle.svm.hosted.NativeImageGenerator.lambda$runPointsToAnalysis(NativeImageGenerator.java:693)
    at com.oracle.svm.hosted.FeatureHandler.forEachFeature(FeatureHandler.java:70)
    at com.oracle.svm.hosted.NativeImageGenerator.runPointsToAnalysis(NativeImageGenerator.java:693)
    at com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:555)
    at com.oracle.svm.hosted.NativeImageGenerator.lambda$run[=11=](NativeImageGenerator.java:468)
    at java.base/java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1407)
    at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
    at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
    at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
    at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
    at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)
Error: Image build request failed with exit status 1
com.oracle.svm.driver.NativeImage$NativeImageError: Image build request failed with exit status 1
    at com.oracle.svm.driver.NativeImage.showError(NativeImage.java:1558)
    at com.oracle.svm.driver.NativeImage.build(NativeImage.java:1308)
    at com.oracle.svm.driver.NativeImage.performBuild(NativeImage.java:1269)
    at com.oracle.svm.driver.NativeImage.main(NativeImage.java:1228)
    at com.oracle.svm.driver.NativeImage$JDK9Plus.main(NativeImage.java:1740)
...

我的假设是 micronaut 必须在内部进行一些反射,这会导致构建过程失败。 我添加了以下 json 文件来设置反射配置

[
  {
    "name" : "io.micronaut.core.graal.AutomaticFeatureUtils",
    "allDeclaredConstructors" : true,
    "allPublicConstructors" : true,
    "allDeclaredMethods" : true,
    "allPublicMethods" : true
  },
  {
    "name":"io.micronaut.core.graal.AutomaticFeatureUtils",
    "methods":[{"name":"initializeAtBuildTime","parameterTypes":["org.graalvm.nativeimage.hosted.Feature$BeforeAnalysisAccess","java.lang.String[]"] }]
  }
]

但是错误仍然存​​在。不确定是否是因为 AutomaticFeatureUtils.initializeAtBuildTime 需要字符串的可变参数,但是在堆栈跟踪中我只能看到一个字符串被传递,或者错误不是完全与反射有关。

无论我如何尝试,构建图像时都找不到该方法。

知道我在这里可能遗漏了什么吗?

谢谢!!

此错误与反射无关。

Feature 和 AutomaticFeature 是 GraalVM 本机图像概念,允许对本机图像进行编程配置,更多详细信息可以在 javadoc 中找到,例如:https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/hosted/Feature.java#L59

您显示的错误发生在图像生成过程中,这是一个正常的java过程,不需要配置反射等。这些是运行时行为的配置。

错误说的是 AwsSdkAutomaticFeature (source tries to call void io.micronaut.core.graal.AutomaticFeatureUtils.initializeAtBuildTime(org.graalvm.nativeimage.hosted.Feature$BeforeAnalysisAccess, java.lang.String) but there's no such method. Most probable cause is the version mismatch between the dependency and the core micronaut library that contain graal support.