【術語解釋】fat-jar理解


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 在德語中是aboveover的含義(甚至如果追根溯,它和英文單詞over同源。所以uber-jar直接使用英文翻譯就是over-jar,也就是在jar中額外添加一些其他的文件(這種額外的文件實際上就是上面說到的依賴的第三方庫等)。參考自以下資料

Über is the German word for above or over (it's actually cognate with the English over).

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包。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM