Maven打包后Class Not Found异常,IDEA中却能找到依赖类
解决ClassNotFound: org.apache.htrace.shaded.commons.logging.impl.LogFactoryImpl的方案
1. 检查Maven打包插件配置
如果使用maven-shade-plugin,需确认包过滤或重命名规则未遗漏impl子包:
- 检查插件的
<filters>节点,确保对org.apache.htrace.shaded.commons.logging的包含规则是**(匹配所有子包),而非仅*(仅匹配当前层级) - 如果有
<relocations>重命名配置,要保证impl子包被正确映射
错误配置示例(需修正):
<filter> <artifact>org.apache.htrace:htrace-core4</artifact> <includes> <include>org/apache/htrace/shaded/commons/logging/*</include> </includes> </filter>
修正后:
<filter> <artifact>org.apache.htrace:htrace-core4</artifact> <includes> <include>org/apache/htrace/shaded/commons/logging/**</include> </includes> </filter>
2. 确认依赖scope配置
检查htrace相关依赖的scope是否为compile或runtime,provided或test scope的依赖不会被打包进最终jar:
<dependency> <groupId>org.apache.htrace</groupId> <artifactId>htrace-core4</artifactId> <version>4.2.0-incubating</version> <!-- 避免使用<scope>provided</scope>或<scope>test</scope> --> </dependency>
3. 清理缓存并重新构建
执行以下Maven命令,清除旧打包文件并强制更新依赖:
mvn clean install -U
clean:删除target目录的旧构建产物-U:强制更新快照依赖,避免本地缓存的依赖包不完整
4. 排查依赖传递阻断
用Maven命令查看依赖树,确认目标类所在的jar是否被正确引入:
mvn dependency:tree -Dincludes=org.apache.htrace
如果发现其他依赖通过<exclusions>排除了htrace的impl子包,手动在依赖中移除该排除规则:
<dependency> <groupId>org.apache.htrace</groupId> <artifactId>htrace-core4</artifactId> <version>4.2.0-incubating</version> <exclusions> <!-- 移除导致impl子包被排除的规则 --> </exclusions> </dependency>
5. 验证本地依赖包完整性
找到本地Maven仓库中htrace的jar包(路径示例:~/.m2/repository/org/apache/htrace/htrace-core4/4.2.0-incubating/htrace-core4-4.2.0-incubating.jar),解压后检查是否存在org/apache/htrace/shaded/commons/logging/impl/LogFactoryImpl.class。若本地jar缺失该类,删除该目录后重新执行mvn install拉取完整依赖。
内容的提问来源于stack exchange,提问作者Dilibaba
相关产品推荐
相关产品推荐

