先上POM.XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<graalvm.version>22.3.1</graalvm.version>
<java.version>17</java.version>
<project.build.sourceEncoding>GBK</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.graalvm.sdk</groupId>
<artifactId>graal-sdk</artifactId>
<version>22.3.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jxl</groupId>
<artifactId>jxl</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jxl-2.6.12.jar</systemPath>
</dependency>
<dependency>
<groupId>com.oracle.substratevm</groupId>
<artifactId>native-image-maven-plugin</artifactId>
<version>19.2.1</version>
<type>maven-plugin</type>
</dependency>
</dependencies>
<!--build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.0.4</version>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build-->
<build>
<plugins>
<plugin>
<groupId>com.oracle.substratevm</groupId>
<artifactId>native-image-maven-plugin</artifactId>
<version>19.2.1</version>
<executions>
<execution>
<goals>
<goal>native-image</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<skip>false</skip>
<imageName>graalvmMaven</imageName>
<mainClass>test</mainClass>
<buildArgs>
-no-fallback
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
最后在目录下mvn clean package发生错误
Failed to execute goal com.oracle.substratevm:native-image-maven-plugin:19.2.1:native-image (default) on project test: Execution default of goal com.oracle.substratevm:native-image-maven-plugin:19.2.1:native-image failed: An API incompatibility was encountered while executing com.oracle.substratevm:native-image-maven-plugin:19.2.1:native-image: java.lang.IllegalAccessError: class org.graalvm.compiler.serviceprovider.JavaVersionUtil (in unnamed module @0x6b64429e) cannot access class jdk.vm.ci.services.Services (in module jdk.internal.vm.ci) because module jdk.internal.vm.ci does not export jdk.vm.ci.services to unnamed module @0x6b64429e
spring生成的jar可以正常跑
java -agentlib:native-image-agent=config-output-dir=META-INF/native-image -jar
target目录下生成一个META-INF的文件夹,这个文件夹中包含的内容就是使用跟踪代理生成的反射配置。
这些都可以跑,没问题
class org.graalvm.compiler.serviceprovider.JavaVersionUtil cannot access class jdk.vm.ci.services.Services这个不知道怎么办了
%JAVA_HOME%指向的是graalvm的地址:
C:\Program Files\Java\graalvm-ee-java17-22.3.1
因为参照的Demo是用的
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>native-image-maven-plugin</artifactId>
<version>21.2.0</version>
但阿里云的仓库没有,所以用了阿里云的
<groupId>com.oracle.substratevm</groupId>
<artifactId>native-image-maven-plugin</artifactId>
<version>19.2.1</version>
能正常打包就好
基于Monster 组和GPT的调写:
可能是因为 native-image-maven-plugin 版本与 GraalVM 版本不兼容导致的。建议更新 native-image-maven-plugin 版本至 21.2.0 或更高版本,因为在 POM.xml 中使用的版本是 19.2.1,而且它也是一个较旧的版本。
可以将以下内容添加到 中:
<plugin>
<groupId>org.graalvm.nativeimage</groupId>
<artifactId>native-image-maven-plugin</artifactId>
<version>21.2.0</version>
<executions>
<execution>
<goals>
<goal>native-image</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<mainClass>test</mainClass>
</configuration>
</plugin>
同时,如果想继续使用 com.oracle.substratevm:native-image-maven-plugin,可以尝试升级 GraalVM 到 21.2.0 或更高版本,因为这个插件的版本可能不支持所使用的 GraalVM 版本。