Quickstart: Create a Java app on Azure App Service

Azure App Service provides a highly scalable, self-patching web hosting service. This quickstart shows how to use the Azure CLI with the Azure Web App Plugin for Maven to deploy a .jar file, or .war file. Use the tabs to switch between Java SE and Tomcat instructions.

Note

The same can also be done using popular IDEs like IntelliJ and Eclipse. Check out our similar documents at Azure Toolkit for IntelliJ Quickstart or Azure Toolkit for Eclipse Quickstart.

Sample app running in Azure App Service

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

Use Azure Cloud Shell

Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. You can use either Bash or PowerShell with Cloud Shell to work with Azure services. You can use the Cloud Shell preinstalled commands to run the code in this article without having to install anything on your local environment.

To start Azure Cloud Shell:

Option Example/Link
Select Try It in the upper-right corner of a code block. Selecting Try It doesn't automatically copy the code to Cloud Shell. Example of Try It for Azure Cloud Shell
Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. Launch Cloud Shell in a new window
Select the Cloud Shell button on the menu bar at the upper right in the Azure portal. Cloud Shell button in the Azure portal

To run the code in this article in Azure Cloud Shell:

  1. Start Cloud Shell.

  2. Select the Copy button on a code block to copy the code.

  3. Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux or by selecting Cmd+Shift+V on macOS.

  4. Select Enter to run the code.

Create a Java app

Clone the Spring Boot Getting Started sample project.

git clone https://github.com/spring-guides/gs-spring-boot

Change directory to the completed project.

cd gs-spring-boot/complete

Configure the Maven plugin

The deployment process to Azure App Service will use your Azure credentials from the Azure CLI automatically. If the Azure CLI is not installed locally, then the Maven plugin will authenticate with Oauth or device login. For more information, see authentication with Maven plugins.

Run the Maven command below to configure the deployment. This command will help you to set up the App Service operating system, Java version, and Tomcat version.

mvn com.microsoft.azure:azure-webapp-maven-plugin:1.12.0:config
  1. When prompted with Subscription option, select the proper Subscription by entering the number print in the line start.

  2. When prompted with Web App option, accept the defaut option <create> by pressing enter or select an existing app.

  3. When prompted with OS option, select Windows by entering 3.

  4. When prompted with Pricing Tier option, select B2 by entering 2.

  5. Use the default Java version, Java 8, by pressing enter.

  6. Finally, press enter on the last prompt to confirm your selections.

    Your summary output will look similar to the snippet shown below.

    Please confirm webapp properties
    Subscription Id : ********-****-****-****-************
    AppName : spring-boot-1599007390755
    ResourceGroup : spring-boot-1599007390755-rg
    Region : westeurope
    PricingTier : Basic_B2
    OS : Windows
    Java : 1.8
    WebContainer : java 8
    Deploy to slot : false
    Confirm (Y/N)? : Y
    [INFO] Saving configuration to pom.
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 41.118 s
    [INFO] Finished at: 2020-09-01T17:43:45-07:00
    [INFO] ------------------------------------------------------------------------
    
  1. When prompted with Subscription option, select the proper Subscription by entering the number print in the line start.

  2. When prompted with Web App option, accept the defaut option <create> by pressing enter or select an existing app.

  3. When prompted with OS option, select Linux by pressing enter.

  4. When prompted with Pricing Tier option, select B2 by entering 2.

  5. Use the default Java version, Java 8, by pressing enter.

  6. Finally, press enter on the last prompt to confirm your selections.

    Please confirm webapp properties
    Subscription Id : ********-****-****-****-************
    AppName : spring-boot-1599007116351
    ResourceGroup : spring-boot-1599007116351-rg
    Region : westeurope
    PricingTier : Basic_B2
    OS : Linux
    RuntimeStack : JAVA 8-jre8
    Deploy to slot : false
    Confirm (Y/N)? : Y
    [INFO] Saving configuration to pom.
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 20.925 s
    [INFO] Finished at: 2020-09-01T17:38:51-07:00
    [INFO] ------------------------------------------------------------------------
    

You can modify the configurations for App Service directly in your pom.xml if needed. Some common ones are listed below:

Property Required Description Version
<schemaVersion> false Specify the version of the configuration schema. Supported values are: v1, v2. 1.5.2
<subscriptionId> false Specify the subscription id. 0.1.0+
<resourceGroup> true Azure Resource Group for your Web App. 0.1.0+
<appName> true The name of your Web App. 0.1.0+
<region> true Specifies the region where your Web App will be hosted; the default value is westeurope. All valid regions at Supported Regions section. 0.1.0+
<pricingTier> false The pricing tier for your Web App. The default value is P1V2 for production workload, while B2 is the recommended minimum for Java dev/test. Learn more 0.1.0+
<runtime> true The runtime environment configuration, you could see the detail here. 0.1.0+
<deployment> true The deployment configuration, you could see the details here. 0.1.0+

Be careful about the values of <appName> and <resourceGroup>(helloworld-1590394316693 and helloworld-1590394316693-rg accordingly in the demo), they will be used later.

Deploy the app

The Maven plugin uses account credentials from the Azure CLI to deploy to App Services. Sign in with the Azure CLI before continuing.

az login

Then you can deploy your Java app to Azure using the following command.

mvn package azure-webapp:deploy

Once deployment has completed, your application will be ready at http://<appName>.azurewebsites.net/(http://helloworld-1590394316693.azurewebsites.net in the demo). Open the url with your local web browser, you should see

Sample app running in Azure App Service

Congratulations! You've deployed your first Java app to App Service.

Clean up resources

In the preceding steps, you created Azure resources in a resource group. If you don't expect to need these resources in the future, delete the resource group from portal, or by running the following command in the Cloud Shell:

az group delete --name <your resource group name; for example: helloworld-1558400876966-rg> --yes

This command may take a minute to run.

Next steps