Java模块化项目编译提示找不到io.vertx.codegen模块如何解决?
你遇到的编译错误核心原因是POM中引入的vertx-codegen依赖带了processor分类器,该分类器的JAR包仅包含注解处理器实现,没有JPMS模块描述文件,因此Java模块化系统无法识别到io.vertx.codegen模块。除此之外还存在几处配置遗漏,按以下步骤修改即可解决:
1. 修正vertx-codegen依赖配置
在POM的dependencies节点下新增无classifier的vertx-codegen依赖,scope设为provided(仅编译期需要,不会打包到最终产物):
<dependency> <groupId>io.vertx</groupId> <artifactId>vertx-codegen</artifactId> <scope>provided</scope> </dependency>
原来带processor分类器的vertx-codegen依赖可以暂时保留,后续配置完编译插件后可删除避免冗余。
2. 配置Maven编译插件适配模块化+注解处理器
Java 9+模块化项目中,注解处理器需要单独配置到maven-compiler-plugin的annotationProcessorPaths中才会生效,在POM的build/plugins节点下新增以下配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>11</source> <!-- 替换为你实际使用的JDK版本 --> <target>11</target> <annotationProcessorPaths> <path> <groupId>io.vertx</groupId> <artifactId>vertx-codegen</artifactId> <version>${vertx.version}</version> <classifier>processor</classifier> </path> <path> <groupId>io.vertx</groupId> <artifactId>vertx-service-proxy</artifactId> <version>${vertx.version}</version> </path> </annotationProcessorPaths> </configuration> </plugin>
配置完成后可以删掉原来dependencies节点里带processor分类器的vertx-codegen依赖。
3. 修正module-info.java配置
- 首先修正错误的模块名:vertx 4.x版本的mssql客户端模块名为
io.vertx.mssql.client,你当前写的io.vertx.client.sql.mssql是错误的,需要替换。 - 新增
opens配置,将服务接口所在的包开放给vertx codegen和serviceproxy,允许它们通过反射读取注解信息生成代码。
修改后的完整module-info.java参考:
module mo.com.test.daomodule { requires io.vertx.core; requires io.vertx.mssql.client; requires io.vertx.client.sql; requires io.vertx.codegen; requires io.vertx.serviceproxy; requires org.apache.logging.log4j.core; requires org.apache.logging.log4j; exports mo.com.test.dao.db.service; exports mo.com.test.dao.db; // 开放服务包给codegen和serviceproxy反射访问 opens mo.com.test.dao.db.service to io.vertx.codegen, io.vertx.serviceproxy; }
完成以上三步后重新执行编译即可解决模块找不到的问题,同时vertx codegen也能正常生成服务代理代码。
内容的提问来源于stack exchange,提问作者adnips
相关产品推荐
相关产品推荐

