Deploying Spring boot project to external tomcat server.
Published: 2024-07-16 11:10:22

Although spring boot project comes with tomcat server itself. But sometimes we may have requirements to make our spring boot application on an external tomcat server. So, keeping knowledge on how we can deploy our spring boot project on external tomcat server is a plus point for every spring boot learner.


## Building the spring boot project:

I have a simple project with a home route, which is showing "Hello World" to the browser.
![A restcontroller that has a function that returns "hello world" string](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vghlukpobrzzyw8una9b.png)
Now that I have my controller set up. Now I need to extend my **DemoApplication** class to **SpringBootServletInitializer** and should override the function configure just like I have shown below.
```
....
....
@SpringBootApplication
public class DemoApplication  extends SpringBootServletInitializer {
 @Override
 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  return application.sources(DemoApplication.class);
 }
 public static void main(String[] args) {
  SpringApplication.run(DemoApplication.class, args);
 }

}
```
I will now update my pom.xml file.
`<packaging>war</packaging>`

```
<!--for tomcat deployment -->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
   <scope>provided</scope>
  </dependency>
```
![pom.xml file final look](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xonmwzzpmitv6qjvcsbv.png)
This image will make things clearer.
```
<build>
  <finalName>my-app-2</finalName>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
```
Adding _**finalName**_ in the pom.xml will be helpful if you want to give custom .war file which we are going to get after the build process.

## Build our Spring boot project:
Now let's build our newly created spring boot application.
`mvn clean package`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/07ctdomo0m9yubbw44l5.png)
It will build your spring boot application and will make a folder named **target** in your root folder.
![the target folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zk95utywe7jcqtfmcorg.png)
my-app-2.war is my required war file which can be used later on to deploy my web application to tomcat server.

## Installing Tomcat Server:
Now that we have .war file, lets install tomcat zip file from its official website. After download the zip file we need to extract that file and keep the extracted folder in the C-drive of our computer.
![apache tomcat](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/os8zwnp5ci3v8sotvgkb.png)
Note that you have to take compatible tomcat server for your application otherwise you cannot view your deployed app. As my spring boot version is 3.3.1, I have downloaded tomcat version 10. Check your spring boot version then choose the right version of your tomcat server.
Now let's run our tomcat server using startup.bat command in windows. The file is in the bin directory.
![startup.bat](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/602sv7rprp6hvzo3nabu.png)
![tomcat server home page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mm5qwno9mpsd2lwc723g.png)
So, our tomcat server is running perfectly. Let's take the .war file from our spring boot project to tomcat servers webapps folder.
![builded war file in the tomcat webapps folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/piuje9ag43o8n7g3s0dk.png)
Just paste your .war file and wait for 2 to 3 seconds. The server will automatically extract the folder out of the .war file. In the following image you can see I have my-app-2.war file, which is extracted to my-app-2 folder by the tomcat server itself.
Now restart the tomcat server and your application will be accessible on http://localhost:8080/my-app-2/
![Spring App on browser after deployment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mpg25k3lzpx1fk140zq.png)
I have changed my tomcat port to 8081 but you will see your app on 8080 as its default port.
Thank you.