Les
Les

Reputation: 31

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)

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

[Content of the /target directory]

[Test Environment]

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

Les

Upvotes: 3

Views: 4523

Answers (2)

theINtoy
theINtoy

Reputation: 3658

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, e.g.

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

As for the S3 issue with the Random, you could look to the quarkiverse, on Maven Central Quarkiverse and their s3 module:

<dependency>
    <groupId>io.quarkiverse.amazonservices</groupId>
    <artifactId>quarkus-amazon-s3</artifactId>
    <version>1.0.5</version>
</dependency>

Upvotes: 1

xstefank
xstefank

Reputation: 499

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

Upvotes: 6

Related Questions