Eventually, every application needs to be packaged and deployed on a server so the users can use it. Our Watchlist app is no exception. The good news is that it's already been done by Spring Boot. If you look at the bottom of the pom.xml
file that was generated by Spring Initializr, you'll see a section called build plugins which in our case is configured to Spring Boot plugin:
org.springframework.boot
spring-boot-maven-plugin
This plugin gives us three different options for packaging and running our application:
Packaging the application as an executable JAR file (a.k.a. "fat" JAR).
Packaging the application as a WAR file.
Running the application without packaging.
Let’s see how we can take advantage of each one of these options.
Packaging a fat JAR file
In this method, a JAR is created that includes not only the compiled code and the resources, but a servlet container (web server) like Tomcat is also packaged inside. That’s why it’s called an executable fat JAR. As a result, all you’ll have to do is to run the JAR file like you would run any other executable JAR, i.e., with java -jar <application-name>.jar
command.
This method could only be dreamed of before the release of Spring Boot! Spring Boot frees you from the hassle of installing and maintaining a separate web server and also the process of deploying the built application to the server. That’s the major selling point of Spring Boot over vanilla Spring. 🙂
To open a command line console in your computer, go the root directory of the project, and then run ./mvnw package
. It might take a couple of minutes when you run it for the first time. After it’s finished, run java -jar ./target/watchlist-0.0.1-SNAPSHOT.jar
. Now you should be able to see the welcome page of our application when you visit http://localhost:808
.
This was straightforward, wasn’t it? Yes, but don't take it for granted! This feature is one of the main reasons Spring Boot has become an attractive option for developing microservices. Microservices architecture breaks down a big application into small pieces and then develops, deploys, and scales each piece separately. The ability to package an application with its own web server into a single executable JAR is ideal for such scenarios.
Packaging a WAR file
Creating a WAR (web application archive) and then deploying it to a server used to be the only way to run Java web applications and it’s still supported by Spring Boot. A WAR is a single compressed file that contains multiple files bundled inside it. Creating a WAR is becoming a thing of the past because of the convenience of creating and running fat JARs, but you still might see it in larger organizations with a bit older applications - so it’s worth having a look.
If you want to follow me in this section, install a Tomcat server on you computer by following the official guide. You can get one server up and running on your computer.
First, install an Apache Tomcat server from its official website. Then, open the POM file in the project, and add this line at the top: <packaging>war</packaging>
. Next, open a command console in the root directory of the application source directory, and run this command: ./mvnw package
. When finished, copy the generated WAR from target folder file into the webapps
folder of your Tomcat server. You should be able to see the application running if you go to http://localhost:8080
.
Running in place
So far, you know three ways to run your Spring Boot application:
Directly from Eclipse STS.
Packaging and then running a JAR file.
Packaging and deploying a WAR file into a Tomcat server.
There’s one last way of running the app: using Maven without packaging. This method is suitable for development environment where you have access to the source code, but you don't want to use your IDE for running your app. Let's see how it works.
In the command line in your application root directory, run ./mvnw springboot:run
. You should be able to see the application running just like before if you go to http://localhost:8080
.
Let’s recap!
You now know there are three ways to package and deploy your application:
Packing with a fat JAR:
A fat JAR includes compiled code, resources, and a servlet container.
It is streamlined, without the hassle of installing a web server and deploying the app to a server.
Packaging with a WAR file:
A WAR file is a compressed file that contains multiple file bundles.
It requires the installation of a web server, like Tomcat.
It used to be a standard practice, but is less popular than using fat JARs.
Using Maven without packaging.
In the next part of the course, you will learn how to create dynamic pages using Spring MVC. Once you test your knowledge with the end-of-part quiz, I'll meet you there! 😉