问题描述
我想使用 Maven-shade-plugin 来创建 uber-jar.但是当我调用 mvn package
命令时,Maven 报告说有一些重叠的类.我附上了所有有问题的重叠,其中一些是由于库(Log4J)的旧版本和新版本引起的,但其中一些似乎具有相同的类 - 例如javax.mail 和 mailapi/smtp/imap 等等.
I would like to use Maven-shade-plugin to create uber-jar. But when I call mvn package
command Maven reports that there are some overlapping classes. I am attaching all problematic overlapps, some of them are caused because older and new verion of a library (Log4J), but some of them seems to have the same classes - e.g. javax.mail and mailapi/smtp/imap et cetera.
在这种情况下最好做什么?是否有一些关键如何决定哪些重叠可以安全忽略哪些需要正确?
What is the best to do in this situation? Is there some key how to decide which overlapping is safe to ignore a which needs to be correct?
- mailapi-1.4.3.jar, javax.mail-1.5.0.jar define 166 overlappping classes
- spring-2.5.6.SEC03.jar, spring-tx-3.1.4.RELEASE.jar define 176 overlappping classes:
- spring-beans-3.1.4.RELEASE.jar, spring-2.5.6.SEC03.jar define 283 overlappping classes:
- slf4j-log4j12-1.7.5.jar, slf4j-impl-2.0-beta2.jar define 3 overlappping classes:
- spring-2.5.6.SEC03.jar, spring-context-support-3.1.4.RELEASE.jar define 55 overlappping classes:
- aopalliance-1.0.jar, spring-2.5.6.SEC03.jar define 9 overlappping classes:
- imap-1.5.0.jar, javax.mail-1.5.0.jar define 87 overlappping classes:
- commons-logging-api-1.1.jar, commons-logging-1.1.3.jar define 19 overlappping classes:
- spring-2.5.6.SEC03.jar, spring-core-3.1.4.RELEASE.jar define 161 overlappping classes:
- spring-2.5.6.SEC03.jar, spring-context-3.1.4.RELEASE.jar define 326 overlappping classes:
- log4j12-api-2.0-beta3.jar, log4j-1.2.17.jar define 23 overlappping classes:
- spring-aop-3.1.4.RELEASE.jar, spring-2.5.6.SEC03.jar define 237 overlappping classes:
- spring-jdbc-3.1.4.RELEASE.jar, spring-2.5.6.SEC03.jar define 239 overlappping classes:
- quartz-1.8.6.jar, quartz-jobs-2.2.1.jar define 15 overlappping classes:
- smtp-1.5.0.jar, javax.mail-1.5.0.jar define 17 overlappping classes:
- spring-asm-3.1.4.RELEASE.jar, spring-2.5.6.SEC03.jar define 31 overlappping classes:
此应用程序A"用作我的另一个 Java 应用程序的 Maven 依赖项 - 我将此应用程序称为B".此 B 应用程序使用 javax.mail 版本 1.5.1.这个库也使用了第一个应用程序.但是当我调用 mvn package 命令时,Maven 注意到 javax.mail-api-1.5.1.jar, javax.mail-1.5.1.jar 定义了 135 个重叠类
.
this application "A" uses as a Maven dependency my another Java application - I'll call this app "B". This B application uses javax.mail ver 1.5.1. This library uses the first application too. But when I call mvn package command, Maven notices that javax.mail-api-1.5.1.jar, javax.mail-1.5.1.jar define 135 overlappping classes
.
是这个问题吗?如果是,如何解决,或者我可以忽略它吗?
Is this problem and if so, how to solve it or can I ignore it?
推荐答案
首先要做的是尽可能多地消除重叠类的明显原因.例如:
The first thing to do is remove as many of the obvious causes of overlapping classes as you can. For instance:
- 您对 spring 2.5.6 和 spring 3.1.4 都有依赖,这会给您带来比仅在 shade 插件中更多的问题.设置你的模块依赖,这样你就只有一个版本的 spring.如果必须,请使用依赖项排除(例如,您有无法控制的传递依赖项).
- 修复依赖版本冲突后,您还可以使用 shade 插件配置配置哪些 jar 进入 uber-jar,如 http://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html李>
- 一些 jar 可能包含它们重叠 jar 中的所有类.
- 我怀疑 commons-logging-1.1.3.jar 具有 commons-logging-api-1.1.jar 中声明的类的超集.如果是这种情况,您可以排除 api jar.
- 针对已编辑的问题,javax.mail-1.5.1.jar 包含 javax.mail-api-1.5.1.jar 中的类的超集.由于这些显然是相同的版本并且重叠的类应该相同,因此使用重叠的类构建阴影 jar 不会有任何害处(它将从它最后处理的任何 jar 中获取类).但是,如果排除 api jar,构建会更整洁,速度也会更快.
- You have dependencies on both spring 2.5.6 and spring 3.1.4, which will give you more problems than just in the shade plugin. Set up your module dependencies so you have just one version of spring. Use dependency exclusions if you have to (say you have transitive dependencies that you do not control).
- Once the dependency version clashes are fixed, you can also configure which jars go into the uber-jar with the shade plugin configuration, as described at http://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html
- Some of the jars probably contain all of the classes from their overlapping jars.
- I suspect that commons-logging-1.1.3.jar has a superset of the classes declared in commons-logging-api-1.1.jar. If this is the case, you can exclude the api jar.
- In response to the edited question, javax.mail-1.5.1.jar contains a superset of the classes in javax.mail-api-1.5.1.jar. Since these are clearly the same version and the overlapping classes should be identical, it will do no harm to build the shaded jar with the overlapping classes (it will take the classes from whichever jar it processes last). However, the build will be tidier and slightly faster if you exclude the api jar.
您不太可能需要在阴影 jar 中保留冲突的类版本.如果你这样做了,shade 插件还允许重定位类,如 http://maven.apache.org/plugins/maven-shade-plugin/examples/class-relocation.html
It's unlikely that you will need to retain conflicting versions of the classes in the shaded jar. If you do, the shade plugin also allows the relocation of classes, as described at http://maven.apache.org/plugins/maven-shade-plugin/examples/class-relocation.html
这篇关于Maven-shade-plugin、uber-jar 和重叠类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!