Let's end our example by creating a fully self-contained executable jar file that can run in a production environment. Executable jars (sometimes become fat jars "fat jars") is an archive that contains your compiled class and the dependent jar required for your code to run.
Executable Jars and Java:
Java does not provide any standard way to load inline jar files, which also contain jar files in jar files. This is a problem if you want to publish a self-contained application. To solve this problem, many developers use "shared" jars. A shared jar simply packs the classes from all jars into a single "Super Jar". The problem with sharing the jar approach is that it is difficult to distinguish which libraries are available in your application. In multiple jars, if the same file name exists (but the content is different) it can also be a problem.
Spring boot takes a different approach and allows you to really embed the jars.
In order to create an executable jar, you need to add spring-boot-maven-plugin to our pom.xml. Insert the following under the Dependencies node:
<Build> <Plugins> <plugin> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-maven-plugin</Artifactid> </plugin> </Plugins></Build>
Note: The Spring-boot-starter-parent pom contains the <executions> configuration used to bind the repackage target. If you do not use the parent POM, you will need to declare the configuration yourself.
Save your Pom.xml and run the MVN package from the command line:
1234567891011121314 |
$ mvn
package
[INFO] Scanning
for
projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject
0.0
.
1
-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] .... ..
[INFO] --- maven-jar-plugin:
2.4
:jar (
default
-jar) @ myproject ---
[INFO] Building jar: /Users/developer/example/spring-boot-example/target/myproject-
0.0
.
1
-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:
1.3
.
0
.BUILD-SNAPSHOT:repackage (
default
) @ myproject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
|
If you look at the target directory, you should see Myproject-0.0.1-snapshot.jar. The file should be about 10Mb in size. If you want to peek inside the structure, you can run the jar TVF:
1 |
$ jar tvf target /myproject-0 .0.1-SNAPSHOT.jar |
In the target directory, you should also see a small file named Myproject-0.0.1-snapshot.jar.original. This is the original jar file that Maven created before spring boot was repackaged.
In order to run the application, you can use the Java-jar command:
1 |
$ java -jar target /myproject-0 .0.1-SNAPSHOT.jar |
. ____ _ __ _ _
/\\ / ___‘_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | ' _ | ' _| | ' _ \ _ ' | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____|. __|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot:: (v1.3.0.build-snapshot)
....... . . .
....... . . . (Log output here)
....... . . .
........ Started Example in 2.536 seconds (JVM running for 2.864)
As before, click Ctrl-c to gently exit the program.
Source: >
<wiz_tmp_tag id= "Wiz-table-range-border" contenteditable= "false" style= "Display:none;" >
From for notes (Wiz)
Springboot Creating an executable jar