The background,

When learning to package jars using Maven, or plug-ins using SpringBoot, Jar, executing-JAR, UberJAR /fatjar, jar-with-dependencies, jar-in-jar/nested-jars, etc., are these jars related? Is there any difference in their structure?

2. Explanation of nouns

Pre: Strictly speaking, JAR is a compressed file format with a suffix of. Jar, which is similar to. Zip.

1.jar:

Especially in Java program can be used as a dependent jar package, Java can directly read the jar in the class file reference: docs.oracle.com/javase/8/do…

2.executable-jar:

When the JAR package is used as the Java direct launch “medium”, not only the class file in the JAR package can be read directly, but also the relevant information about the program entry is required, so that the JAR package becomes the executable jar reference: Docs.oracle.com/javase/tuto…

3.uber-jar/fat-jar:

Here, we need to distinguish between application classes and application dependencies. When a JAR package contains not only the classes of the application itself, but also the dependencies required by the application, it is called fat-JAR or Uber-JAR

4.jar-with-dependencies:

Maven-assembly-plugin jar-with-dependencies dependencies are the default jars for maven-assembly-plugin applications. Maven.apache.org/plugins/mav…

5.jar-in-jar/nested-jars:

Jar-in-jar: jars jars jars jars jars jars jars jars jars jars jars jars jars jars Would also will packed into the final business code and its dependence on the jar package reference: maven.apache.org/plugins/mav…

PS: Java programs cannot read dependencies in jAR-in-jar directly by default

Three, relationship,

A picture is worth a thousand words:

Structure of JAR package

1.jar:

willThe class fileIt is packaged in a specific form and can be read directly by Java programs as a dependency. Its structure is as follows:

PS: Relying on meta-INF in jar packages is optional

2.executable-jar:

Defined in Javaexecutable-jarWill be added on the basis of relying on JAR packages/META-INF/MANIFEST.MFFile and specify in itMain-ClassProperty, that is, to specify the programThe main entrance

Manifest. MF in PS: Executable jar, other attributes except main-class are optional

3.jar-with-dependencies

Unzip the dependencies directly to the top level directory in the JAR:

4.jar-in-jar/nested-jars

Put dependencies directly into jar packages (without extracting them)/BOOT-INF.libDirectory:

Q: Now that Maven has provided itjar-with-dependenciesWhy does SpringBoot have onejar-in-jar/nested-jars?

Use is mentioned briefly in the SpringBoot documentationjar-in-jar/nested-jarsThe main reason is to solve the problem of class conflict and cross coverage caused by all decompression to the jar package root directory, and it is difficult to trace back to the source.

5. Startup process

1.jar-with-dependencies:

directlyjava -jar xxx.jarJust turn it on. Follow the regularexecutable-jarConsistent, no additional processing is required.

2.jar-in-jar/nested-jars:

Java-jar xxx.jar is also launched, but an additional layer is wrapped to read nested-jar dependencies, mainly to extend the URL protocol, and a custom LaunchedURLClassLoader ClassLoader is used

A. Jar package structure information:

You can seeMain-ClassIs notMain entry of the service, butSpringboot-loader main entry:

B. Schematic diagram of actual startup process:

PS: LaunchedURLClassLoader is also known as Springboot classloader.