使用Maven构建GraalVM Native Image失败:找不到AgentOptionsConfiguration中的'option'
GraalVM Native Image构建失败:AgentOptionsConfiguration找不到'option'字段
问题场景
在Windows 11系统上,遵循GraalVM Native Image Maven插件官方文档构建Java应用Native Image。mvn clean package和mvn -Pnative -Dagent exec:exec@java-agent执行正常,但运行mvn -DskipTests=true -Pnative -Dagent package时失败,报错:Cannot find 'option' in class org.graalvm.buildtools.maven.config.agent.AgentOptionsConfiguration
环境信息
PS E:\zipper\OneDrive\remoteAccess\dev\p3s\p3sclient> mvn -version Apache Maven 3.9.6 (bc0240f3c744dd6b6ec2920b3cd08dcc295161ae) Maven home: C:\Program Files\NetBeans-22\netbeans\java\maven Java version: 22.0.2, vendor: GraalVM Community, runtime: C:\Progra~1\Java\graalvm-community-openjdk-22.0.2+9.1 Default locale: en_US, platform encoding: UTF-8 OS name: "windows 11", version: "10.0", arch: "amd64", family: "windows"
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> <name>p3sclient</name> <groupId>p3s</groupId> <artifactId>p3sclient</artifactId> <version>1.0</version> <packaging>jar</packaging> <parent> <groupId>p3s</groupId> <artifactId>p3s</artifactId> <version>1.0</version> <relativePath>../</relativePath> </parent> <properties> <native.maven.plugin.version>0.10.2</native.maven.plugin.version> <junit.jupiter.version>5.11.0</junit.jupiter.version> <maven.compiler.source>22</maven.compiler.source> <maven.compiler.target>22</maven.compiler.target> <imageName>p3sclient</imageName> <mainClass>p3sclient.Main</mainClass> </properties> <dependencies> <dependency> <groupId>com.formdev</groupId> <artifactId>flatlaf</artifactId> <version>0.46</version> </dependency> <dependency> <groupId>p3s</groupId> <artifactId>common</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.11.0</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.4.1</version> <executions> <execution> <id>java</id> <goals> <goal>java</goal> </goals> <configuration> <mainClass>${mainClass}</mainClass> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <!-- annotationProcessorPaths requires maven-compiler-plugin version 3.5 or higher --> <version>3.10.1</version> <configuration> <annotationProcessorPaths> <path> <groupId>info.picocli</groupId> <artifactId>picocli-codegen</artifactId> <version>4.7.5</version> </path> </annotationProcessorPaths> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.4.2</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>${mainClass}</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <id>assembly</id> <goals> <goal>single</goal> </goals> <phase>package</phase> </execution> </executions> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>${mainClass}</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build> <profiles> <profile> <id>native</id> <build> <plugins> <plugin> <groupId>org.graalvm.buildtools</groupId> <artifactId>native-maven-plugin</artifactId> <version>${native.maven.plugin.version}</version> <executions> <execution> <id>build-native</id> <goals> <goal>compile-no-fork</goal> </goals> <phase>package</phase> </execution> </executions> <configuration> <fallback>false</fallback> <agent> <enabled>true</enabled> <options> <enableExperimentalPredefinedClasses>true</enableExperimentalPredefinedClasses> <enableExperimentalUnsafeAllocationTracing> true </enableExperimentalUnsafeAllocationTracing> <trackReflectionMetadata>true</trackReflectionMetadata> </options> </agent> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.4.1</version> <executions> <execution> <id>java-agent</id> <goals> <goal>exec</goal> </goals> <configuration> <executable>java</executable> <workingDirectory>${project.build.directory}</workingDirectory> <arguments> <argument>-classpath</argument> <classpath/> <argument>${mainClass}</argument> </arguments> </configuration> </execution> <execution> <id>native</id> <goals> <goal>exec</goal> </goals> <configuration> <executable>${project.build.directory}/${imageName}</executable> <workingDirectory>>${project.build.directory}</workingDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>
错误信息
[ERROR] Failed to execute goal org.graalvm.buildtools:native-maven-plugin:0.10.2:merge-agent-files (test-native) on project p3sclient: Unable to parse configuration of mojo org.graalvm.buildtools:native-maven-plugin:0.10.2:merge-agent-files for parameter option: Cannot find 'option' in class org.graalvm.buildtools.maven.config.agent.AgentOptionsConfiguration -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.graalvm.buildtools:native-maven-plugin:0.10.2:merge-agent-files (test-native) on project p3sclient: Unable to parse configuration of mojo org.graalvm.buildtools:native-maven-plugin:0.10.2:merge-agent-files for parameter option: Cannot find 'option' in class org.graalvm.buildtools.maven.config.agent.AgentOptionsConfiguration at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:333) at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:316) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:212) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:174) at org.apache.maven.lifecycle.internal.MojoExecutor.access$000 (MojoExecutor.java:75) at org.apache.maven.lifecycle.internal.MojoExecutor$1.run (MojoExecutor.java:162) ... ... ...
解决方案
问题根源是native-maven-plugin版本与agent配置格式不兼容:你使用的0.10.2版本插件不支持当前pom里的<options>子标签配置方式,该配置属于较新版本插件的语法。
两种修复方式:
- 升级插件版本:将
native.maven.plugin.version改为1.0.0及以上的稳定版,新版本插件支持当前的<options>配置格式。 - 适配旧版本配置格式:如果不想升级插件,把agent的选项改为旧版本支持的格式,通过
<option>标签传递JVM参数:
或者直接在<agent> <enabled>true</enabled> <options> <option>-Dgraalvm.agent.experimental.predefinedClasses=true</option> <option>-Dgraalvm.agent.experimental.unsafeAllocationTracing=true</option> <option>-Dgraalvm.agent.trackReflectionMetadata=true</option> </options> </agent>exec-maven-plugin的java-agent执行块中添加agent参数:<execution> <id>java-agent</id> <goals> <goal>exec</goal> </goals> <configuration> <executable>java</executable> <workingDirectory>${project.build.directory}</workingDirectory> <arguments> <argument>-classpath</argument> <classpath/> <argument>-agentlib:native-image-agent=experimental-predefined-classes=true,experimental-unsafe-allocation-tracing=true,track-reflection-metadata=true</argument> <argument>${mainClass}</argument> </arguments> </configuration> </execution>
内容的提问来源于stack exchange,提问作者howToDeleteMyAccount
相关产品推荐
相关产品推荐

