1. Install graalvm
tar -xzf graalvm-ce-java-linux–.tar.gz
export PATH=/path/to//bin:$PATH
export JAVA_HOME=/path/to/
2. Install native-image
https://docs.oracle.com/en/graalvm/enterprise/21/docs/reference-manual/native-image/BuildConfiguration/
https://gitee.com/westinyang/java-graalvm-start
gu install native-image
3. Precautions for using native-image
The memory of the machine should be slightly larger, otherwise this error will be reported: exit status 137
4. Use native-image to directly compile the jar of spring boot/spring cloud/spring cloud alibaba as an independent executable file. If it fails, give up:
Case 1: native-image does not add –no-fallback, the compiled executable file, at runtime, also depends on java and the original jar
Situation 2: But the executable file compiled with native-image –no-fallback -jar xxx.jar runs and reports this error: zip END header not found
So, please refer to the following to achieve:
5. Compile spring boot/spring cloud/spring cloud alibaba with maven
(4.1) Situation 1: Project inheritance spring-boot-starter-parent
In the pom.xml of the submodule, add the following content:
<plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> <configuration> <!--Executable file that runs independently --> <buildArgs> <arg>--no-fallback</arg> <!-- org.apache.poi: Caused by: java.nio.charset.UnsupportedCharsetException: CP1252 --> <!--<arg>-H: + AddAllCharsets</arg>--> </buildArgs> <executions> <execution> <id>add-reachability-metadata</id> <goals> <goal>add-reachability-metadata</goal> <!--Triggered when compiling, specially added --> <goal>compile</goal> </goals> </execution> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <jvmArguments> -agentlib:native-image-agent=config-merge-dir=src/main/resources/META-INF/native-image/ </jvmArguments> </configuration> </plugin>
Operation mode one:
Because profile native is defined in spring-boot-starter-parent, you can use:
(1) Execute mvn package in the project root directory
(2) Then cd to the submodule and execute: mvn -Pnative native:compile
(4.2) Situation 2: The project does not inherit spring-boot-starter-parent, but uses spring-boot-dependencies
<dependencyManagement> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencyManagement>
Reference: spring-boot-starter-parent, copy the relevant content, and delete some dependencies that are not used by your own project.
<build> <resources> <resource> <directory>${basedir}/src/main/resources</directory> <filtering>true</filtering> <includes> <include>**/application*.yml</include> <include>**/application*.yaml</include> <include>**/application*.properties</include> </includes> </resource> <resource> <directory>${basedir}/src/main/resources</directory> <excludes> <exclude>**/application*.yml</exclude> <exclude>**/application*.yaml</exclude> <exclude>**/application*.properties</exclude> </excludes> </resource> </resources> <plugin Management> <plugins> <!--refer to spring cloud alibaba--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <inherited>true</inherited> <configuration> <parameters>true</parameters> <source>17</source> <target>17</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>${start-class}</mainClass> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <propertiesEncoding>${project.build.sourceEncoding}</propertiesEncoding> <delimiters> <delimiter>${resource.delimiter}</delimiter> </delimiters> <useDefaultDelimiters>false</useDefaultDelimiters> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> <configuration> <mainClass>${start-class}</mainClass> </configuration> </plugin> <plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> <extensions>true</extensions> </plugin> </plugins> </pluginManagement> </build> <profiles> <profile> <id>prod</id> <build> <plugin Management> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <jvmArguments> -agentlib:native-image-agent=config-merge-dir=src/main/resources/META-INF/native-image/ </jvmArguments> <image> <builder>paketobuildpacks/builder:tiny</builder> <env> <BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE> </env> </image> </configuration> <executions> <execution> <id>process-aot</id> <goals> <goal>process-aot</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> <configuration> <!--Executable file that runs independently --> <buildArgs> <arg>--no-fallback</arg> <!-- org.apache.poi: Caused by: java.nio.charset.UnsupportedCharsetException: CP1252 --> <!--<arg>-H: + AddAllCharsets</arg>--> </buildArgs> <classesDirectory>${project.build.outputDirectory}</classesDirectory> <metadataRepository> <enabled>true</enabled> </metadataRepository> <requiredVersion>22.3</requiredVersion> </configuration> <executions> <execution> <id>add-reachability-metadata</id> <goals> <goal>add-reachability-metadata</goal> <!--triggered when compile--> <goal>compile</goal> </goals> </execution> </executions> </plugin> </plugins> </pluginManagement> </build> </profile> </profiles>
In the submodule, add:
<plugins> <plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins>
run:
(1) If you do not want to generate a native image, execute:
mvn package
(2) If you want to generate a native image, execute:
mvn package -P prod
The advantage of this is: there is no need to enter a module separately to execute mvn -P native native:compile
More references:
https://cn.dubbo.apache.org/zh-cn/docs/references/graalvm/support-graalvm/
https://docs.oracle.com/zh-cn/learn/understanding-reflection-graalvm-native-image/index.html#step-4-introducing-native-image-reflection-config
https://cn.dubbo.apache.org/zh-cn/overview/mannual/java-sdk/advanced-features-and-usage/performance/support-graalvm/ can be compiled successfully. With dubbo 3.2.2