我知道编辑这个网站吗?

Uber-JAR

We recommend against using uber-JARs. They introduce many problems for downstream consumers.

uber-JAR(也称为 fat JAR带有依赖项的 JAR)是一个 JAR 文件,它不仅包含 Java 程序,还嵌入其依赖项。这意味着 JAR 充当软件的“一体化”发行版本,不需要任何其他 Java 代码。(当然,您仍然需要 Java 运行时和底层操作系统。)

##方法

构建 uber-JAR 的常用方法有以下三种:

  • Unshaded. 解压所有JAR文件,然后将它们重新压缩到一个JAR中。
    • Pro: Works with Java’s default class loader.
    • Con: Files present in multiple JAR files with the same path (e.g., META-INF/services/javax.script.ScriptEngineFactory) will overwrite one another, resulting in faulty behavior.
    • Tools: Maven Assembly Plugin, Classworlds Uberjar
  • Shaded。与unshaded相同,但重命名(即“shade”)所有依赖项的所有包。
    • Pro: Works with Java’s default class loader. Avoids some (not all) dependency version clashes.
    • Con: Files present in multiple JAR files with the same path (e.g., META-INF/services/javax.script.ScriptEngineFactory) will overwrite one another, resulting in faulty behavior. As a workaround, if you use Maven, you can add the following lines to the shade plugin section of the POM to apply the appending tranform to multiple resources with the same name (e.g. META-INF/json/org.scijava.plugin.Plugin or META-INF/json/mpicbg.spim.data.generic.sequence.ImgLoaderIo). Both are annotations necessary to find interface or abstract class implementation during runtime across JARs: META-INF/json/org.scijava.plugin.Plugin

      If you need to append more than one file, you unfortunately need to list multiple transformer implementations as wildcards are not allowed. For appending two files, the entry would look like this:

      <transformers combine.children="append">
          <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
              <resource>META-INF/json/org.scijava.plugin.Plugin</resource>
          </transformer>
          <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
              <resource>META-INF/json/mpicbg.spim.data.generic.sequence.ImgLoaderIo</resource>
          </transformer>
      </transformers>
      

      An example of a full maven-shade-plugin can be found here (single entry) and here (multiple entries).

    • Tools: Maven Shade Plugin
  • JAR of JAR。最终的 JAR 文件包含嵌入其中的其他 JAR 文件。
    • Pro: Avoids dependency version clashes. All resource files are preserved.
    • Con: Needs to bundle a special “bootstrap” classloader to enable Java to load classes from the wrapped JAR files. Debugging class loader issues becomes more complex. Using getResource() calls when there are MANY contained jars can be problematic, due to locking inside misc.sun.URLClassPath.
    • Tools: Eclipse JAR File Exporter, One-JAR.

      讨论

优点:单个JAR文件更容易部署。多个JAR文件的版本不可能不匹配。构建Java类路径更容易,因为只需要包含一个JAR。

缺点:

  • 更新软件版本时,都必须重新部署整个 uber-JAR(例如,截至 2015 年 5 月,ImageJ2 为 ~68 MB)。如果打包各个 JAR 组件,则只需更新那些发生更改的组件。此问题与通过 Java Web Start 部署的 Java 应用程序特别相关,因为它会自动下载每个 JAR 依赖项的最新可用版本;在这种情况下,如果您使用uber-JAR,您的应用程序的启动时间将受到影响。
  • 您不能只创建包含所需功能的JAR,因此您的应用程序的占用空间可能会膨胀。
  • 如果下游代码依赖于嵌入在未着色的 uber-jar 中的任何相同依赖项,则您的类路径上可能会遇到这些依赖项的多个副本的麻烦(例如,NoSuchMethodError 对于未着色的 uber-JAR),特别是如果您需要使用该依赖项的版本与 uber-JAR 的多个不同版本时。

您所看到的,了解 uber-JAR 的使用将如何影响您的应用程序非常重要。特别是,使用单独的组件 JAR 可能会更好地为 Java 应用程序提供服务,最好使用依赖关系管理平台(例如 MavenIvy)进行管理。但非对于 Java 应用程序,uber-JAR 可能足以满足您的需求。

另请参阅