且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Maven-shade-plugin、uber-jar 和重叠类

更新时间:2023-09-19 14:41:04

首先要做的是尽可能多地消除重叠类的明显原因.例如:

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 ***es 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