Deploy Spring boot application to App Service Tomcat Stack using generated war file

This post has been republished via RSS; it originally appeared at: New blog articles in Microsoft Community Hub.

Some people usually choose to follow this document to package the Spring Boot application to a jar file and use cli command to deploy the jar file to the app service with Java SE stack. However, you may also choose to package the Spring Boot project to a war file and deploy it to Tomcat. This article shows how you can do it.

Firstly, you need to modify your Spring Boot application entrance class to entend SpringBootServletInitializer class and override configure method:

 

 

 

 

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

 

 

 

 

You also need to make sure your project will be package to a war file. In Maven, it is specified in packaging element in the pom.xml:

 

 

 

 

<packaging>war</packaging>

 

 

 

 

If you are using Gradle, modify build.gradle as below:

 

 

 

 

apply plugin: 'war'

 

 

 

 

You also need to have the following text in Maven to mark the embedded servlet container dependency as provided:

 

 

 

 

<dependencies> <!-- … --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!-- … --> </dependencies>

 

 

 

 

If you are using Gradle:

 

 

 

 

dependencies { // … providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' // … }

 

 

 

 

Then run "mvn clean" in the project folder or rebuild grade to generate the war file.

You can then create a web app and choose the same Tomcat version as the embedded one of your Spring Boot project. (Currently Tomcat 10 is not supported for Springboot yet).

yorkzhang_0-1672883711419.png

Once it is created, run below command to deploy the war file to the app service:

 

 

 

 

az webapp deploy --resource-group <group-name> --name <app-name> --src-path ./<package-name>.war

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.