简体   繁体   中英

Quarkus Native Image Build Failure

I am attempting to build a native image on MacOS for an AWS Lambda function. The application compiles and runs just fine in the IDE, but I am hitting the following issue(s) when I try the native image build:

 mvn clean install -Dnative -Dquarkus.native.container-build=true
 mvn clean install -Pnative -Dquarkus.native.container-build=true
  1. With following in my pom.xml file, the build fails

    <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-amazon-lambda</artifactId> </dependency>

Error: Unsupported features in 4 methods Detailed message: Error: Detected an instance of Random/SplittableRandom class in the image heap. Instances created during image generation have cached seed values and don't behave as expected. To see how this object got instantiated use --trace-object-instantiation=java.util.Random. The object was probably created by a class initializer and is reachable from a static field. You can request class initialization at image runtime by using the option --initialize-at-run-time=. Or you can write your own initialization methods and call them explicitly from your main entry point. Trace: Object was reached by reading field com.amazonaws.retry.PredefinedBackoffStrategies$EqualJitterBackoffStrategy.random of constant com.amazonaws.retry.PredefinedBackoffStrategies$EqualJitterBackoffStrategy@a7c6c06 reached by reading field com.amazonaws.retry.PredefinedBackoffStrategies$SDKDefaultBackoffStrategy.equalJitterBackoffStrategy of constant com.amazonaws.retry.PredefinedBackoffStrategies$SDKDefaultBackoffStrategy@6f61d2d9 reached by scanning method com.amazonaws.retry.PredefinedRetryPolicies.getDefaultBackoffStrategy(PredefinedRetryPolicies.java:110)

  • If I change the pom file to the following

     <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-amazon-lambda-archetype</artifactId> <version>2.4.2.Final</version> </dependency>

the build succeeds without error, but the function.zip for example is not created in the /target directory.

[Content of the /target directory]

  • com_abc_demo_xyz-1.0-SNAPSHOT-native-image-source-jar
  • com_abc_demo_xyz-1.0-SNAPSHOT-runner
  • com_abc_demo_xyz-1.0-SNAPSHOT.jar
  • failsafe-reports
  • generated-sources
  • maven-archiver
  • maven-status
  • quarkus-app
  • quarkus-artifact.properties

[Test Environment]

  • Java Version 11.0.6
  • Docker Desktop 4.1.1
  • macOS Monterey
  • Maven 3.8.3
  • GraalVM: graalvm-ce-java11-21.3.0
  • JAVA_PATH=/Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home
  • GRAALVM_HOME=/Library/Java/JavaVirtualMachines/graalvm-ce-java11-21.3.0/Contents/Home

Even with the debug feature enabled it does not make sense to me how to resolve this matter.

Les

By default, Quarkus initializes all classes at build time.

This means that if you use Random/SplittableRandom in your code this will be initialized at build time and thus this error.

As mentioned above, this problem occurs when you try to initialize these classes in a static block like for instance:

public class RandomWrapper {

    public static final Random random;

    static {
        random = new Random();
    }
}

which fails with your error. However, as the exception tells you, you can specify which classes should be initialized at runtime with --initialize-at-run-time=<class-name> flag. In Quarkus, this can be done by specifying the following configuration in your application.properties :

quarkus.native.additional-build-args=--initialize-at-run-time=org.acme.RandomWrapper

It is possible to make this part of the build. I personally prefer yaml properties, so the first thing to add to the pom is the ability to use yaml properties:

        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-config-yaml</artifactId>
        </dependency>

then in your application.yaml in the src/main/resources add the commandline options you need, eg

quarkus:
  banner:
    enabled: false

  native:
    additional-build-args:
      - --initialize-at-run-time=io.grpc.netty.shaded.io.netty.util.internal.logging.Log4JLogger,io.netty.util.internal.logging.Log4JLogger
      - --allow-incomplete-classpath

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM