Vaadin 24生产模式打包时为何出现'Cannot invoke java.lang.Class.isInterface()'错误?
问题描述
使用spring-boot:run启动应用完全正常,但执行Maven生产打包目标时,在build-frontend阶段抛出空指针错误:
[ERROR] Failed to execute goal com.vaadin:vaadin-maven-plugin:24.0.5:build-frontend (default-cli) on project myproj: Could not execute build-frontend goal: Error occured during goal execution: Cannot invoke "java.lang.Class.isInterface()" because the return value of "org.reflections.Reflections.forClass(String, java.lang.ClassLoader[])" is nullPlease run Maven with the -e switch (or Gradle with the --stacktrace switch), to learn the full stack trace. -> [Help 1]
项目环境与基础POM配置:
- JDK版本:17
- Vaadin版本:24.0.5
- POM核心配置:
<properties> <java.version>17</java.version> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <vaadin.version>24.0.5</vaadin.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties>
额外信息:仅生产环境打包触发错误,非生产环境运行正常;旧版本Vaadin遇到同类问题时,添加--enable-preview参数可解决,但此环境下无效。
可行解决方法
1. 清理Maven缓存并重新编译
执行以下命令,强制清理旧编译产物并更新依赖:
mvn clean install -U
此操作能排除因缓存的损坏类文件或依赖不一致导致的扫描失败问题。
2. 配置Vaadin插件排除问题包或指定类路径
在POM中为vaadin-maven-plugin添加配置,排除可能引发反射扫描错误的包,或明确指定类路径:
<build> <plugins> <plugin> <groupId>com.vaadin</groupId> <artifactId>vaadin-maven-plugin</artifactId> <version>${vaadin.version}</version> <executions> <execution> <goals> <goal>build-frontend</goal> </goals> <phase>compile</phase> </execution> </executions> <configuration> <!-- 替换为实际存在问题的包路径 --> <excludePackages>com.example.problematic.package</excludePackages> <!-- 明确指定类路径,确保插件能加载所有必要类 --> <classpath>${project.build.outputDirectory}:${project.build.testOutputDirectory}</classpath> </configuration> </plugin> </plugins> </build>
3. 升级Vaadin到24.0.x最新小版本
Vaadin 24.0.5存在已知的JDK17反射兼容性问题,升级到24.0系列的最新版本(如24.0.14)可修复此类bug,只需修改POM中的vaadin.version参数即可。
4. 添加JDK模块开放参数
由于JDK17的模块封装机制,可能导致Reflections无法访问某些核心类,在maven-compiler-plugin中添加--add-opens参数:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <compilerArgs> <arg>--add-opens=java.base/java.lang=ALL-UNNAMED</arg> <arg>--add-opens=java.base/java.util=ALL-UNNAMED</arg> </compilerArgs> </configuration> </plugin>
内容的提问来源于stack exchange,提问作者Sebastien Libert

