Search Tutorials


Deploy a Spring Boot App (JAR) to Azure App Service | JavaInUse

Deploy a Spring Boot App (JAR) to Azure App Service

In previous tutorial we deployed a containerized Spring Boot Application to Azure App Service. In this tutorial we will be creating a Spring Boot 3 application and deploying it as an Azure App Service using the jar file. In the next tutorial we will be creating an Azure API Management service to securely access the deployed Azure App Service.
Azure App Service is a fully managed platform-as-a-service (PaaS) offering from Microsoft Azure for building, deploying, and scaling web apps.

Benefits of Deploying to Azure App Service:

  1. Managed infrastructure: Azure handles server management, patching, and maintenance, allowing you to focus on your application code.
  2. Scalability: Easily scale your application up or down based on demand, either manually or automatically.
  3. High availability: Built-in load balancing and traffic manager for improved reliability.
  4. Continuous deployment: Streamlined integration with Azure DevOps, GitHub, and other CI/CD tools.
  5. Security: Built-in authentication, SSL support, and integration with Azure security services.
  6. Cost-effective: Pay only for the resources you use, with various pricing tiers available.
  7. Multiple language support: Supports various programming languages and frameworks, including Java and Spring Boot.
  8. Easy monitoring: Integrated application insights and diagnostics tools.
  9. Global reach: Deploy your application to data centers worldwide for reduced latency.
  10. Ecosystem integration: Seamless integration with other Azure services for enhanced functionality.
For this we will be using the following workflow.
Deploy a Spring Boot App(JAR) to Azure App Service

Video

This tutorial is explained in the below Youtube Video.

Implementation

Spring Boot Application

Go to Spring Initializr and create a spring boot application as follows-
Deploy a Spring Boot App to Azure App Spring Initializr
The pom.xml of the created application is as follows-
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.2.7</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.javainuse</groupId>
	<artifactId>boot-azure-apps</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>boot-azure-apps</name>
	<description>Demo project for Spring Boot</description>
	<url/>
	<licenses>
		<license/>
	</licenses>
	<developers>
		<developer/>
	</developers>
	<scm>
		<connection/>
		<developerConnection/>
		<tag/>
		<url/>
	</scm>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>




Next we will be creating a controller class. In this class we will be exposing a GET rest endpoint with url /test which returns string "Hello JavaInUse".
package com.javainuse.boot_azure_apps;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {


    @GetMapping("/test")
    public String test() {
        return "Hello JavaInUse";
    }
}
Finally we want the deployed spring boot application to run on port 80 so in application.properties specify this.
server.port=80
If we now start the application and go to localhost:80/test we get the output as follows
Spring Boot 3 Web Example

Azure Webapp Maven Plugin

To the pom.xml we will be adding the azure webapp maven plugin as follows-
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.2.7</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.javainuse</groupId>
	<artifactId>boot-azure-apps</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>boot-azure-apps</name>
	<description>Demo project for Spring Boot</description>
	<url />
	<licenses>
		<license />
	</licenses>
	<developers>
		<developer />
	</developers>
	<scm>
		<connection />
		<developerConnection />
		<tag />
		<url />
	</scm>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>com.microsoft.azure</groupId>
				<artifactId>azure-webapp-maven-plugin</artifactId>
				<version>2.9.0</version>
			</plugin>
		</plugins>
	</build>

</project>
Next we will be building this pom.xml using the maven command - azure-webapp:config
Spring Boot 3 Web Example
This will download all the azure-webapp maven plugin dependencies. Also it will create the following configuration for our application.
Spring Boot 3 Web Example
In the pom.xml abve configuration gets added automatically-
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.2.7</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.javainuse</groupId>
	<artifactId>boot-azure-apps</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>boot-azure-apps</name>
	<description>Demo project for Spring Boot</description>
	<url />
	<licenses>
		<license />
	</licenses>
	<developers>
		<developer />
	</developers>
	<scm>
		<connection />
		<developerConnection />
		<tag />
		<url />
	</scm>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>com.microsoft.azure</groupId>
				<artifactId>azure-webapp-maven-plugin</artifactId>
				<version>2.9.0</version>
				<configuration>
					<schemaVersion>v2</schemaVersion>
					<resourceGroup>boot-azure-apps-1719693557570-rg</resourceGroup>
					<appName>boot-azure-apps-1719693557570</appName>
					<pricingTier>P1v2</pricingTier>
					<region>centralus</region>
					<runtime>
						<os>Linux</os>
						<javaVersion>Java 17</javaVersion>
						<webContainer>Java SE</webContainer>
					</runtime>
					<deployment>
						<resources>
							<resource>
								<directory>/target</directory>
								<includes>
									<include>*.jar</include>
								</includes>
							</resource>
						</resources>
					</deployment>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>
We will be modifying the above generated configuration to specify the resource group name, application name and also sepcify the server port on which the above application will be running when deployed to azure webapp.
				<configuration>
			        <schemaVersion>v2</schemaVersion>
			        <resourceGroup>newsub</resourceGroup>
			        <appName>boot-azure-apps-javainuse</appName>
			        <pricingTier>P1v2</pricingTier>
			        <region>centralus</region>
			        <appSettings>
			        	<property>
			        		<name>JAVA_OPTS</name>
			        		<value>-Dserver.port=80</value>
			        	</property>
			        </appSettings>
			        <runtime>
			            <os>Linux</os>
			            <javaVersion>Java 17</javaVersion>
			            <webContainer>Java SE</webContainer>
			        </runtime>
			        <deployment>
			            <resources>
			                <resource>
			                    <directory>/target</directory>
			                    <includes>
			                        <include>*.jar</include>
			                    </includes>
			                </resource>
			            </resources>
			        </deployment>
			    </configuration>
Next in the command prompt login to the azure portal as follows-
Spring Boot 3 Web Docker azure login
az login
Next go to the project location and use the mvn azure-webapp:deploy command to deploy the jar file to azure app service.
Spring Boot 3 Web Example
With the above maven command the application gets deployed to azure web apps.
Spring Boot 3 Web Example

Spring Boot 3 Web Example
If we now try to access the exposed GET endpoint, we get the output as follows-
Spring Boot 3 Web Example

Download Source Code

Download it -
Spring Boot 3 + Azure App + JAR Example