fat-jar术语解读
术语概念
fat-jar,也被称为uber-jar,是一种特定方式的Jar包。根据ImageJ网站上对于fat-jar的解释(具体见下面引用的英文),从fat-jar的另外一个别名 JAR with dependencies中可以大致窥见这个术语的含义。
对于jar包的格式不太了解的同学可以先了解一下jar文件的格式,通常情况之下jar包用来存储编译好的class文件和resources文件。如果java工程当中依赖于第三方库,那么仅仅打包本项目中编译好的class和resources而成的jar包无法直接运行,仍然需要额外的依赖和引入第三方包。fat-jar就是将依赖的第三方库也打包放入已经编译好的jar中,形成一个“All-in-one”的不需要依赖其他任何第三方包可独立运行部署的jar。
An uber-JAR—also known as a fat JAR or JAR with dependencies—is a JAR file that contains not only a Java program, but embeds its dependencies as well. This means that the JAR functions as an "all-in-one" distribution of the software, without needing any other Java code. (You still need a Java runtime, and an underlying operating system, of course.)
术语由来
Über
在德语中是above
和over
的含义(甚至如果追根溯,它和英文单词over
同源。所以uber-jar直接使用英文翻译就是over-jar
,也就是在jar中额外添加一些其他的文件(这种额外的文件实际上就是上面说到的依赖的第三方库等)。参考自以下资料
Über
is the German word forabove
orover
(it's actually cognate with the Englishover
).Hence, in this context, an uber-jar is an "over-jar", one level up from a simple JAR (a), defined as one that contains both your package and all its dependencies in one single JAR file. The name can be thought to come from the same stable as ultrageek, superman, hyperspace, and metadata, which all have similar meanings of "beyond the normal".
The advantage is that you can distribute your uber-jar and not care at all whether or not dependencies are installed at the destination, as your uber-jar actually has no dependencies.
All the dependencies of your own stuff within the uber-jar are also within that uber-jar. As are all dependencies of those dependencies. And so on.
相关概念
在java应用的开发完成之后,后续的工作就是将这个应用打包并部署到服务器上运行。这里存在好几种打包的方式,一起来研究一下除开今天的主角fat-jar,还有以下的一些打包的概念,此处参考文章The Skinny on Fat, Thin, Hollow, and Uber - DZone Java
- Skinny – Contains ONLY the bits you literally type into your code editor, and NOTHING else.
- Thin – Contains all of the above PLUS the app’s direct dependencies of your app (db drivers, utility libraries, etc).
- 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, we’ll get to later.
- Fat/Uber – Contains the bit you literally write yourself PLUS the direct dependencies of your app PLUS the bits needed to run your app “on its own”.
以上的描述可能比较让人摸不着头脑,可以通过一张图来了解上述四种打包方式的关系——
使用场景
springBoot就是使用这样的fat-jar的技术进行打包,将所有的依赖打进一个jar中实现一个All-in-one的jar包。