This is the 9th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Native-image is built natively

If you have Graal VM installed locally, you can do this directly in the project directory:

mvn clean package -Pnative
Copy the code

The build takes a long time, and when the build is complete, a binary execution file is generated in the./target directory. The common name is quarkus-demo-1.0-runner, and the binary is executed directly to run the project.

➜ target: . / quarkus - demo - 1.0 - runner __ ____ ____ _____ _____ ____ _____ ______ - / __ / / / / / _ | / _ \ / / / _ / / / / __ / / / _ _ - / / / / / __ | /, _ /, / / __ / / \ \ - \ _ \ _ \ ____ _ / | _ / _ / | _ / _ / _ - | | \ _____ / ___ / 2021-07-09 16:54:10, 812 INFO/IO. Quarkus (main) quarkus - demo 1.0 Native (powered by Quarkus 2.0.1.Final) started in 0.121s. Listening on: http://0.0.0.0:8080 2021-07-09 16:54:11,041 INFO [IO. Quarkus] (main) Profile Prod Activated. 2021-07-09 16:54:11,041 INFO [io.quarkus] (main) Installed features: [cdi, resteasy, Smallrye - Context - Propagation] ^ c2021-07-09 16:55:propagation] [IO. Quarkus] (Shutdown thread) Quarkus -demo stopped in 0.008 sCopy the code

Container builds native-image

If you do not have Graal VM installed locally, Quarkus provides a base image to build: quay. IO/Quarkus/bi-quarkus-native-image

mvn clean package -Pnative -Dquarkus.native.container-build=true
Copy the code

Problems with container construction

1. The memory is insufficient during the build

When using a container to build a native-image, the following error may be reported:

[ERROR] Failed to execute goal IO. Quarkus :quarkus-maven-plugin:2.0.1.Final:build (default) on project quarkus-demo: Failed to build quarkus application: io.quarkus.builder.BuildException: Build failure: Build failed due to errors [ERROR] [error]: Build step io.quarkus.deployment.pkg.steps.NativeImageBuildStep#build threw an exception: java.lang.RuntimeException: Failed to build native image [ERROR] at io.quarkus.deployment.pkg.steps.NativeImageBuildStep.build(NativeImageBuildStep.java:223) [ERROR] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [ERROR] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [ERROR] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [ERROR] at java.base/java.lang.reflect.Method.invoke(Method.java:566) [ERROR] at io.quarkus.deployment.ExtensionLoader$2.execute(ExtensionLoader.java:820) [ERROR] at io.quarkus.builder.BuildContext.run(BuildContext.java:277) [ERROR] at org.jboss.threads.ContextHandler$1.runWith(ContextHandler.java:18) [ERROR] at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2442) [ERROR] at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1476) [ERROR] at java.base/java.lang.Thread.run(Thread.java:829) [ERROR] at org.jboss.threads.JBossThread.run(JBossThread.java:501) [ERROR] Caused by: java.lang.RuntimeException: Image generation failed. Exit code was 137 which indicates an out of memory error. Consider increasing the Xmx value for  native image generation by setting the "quarkus.native.native-image-xmx" property [ERROR] at io.quarkus.deployment.pkg.steps.NativeImageBuildStep.imageGenerationFailed(NativeImageBuildStep.java:360) [ERROR] at io.quarkus.deployment.pkg.steps.NativeImageBuildStep.build(NativeImageBuildStep.java:200) [ERROR] ... 11 more [ERROR] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionExceptionCopy the code

When the error message is out of memory, you can try to set the quarkus.native. Native image-xmx parameter, but the error is still reported after setting this parameter. After looking up the information, I saw the following sentence in a StackOverflow answer:

 Pay attention it has to be less than the memory you set in your docker daemon.
Copy the code

He says it must be smaller than the memory you set in the Docker daemon.

Then I looked at docker’s official documentation and found that the default memory on Mac and Windows is 2GB.

Therefore, as long as the value is increased, 8GB is recommended. Each system setting method:

Mac:docs.docker.com/docker-for-…

Windows:docs.docker.com/docker-for-…

2. The binaries after the container is built cannot run

ZSH: exec format error:./quarkus-demo-1.0-runner

This is because my native system is The Mac system, but the build image provided by Quarkus is the Linux system, but Graal VM does not seem to support cross-compilation at present, so the binary files built in the Linux system can only be executed in the Linux system.

So we can run the built binary directly with the container.