sbt发布assembly解决jar包冲突问题 deduplicate: different file contents found in the following

一、问题定义

    最近在用sbt打assembly包时出现问题,在package的时候,发生jar包冲突/文件冲突问题,两个相同的class来自不同的jar包在classpath内引起冲突。

具体是:我有一个self4j的jar, 还有一个hadoop-common-hdfs的jar包,其中hadoop-common-hdfs.jar内包含了self4j这个jar包,导致冲突。

此类异常一般是由于打包不规范和打包疏忽引起的。

(个人认为正确的打包策略是:只打包自己核心功能,不将依赖打包在一起,但是有时为了方便或者有时不得不打在一起,要注意可能会出现上述问题)


异常log如下

[trace] Stack trace suppressed: run last *:assembly for the full output.
[error] (*:assembly) deduplicate: different file contents found in the following:
[error] C:\Users\shengli.victor\.ivy2\cache\org.slf4j\slf4j-api\jars\slf4j-api-1.7.7.jar:org/slf4j/IMarkerFactory.class
[error] C:\Users\shengli.victor\.ivy2\cache\com.xxx.xx.hdfsfile\hdfscommon\jars\hdfscommon-1.1.jar:org/slf4j/IMarkerFactory.class
[error] Total time: 4 s, completed 2014-11-20 19:07:33

异常很明显,来自2个不同的jar包self4j,  hdfscommon-1.1.jar里,在org/slf4j/IMarkerFactory.class这个类冲突了。

如下图:

hdfscommon-1.1/jar



slf4j-api-1.7.2.jar



二、解决方案

解决jar包冲突有两种方案:

1、删除其中的某个jar,或者说,在打包的时候,不将2个相同的class打在同一个jar包内的classpath内,即exclude jar。

2、合并冲突


1. Excluding JARs and files

% "provided"

将相同的jar中排除一个,因为重复,可以使用"provided"关键字。

例如spark是一个容器类,编写spark应用程序我们需要spark core jar. 但是真正打包提交到集群上执行,则不需要将它打入jar包内。

这是我们使用 % "provided" 关键字来exclude它。

libraryDependencies ++= Seq(
  "org.apache.spark" %% "spark-core" % "0.8.0-incubating" % "provided",
  "org.apache.hadoop" % "hadoop-client" % "2.0.0-cdh4.4.0" % "provided"
)

Maven defines "provided" as:

This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when build

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值