Deploying Spring boot project to external tomcat server.

Shoili Rozario - Jul 16 - - Dev Community

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
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);
 }

}
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

pom.xml file final look
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>
Enter fullscreen mode Exit fullscreen mode

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
It will build your spring boot application and will make a folder named target in your root folder.
the target folder
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
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
tomcat server home page
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
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
I have changed my tomcat port to 8081 but you will see your app on 8080 as its default port.
Thank you.

.
Terabox Video Player