How to deploy the Spring Boot application ( jar/war ) on the live server?

There are various methods to deploy the Spring Boot application. In the previous page, we have described how to generate builds (jar/war) by Maven/Gradle. In this article, we will describe the built deployment process.

So before we begin deploying spring boot applications that are built in either jar or war, we need to understand what are the differences between them, both can be built using any of the built tools Gradle and Maven.

What are the differences between jar and war?

jar (Java ARchive) war ( Web Application Resource)
JAR stands for Java ARchive. WAR  stands for Web Application Resource, also stated Web application ARchive.
It is used to aggregate many Java class files and associated metadata and resources (text, images, etc.) into one file The war contains only binaries and resources that are required for web application

The jar is a self-executable binary archive with an embedded tomcat server.

It needs a pre-configured servlet container that runs a java web application either of Tomcat, Glassfish Server.

How to generate builts jar or war in spring boot?

When we create a project from the spring initializr, there is an option to select Packaging type Jar or War, decide what you need according to your requirement, and choose the needed one.

The application builds are the archive of the binaries compiled source code. Let us know what is needed and how to deploy each archive one by one:

Deploying a JAR (Java Archive) as a standalone application

The jar is a self-executable binary archive with an embedded tomcat server.

If you want to deploy it in Windows OS, you can just double click on it, it will be deployed automatically on the port number you mentioned in the application properties file if not then default port 8080

If you deploy it with Command Line Interface (CLI) in windows cmd and in Linux/Mac Terminal then you can have various deployment options to change configuration externally from the jar like you can mention port number, profile (development/ testing/ production) and many other options.

Command:

$ java -jar filepath/filename.jar

$ java -Dserver.port=8084 -jar filepath/filename.jar  // to run on custom port

$ java -Dspring.profiles.active=testing -jar filepath/filename.jar
// to run with a particular profile

Click here To learn more about externalized configurations.

Deploying a WAR (Web Application Archive) into a servlet container

This archive contains only the web application binaries. It needs a pre-configured servlet container that runs java web application either of Tomcat, Glassfish server.

Taking example of Tomcat server, Inside the directory of the tomcat you can find directories like bin/ conf/ logs/ webapps/ lib/ temp/ work/ etc.

All you just need to move your war file into the webapps/ directory, delete the directory if already present inside webapps/ with the same name as your war file and restart the tomcat server it will deploy automatically in few seconds

Your war file should be named ROOT.war so that you can access directly http://localhost:8080 . If not then you have to type the name as your war filename after the application root. e.g if your filename is demo.war then the access URL will be http://localhost:8080/demo

Thus, it enables us to deploy multiple applications into the same servlet container.



×