[Episode 43] Packaging a Spring Boot Application into an Executable JAR

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] how do we deploy a spring boot application to a production environment for example deployed to a cloud provider like Amazon AWS or macrosoft Azure or deploy to an on premises Unix server don't tell me you want to install your favorite IDE like intj on a production server so that you can run the program by clicking this play button it turns out that once the development is done we need to build and package the application into an artifact and deploy that artifact to the production server recall that when we created our project using Sprint initializer there is an option called packaging by default it is jar jar or Java archive is the most popular packaging type in Sprint boot so in this video I will show you how to package our Sprint boot application into a self-content executable jar file and then show you how to run this jar file from the command line in the end let's open this jar file to look at its file structure so that we can understand how Sprint boot packages our application I will show you two ways to package the application first let's delete the target directory to have a clean start then open the terminal and use this command dot for slash m w package Das D skip test here we're using the maven rapper Maven W to package our project the option-d skip tests makes sure that Maven does not run the tests to save some time press enter as you can see the target directory appears again and you can find the generated jar file under the target directory the second way is easier so again let's delete the target to have a clean start this time we can use the maven panel in intell just double click the package option but this time we're not using the DD skip tests option so this packaging process will take longer as you can see Maven is running all the tests okay I fast forward it a little bit so you don't have to watch Maven building and compiling as you can see build is Success the target directory is back and the jar file is created okay next I want to show you something in pom.xml we scroll all the way down the Sprint boot Maven plugin provides Sprint boot support in Apache Maven it is this plug-in that allows us to package executable jars and round Sprint boot applications so this is the hero behind this things next how to run this executable jar file we can use this command so terminal clear Java Das jar Target directory hogwards artifacts online 0.0.1 snapshot. jar so we can use this Java djar command to launch the application as you can see Sprint boot application is started let's toggle over to the browser all right let's test one API endpoint Local Host ad80 API V1 slash artifacts now remember this one does not require authentication so is public so press enter good as you can see the Sprint boot application is up and running behind the scenes the spring boot Maven plug-in packages the application and its dependencies inside this executable jar you will also hear people call it a fat jar or an Uber jar by the way an embedded Tomcat server is also included in this jar so you don't need to inser it separately in the production environment that means this jar is completely self-contained you can distribute this jar and not worry about whether or not dependencies are installed at the destination as this jar actually has no dependencies what a great idea at this moment you can choose to deploy this jar to a remote production server using a Unix file transfer tool for example SCP or SFTP and then run the jar file using the Java jar command that I just showed you but of course that remote server must have the correct Java run time installed first in the end I would like to show you the file structure inside this jar file you don't need to know the details to work with the jar file but I want to show you how Sprint boot packages our application so let's go ahead and extract the contents of this jar file so let me copy the jar file copy and paste it into a folder I created on the desktop the name of this folder is extracted then let's open the terminal the command used to extract a jar file is jar XF the X option indicates that you want to extract files from the jar file okay as you can see three directories are extracted from this jar file boot-foot loader contains the necessary loader classes to set up and start the application they from Sprint boot so let me collapse it let's look at the boot-foot so for example artifact Hogwarts user security system wizard as you can see application classes written by us are placed under the boot-foot D info SL leap directory okay so let me collapse both under boot dhino there are two more files class path. index and layers. index so what are they the class path. index file provides the ordering that jars should be added to the class path as you can see those jars are from boot d info SLP the layers. index file is used for doer image creation which you will learn in the next video when we create a darker image for a Sprint boot application this executable jar will be split into layers defined in this file this file defines several layers and their order dependencies Sprint boot loader snapshot dependencies and application this file also defines the parts of the jar that should be contained within those layers right now just think of a layer as a directory or folder that contains some classes and jars for example regular released dep dependencies will be contained under the dependencies layer everything under or Sprint framework boot loader will be contained under the Sprint boot loader layer snapshot dependencies will be contained under the snapshot dependencies layer in our project there are no snapshot dependencies application classes written by us and resources will be contained under the application layer you probably wonder what is the point of splitting the jar into these four layers when creating a darker image it turns out that when creating a darker image we need to add the contents of the executable jar to the image but what is the order of adding this layers. index file defines the order that the layers should be added to to the darker image for example all the classes and jars in the dependencies layer should be added to the image before all the Sprint boot loaders in the Sprint boot loader layer and so on Sprint boot separates the classes and jars of the X Cuba jar into different layers based on How likely they are to change between application builds content that is least likely to change should be added first to the darker image followed by layers that are more likely to change the default order is dependencies Sprint boot loader snapshot dependencies and application as you can see here in this file at this moment just remember this layering approach will make darker image creation more efficient it is considered a best practice okay last but not least under meta-inf you can find this manifest.mf it contains information about the files packaged in a jar file I want to highlight two attributes first the main-class attribute specifies the application's entry point in this case it is the the org. sprintf framework. boot. loader. jar launcher class this is the executable Jar's main entry point and it is used to set up an appropriate URL class loader and ultimately call the m method defined in the start- class attribute which in this case is hogwart's artifacts online application if you recall that there is a m method there so Hogwarts artifacts online application is the actual class that we want to launch that is the class that contains a m method in our application all right hopefully you have a better understanding of the executable jar file and its structure in the next video Let's further package this jar file into a doer container see you there
Info
Channel: Bingyang Wei
Views: 950
Rating: undefined out of 5
Keywords:
Id: O2hbIjEteYU
Channel Id: undefined
Length: 14min 58sec (898 seconds)
Published: Mon Oct 30 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.