Deploy a Spring Boot JAR file web app to Azure App Service on Linux

This article demonstrates using the Maven Plugin for Azure App Service Web Apps to deploy a Spring Boot application packaged as a Java SE JAR to Azure App Service on Linux. Choose Java SE deployment over Tomcat and WAR files when you want to consolidate your app's dependencies, runtime, and configuration into a single deployable artifact.

If you don’t have an Azure subscription, create a free account before you begin.

Prerequisites

To complete the steps in this tutorial, you'll need to have the following installed and configured:

Install and sign in to Azure CLI

The simplest and easiest way to get the Maven Plugin deploying your Spring Boot application is by using Azure CLI.

Sign into your Azure account by using the Azure CLI:

az login

Follow the instructions to complete the sign-in process.

Clone the sample app

In this section, you will clone a completed Spring Boot application and test it locally.

  1. Open a command prompt or terminal window and create a local directory to hold your Spring Boot application, and change to that directory; for example:

    md C:\SpringBoot
    cd C:\SpringBoot
    

    -- or --

    md ~/SpringBoot
    cd ~/SpringBoot
    
  2. Clone the Spring Boot Getting Started sample project into the directory you created; for example:

    git clone https://github.com/spring-guides/gs-spring-boot
    
  3. Change directory to the completed project; for example:

    cd gs-spring-boot/complete
    
  4. Build the JAR file using Maven; for example:

    mvn clean package
    
  5. When the web app has been created, start the web app using Maven; for example:

    mvn spring-boot:run
    
  6. Test the web app by browsing to it locally using a web browser. For example, you could use the following command if you have curl available:

    curl http://localhost:8080
    
  7. You should see the following message displayed: Greetings from Spring Boot!

Configure Maven Plugin for Azure App Service

In this section, you will configure the Spring Boot project pom.xml so that Maven can deploy the app to Azure App Service on Linux.

  1. Open pom.xml in a code editor.

  2. In the <build> section of the pom.xml, add the following <plugin> entry inside the <plugins> tag.

    <plugin>
     <groupId>com.microsoft.azure</groupId>
     <artifactId>azure-webapp-maven-plugin</artifactId>
     <version>1.4.0</version>
     <configuration>
       <deploymentType>jar</deploymentType>
    
       <!-- configure app to run on port 80, required by App Service -->
       <appSettings>
         <property> 
           <name>JAVA_OPTS</name> 
           <value>-Dserver.port=80</value> 
         </property> 
       </appSettings>
    
       <!-- Web App information -->
       <resourceGroup>${RESOURCEGROUP_NAME}</resourceGroup>
       <appName>${WEBAPP_NAME}</appName>
       <region>${REGION}</region>  
    
       <!-- Java Runtime Stack for Web App on Linux-->
       <linuxRuntime>jre8</linuxRuntime>
     </configuration>
    </plugin>
    
  3. Update the following placeholders in the plugin configuration:

Placeholder Description
RESOURCEGROUP_NAME Name for the new resource group in which to create your web app. By putting all the resources for an app in a group, you can manage them together. For example, deleting the resource group would delete all resources associated with the app. Update this value with a unique new resource group name, for example, TestResources. You will use this resource group name to clean up all Azure resources in a later section.
WEBAPP_NAME The app name will be part the host name for the web app when deployed to Azure (WEBAPP_NAME.azurewebsites.net). Update this value with a unique name for the new Azure web app, which will host your Java app, for example contoso.
REGION An Azure region where the web app is hosted, for example westus2. You can get a list of regions from the Cloud Shell or CLI using the az account list-locations command.

A full list of configuration options can be found in the Maven plugin reference on GitHub.

Deploy the app to Azure

Once you have configured all of the settings in the preceding sections of this article, you are ready to deploy your web app to Azure. To do so, use the following steps:

  1. From the command prompt or terminal window that you were using earlier, rebuild the JAR file using Maven if you made any changes to the pom.xml file; for example:

    mvn clean package
    
  2. Deploy your web app to Azure by using Maven; for example:

    mvn azure-webapp:deploy
    

Maven will deploy your web app to Azure; if the web app or web app plan does not already exist, it will be created for you.

When your web has been deployed, you will be able to manage it through the Azure portal.

  • Your web app will be listed in App Services:

    Web app listed in Azure portal App Services

  • And the URL for your web app will be listed in the Overview for your web app:

    Determining the URL for your web app

Verify that the deployment was successful by using the same cURL command as before, using your web app URL from the Portal instead of localhost. You should see the following message displayed: Greetings from Spring Boot!

Next steps

To learn more about Spring and Azure, continue to the Spring on Azure documentation center.

Additional Resources

For more information about the various technologies discussed in this article, see the following articles: