When I built the project a few days ago, I always reported this exception every time I started:
Then followed by:
从错误提示来看,是版本不兼容的问题(废话)。
首先估计是slf4j-api的版本,在每个子工程下面不一样,也许在子工程1里面是1.6.1,在子工程2里面是1.5.6,打包运行时候,出现了版本冲突导致的。
eclipse工程下——打开pom文件——选择dependecy Hierarchy ,就可以查看每个jar包以及他们之间的依赖关系。(本质上执行mvn dependency:tree命令是一样的)
查看某个工程的Pom文件,发现其中一个的Pom文件依赖关系是这样的:
其中,当我们在右上角搜索slf的时候,右边显示的是所有的jar包,橙色被选中的是匹配的同名Jar包,我们可以看到有slf4j-api:1.5.6,slf4j-log4j12:1.4.3以及jcl-over-slf4j:1.5.6;左边是工程中哪些其他Jar包间接依赖了slf4j-*,其中compile表示该版本为选中的编译版本,omitted for confict with ××× 表示因为存在×××版本,当前版本被忽略。
因此我们可以看到,slf4j-api编译版本为1.5.6,slf4j-log12编译版本为1.4.3,jcl-over-slf4j编译版本为1.5.6。如果工程中所有依赖的slf4j-api版本都是1.5.6也是允许的,但是考虑到工程整体结构,以及高版本通常兼容低版本,最重要的是只有这个一个子工程用了低版本,当然要升级到1.6.1了!
在此再罗嗦一下,简单说一下Mvn工程Pom文件中dependency之间各jar包版本的依赖关系:
A. pom文件的依赖按声明顺序上到下读取,即先声明的优先
例如:pom文件中依赖的声明顺序由上向下为:
dependency:A(1.0.1)
dependency:A(1.0.2)
那么系统依赖肯定是依赖A1.0.1,因为该依赖最先被声明,后面的被Omitted(忽略)
B. 对于间接依赖,采用最短路径优先算法,距离短的被采用, 其中“—”表示间接依赖
A—B—C—D(1.0.0)
F—E—D(1.0.1)
其中A间接依赖B,B又间接依赖C,C又间接依赖D,此时路径距离为3;另一个依赖:F间接依赖E,E间接依赖D,此时路径为2;
根据最短路径优先原则,会依赖F-E-D(1.0.1)中的D(1.0.1)版本。
C. 对于间接依赖路径长度相同的,谁先声明选谁,和A的规则是一样的
A—B—C—D(1.0.0)
F—E—K—D(1.0.1)
If the path length is the same, the one that declares first will be selected first, and D(1.0.0) must be selected.
In summary:
If we want to upgrade slf4j-api to 1.6.1, the most direct way is to declare a version of slf4j-api:1.6.1 directly before the declaration of tair-mc-client, or exclude tair-mc-client --tair Dependency of -cient --slf4j-api:1.5.6, which indirectly depends on tair-mc-client --tair-client --echache --slf4j-api:1.6.1. In order to be intuitive, we choose to directly declare a dependency of slf4j-api: 1.6.1 version. At the same time, in order to ensure the consistency of slf4j-api and slf4j-log4j12 versions, the slf4j-log4j12:1.6.1 version is directly introduced, because slf4j-log4j12:1.6.1 indirectly depends on slf4j:1.6.1, and declare it at the top of the file, Problem solved .
For excluding conflicts between multiple versions of the same jar package:
The first is to figure out the dependencies between the various Jar packages: for the mvn project using the eclipse tool, you can directly view the dependency Hierarchy in the pom file or use the command mvn dependency:tree to display the dependencies, so that you know which indirect version is something wrong;
Then select the appropriate version and eliminate the unwanted version: usually the higher version is preferred, and a higher version can be declared in a relatively high position in the Pom file, or a lower version dependency can be excluded;