使用JPMS遇拆分包错误,求Maven构建解决方案
JPMS拆分包冲突解决方案(langchain4j与langchain4j.core)
错误信息
the unnamed module reads package dev.langchain4j.store.embedding from both langchain4j and langchain4j.core
约束条件
- 无法修改第三方库代码
- 必须同时依赖langchain4j和langchain4j.core,移除任意一个会引发其他功能错误
- 使用Maven构建,需要可直接修复编译的方案
- 已准备好可复现问题的完整示例项目
解决方案:通过Maven配置JPMS的--patch-module合并重复包
该方案利用JPMS的补丁模块机制,将两个库中的重复包合并为同一个模块的内容,既保留所有依赖功能,又解决拆分包冲突。
1. 修改pom.xml构建配置
在pom.xml中配置maven-compiler-plugin和maven-surefire-plugin,添加JPMS补丁参数:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <release>17</release> <!-- 替换为你的项目使用的JDK版本 --> <compilerArgs> <!-- 将langchain4j.core的重复包补丁到langchain4j模块 --> <arg>--patch-module</arg> <arg>langchain4j=${project.basedir}/target/dependency/langchain4j-core-*.jar</arg> </compilerArgs> </configuration> <!-- 预先下载依赖到指定目录,方便补丁参数引用 --> <executions> <execution> <id>copy-deps-for-patch</id> <phase>compile</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.basedir}/target/dependency</outputDirectory> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> <!-- 测试阶段同样需要应用补丁参数 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.2</version> <configuration> <argLine> --patch-module langchain4j=${project.basedir}/target/dependency/langchain4j-core-*.jar </argLine> </configuration> </plugin> </plugins> </build>
2. 调整module-info.java
移除对langchain4j.core的显式依赖,因为补丁机制已将其内容合并到langchain4j模块中:
module your.module.name { // 替换为你的模块名称 requires langchain4j; // 删除 requires langchain4j.core; 这一行 }
备选临时方案:强制使用未命名模块(不推荐)
如果上述补丁方案无法快速生效,可临时绕过JPMS模块检查,强制项目作为未命名模块编译运行。在pom.xml的maven-compiler-plugin和maven-surefire-plugin中添加以下参数:
编译阶段配置
<compilerArgs> <arg>--add-modules</arg> <arg>ALL-MODULE-PATH</arg> <arg>--allow-unnamed-modules</arg> </compilerArgs>
测试阶段配置
<argLine> --add-modules ALL-MODULE-PATH --allow-unnamed-modules </argLine>
注意:该方案会跳过JPMS的模块隔离检查,可能引发类加载冲突等潜在问题,仅作为紧急临时修复使用。
内容的提问来源于stack exchange,提问作者morpheus
相关产品推荐
相关产品推荐

