Search code examples
mavensbtsbt-assembly

Why do some Applications use a "fat-jar" compared to a normal one?


What is the advantage of using a fat-jar over a traditional jar?


Solution

  • Fat Jar is just a packaging that includes all application dependencies inside the jar file it self, So for example: if your application A belongs to commons-codec-xyz.jar then fat jar's packaging would be like

    A-fat.jar
    -
    |
    |-your.app.A.class
    |-your.app.B.class
    |-org.apache.commons.codec.Foo.class
    

    This way if your app jar is expected to be part of some body else's app's classpath then it makes it really hard to avoid conflicts at runtime

    It is just a cheaper way of managing classpath so when you launch your app you don't have to deal with providing classpath entries you could just do it

    java -cp .:/path/to/A-fat.jar your.Main
    

    Better packaging are to assemble jars into certain compressed format (.zip, .tar.gz, etc..) that holds following structure

    A.jar
    |
    |--lib/
        |
        |-A.jar (only A's classes)
        |-common-codec-xyz.jar
    

    this way it stays transparent what goes into classpath also you could control order of classpath, but you could not distribute it as library which some body else could re-use in their app, for that purpose you should use maven dependencies that would take care of transitive dependencies

    It all depends on application but these are general thoughts