Java 什么是胖 JAR?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19150811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a fat JAR?
提问by erwaman
I've heard people say that they create a fat JAR and deploy it. What do they actually mean ?
我听说有人说他们创建了一个胖 JAR 并部署了它。它们实际上是什么意思?
采纳答案by Dmitry Ginzburg
The fat jar is the jar, which contains classes from all the libraries, on which your project depends and, of course, the classes of current project.
胖 jar 是 jar,它包含来自所有库的类,您的项目依赖于这些库,当然还有当前项目的类。
In different build systems fat jar is created differently, for example, in Gradle one would create it with (instruction):
在不同的构建系统中,fat jar 的创建方式不同,例如,在 Gradle 中,可以使用(指令)创建它:
task fatJar(type: Jar) {
manifest {
attributes 'Main-Class': 'com.example.Main'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
In Maven it's being done this way (after setting up regular jar):
在 Maven 中,它是这样完成的(在设置常规 jar 之后):
<pluginRepositories>
<pluginRepository>
<id>onejar-maven-plugin.googlecode.com</id>
<url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
</pluginRepository>
<plugin>
<groupid>org.dstovall</groupid>
<artifactid>onejar-maven-plugin</artifactid>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<onejarversion>0.97</onejarversion>
<classifier>onejar</classifier>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
回答by MeTTeO
Fat jar or uber jar is a jar which contains all project class files and resources packed together with all it's dependencies. There are different methods for achieving such effect:
Fat jar 或 uber jar 是一个 jar,它包含所有项目类文件和资源以及它的所有依赖项。有不同的方法可以达到这种效果:
- dependencies' jars are copied into main jar and then loaded using special class loader (onejar, spring-boot-plugin:repackage)
- dependencies' jars are extracted at the top of main jar hierarchy (maven-assembly-plugin with it's jar-with-dependencies assembly)
- dependencies' jars are unpacked at the top of main jar hierarchy and their packages are renamed (maven-shade-plugin with shade goal)
- 依赖的 jar 被复制到主 jar 中,然后使用特殊的类加载器(onejar,spring-boot-plugin:repackage)加载
- 依赖的 jar 被提取在主 jar 层次结构的顶部(maven-assembly-plugin 与它的 jar-with-dependencies 程序集)
- 依赖的 jars 在主 jar 层次结构的顶部解压,并且它们的包被重命名(maven-shade-plugin with shade target)
Below sample assembly plugin configuration jar-with-dependencies:
以下示例程序集插件配置jar-with-dependencies:
<project>
...
<build>
...
<plugins>
<plugin>
<!-- NOTE: We don't need a groupId specification because the group is
org.apache.maven.plugins ...which is assumed by default.
-->
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<classifier
</configuration>
...
</project>
For more detailed explanation: Uber-JAR at imagej.net
更详细的解释:Uber-JAR at imagej.net
回答by Sridhar Sarnobat
In the case of an executable jar, another way to think about a fat jar is one you can execute by invoking:
在可执行 jar 的情况下,考虑胖 jar 的另一种方法是您可以通过调用执行的方法:
java -jar myFatLibrary.jar
without the need for -cp
/ --classpath
, or even double clicking the jar icon.
不需要-cp
/ --classpath
,甚至双击 jar 图标。
回答by Jerome Angibaud
A fat jar simply contains same classes as a classical jar + classes from all of their runtime dependencies.
胖 jar 只包含与经典 jar 相同的类 + 来自所有运行时依赖项的类。
With Jeka ( https://jeka.dev) you can achieve it programmatically :
使用 Jeka ( https://jeka.dev),您可以通过编程方式实现:
JkPathTreeSet.of(Paths.get("classes")).andZips(
Paths.get("bouncycastle-pgp-152.jar"),
Paths.get("classgraph-4.8.41.jar"),
Paths.get("ivy-2.4.0.jar")
).zipTo(Paths.get("fat.jar"));
or just by parametring Java plugin :
或仅通过参数化 Java 插件:
javaPlugin.getProject().getMaker().defineMainArtifactAsFatJar(true);
回答by Mark Han
The different names are just ways of packaging java apps.
不同的名称只是打包 java 应用程序的方式。
Skinny– Contains ONLY the bits you literally type into your code editor, and NOTHING else.
Skinny– 仅包含您在代码编辑器中真正键入的位,不包含其他任何内容。
Thin– Contains all of the above PLUS the app's direct dependencies of your app (db drivers, utility libraries, etc).
Thin– 包含上述所有内容以及应用程序的直接依赖项(数据库驱动程序、实用程序库等)。
Hollow– The inverse of Thin – Contains only the bits needed to run your app but does NOT contain the app itself. Basically a pre-packaged “app server” to which you can later deploy your app, in the same style as traditional Java EE app servers, but with important differences.
Hollow– Thin 的反面 – 仅包含运行您的应用程序所需的位,但不包含应用程序本身。基本上是一个预先打包的“应用程序服务器”,您可以稍后将应用程序部署到该服务器上,与传统的 Java EE 应用程序服务器的风格相同,但有重要的区别。
Fat/Uber– Contains the bit you literally write yourself PLUSthe direct dependencies of your app PLUSthe bits needed to run your app “on its own”.
脂肪/尤伯杯-包含位您字面上写自己PLUS您的应用程序的直接依赖PLUS为“自身”运行你的应用程序所需的位。
Source: Article from Dzone
来源:文章来自 Dzone
回答by roottraveller
from the Gradle documentation
In the Java space, applications and their dependenciestypically used to be packaged as separate JARs within a single distribution archive. That still happens, but there is another approach that is now common: placing the classes and resources of the dependencies directly into the application JAR, creating what is known as an uber or fat JAR.
在 Java 空间中,应用程序及其依赖项通常被打包为单个分发存档中的单独 JAR。这仍然会发生,但现在有另一种常见的方法:将依赖项的类和资源直接放入应用程序 JAR 中,创建所谓的 uber 或 fat JAR。
here is a demonstrated of uberJar
task in build.gradle
file:
这是文件中uberJar
任务的演示 build.gradle
:
task uberJar(type: Jar) {
archiveClassifier = 'uber'
from sourceSets.main.output
dependsOn configurations.runtimeClasspath
from {
configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
}
}
In this case, we're taking the runtime dependencies of the project — configurations.runtimeClasspath.files
— and wrapping each of the JAR files with the zipTree()
method. The result is a collection of ZIP file trees, the contents of which are copied into the uber JAR alongside the application classes.
在本例中,我们将获取项目的运行时依赖项 — configurations.runtimeClasspath.files
— 并使用该zipTree()
方法包装每个 JAR 文件。结果是一组 ZIP 文件树,其中的内容与应用程序类一起复制到 uber JAR 中。