Deploy a Spring Boot JAR file app to Azure App Service with Maven and Azure on Linux

In this quickstart, you'll use 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. You'll want to 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 --

    mkdir ~/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.8.0</version>
    </plugin>
    
  3. Then you can configure the deployment, run the following maven command in the Command Prompt and use the number to choose these options in the prompt:

    • OS: linux
    • javaVersion: Java 8
    mvn azure-webapp:config
    

    When you get the Confirm (Y/N) prompt, press 'y' and the configuration is done.

    ~@Azure:~/gs-spring-boot/complete$ mvn azure-webapp:config
    [INFO] Scanning for projects...
    [INFO]
    [INFO] -----------------< org.springframework:gs-spring-boot >-----------------
    [INFO] Building gs-spring-boot 0.1.0
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO]
    [INFO] --- azure-webapp-maven-plugin:1.6.0:config (default-cli) @ gs-spring-boot ---
    [WARNING] The plugin may not work if you change the os of an existing webapp.
    Define value for OS(Default: Linux):
    1. linux [*]
    2. windows
    3. docker
    Enter index to use:
    Define value for javaVersion(Default: Java 8):
    1. Java 11
    2. Java 8 [*]
    Enter index to use:
    Please confirm webapp properties
    AppName : gs-spring-boot-1559091271202
    ResourceGroup : gs-spring-boot-1559091271202-rg
    Region : westeurope
    PricingTier : Premium_P1V2
    OS : Linux
    RuntimeStack : JAVA 8-jre8
    Deploy to slot : false
    Confirm (Y/N)? : Y
    
  4. Add the <appSettings> section to the <configuration> section of <azure-webapp-maven-plugin> to listen on the 80 port.

    <plugin>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-webapp-maven-plugin</artifactId>
        <version>1.8.0</version>
        <configuration>
           <schemaVersion>V2</schemaVersion>
           <resourceGroup>gs-spring-boot-1559091271202-rg</resourceGroup>
           <appName>gs-spring-boot-1559091271202</appName>
           <region>westeurope</region>
           <pricingTier>P1V2</pricingTier>
           <runtime>
             <os>linux</os>
             <javaVersion>jre8</javaVersion>
             <webContainer>jre8</webContainer>
           </runtime>
           <!-- Begin of App Settings  -->
           <appSettings>
              <property>
                    <name>JAVA_OPTS</name>
                    <value>-D server.port=80</value>
              </property>
           </appSettings>
           <!-- End of App Settings  -->
           <deployment>
             <resources>
               <resource>
                 <directory>${project.basedir}/target</directory>
                 <includes>
                   <include>*.jar</include>
                 </includes>
               </resource>
             </resources>
           </deployment>
          </configuration>
    </plugin>
    

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. It might take a few minutes before the web app is visible at the URL shown in the output. Navigate to the URL in a Web browser. You should see the message displayed: Greetings from Spring Boot!

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:

    Find the URL for your web app in Azure portal App Services

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!

Clean up resources

When the Azure resources are no longer needed, clean up the resources you deployed by deleting the resource group.

  • From the Azure portal, select Resource group from the left menu.
  • Enter gs-spring-boot- in the Filter by name field, the resource group created in this tutorial should have this prefix.
  • Select the resource group created in this tutorial.
  • Select Delete resource group from the top menu.

Next steps

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

Additional esources

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