Spring Boot Executable Jar File - Java @ Desk

Friday, March 9, 2018

Spring Boot Executable Jar File

Spring Boot Executable Jar File

In this post, we will learn how to create executable Jar file in Spring Boot through Maven. The Jar file that we create through Maven is not an executable jar file i.e. it does not contains nested jars or external jars added through pom.xml in maven.

The jar file created using maven is not compatible enough to run from command line. In order to run from command line, the jar must include all the nested jars too.

To create an executable jar, spring-boot-maven-plugin need to be added in pom.xml along with the execution configuration repackage goal as shown below
<plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 <version>1.4.0.RELEASE</version>
 <executions>
  <execution>
   <goals>
    <goal>repackage</goal>
   </goals>
  </execution>
 </executions>
</plugin>


The original structure of target folder includes the below structure. It clearly shows, the final jar that is created is only of 8KB i.e. it does not include any nested jars required by the developed classed.

Once the above execution is added, the structure will be changed as shown below


There will be a .jar.original file that is similar to JAR created through 1st mode. The second Jar file is the one that includes all the nested jars required to run the application.

If we open the Jar file using ZIP structure will contain below folders
1) BOOT-INF - It contains external libs and project classes.
2) META-INF
3) org.springframework.boot.loader - It includes all the load configuration.

The above execution needs to be manually added in plugin only if spring-boot-starter-parent is not included in the project POM. If spring-boot-starter-parent is added then spring-boot-maven-plugin can be configured without repackage goal.





No comments:

Post a Comment