Quickstart: Create a Java app in App Service

Note

This article deploys an app to App Service on Windows. To deploy to App Service on Linux, see Create Java web app on Linux.

Azure App Service provides a highly scalable, self-patching web hosting service. This quickstart shows how to use the Azure CLI with the Maven Plugin for Azure App Service to deploy a Java web archive (WAR) file.

Note

The same thing 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

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. Cloud Shell lets you use either bash or PowerShell to work with Azure services. You can use the Cloud Shell pre-installed commands to run the code in this article without having to install anything on your local environment.

To launch 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 top-right menu bar in the Azure portal. Cloud Shell button in the Azure portal

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

  1. Launch Cloud Shell.
  2. Select the Copy button on a code block to copy the code.
  3. Paste the code into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS.
  4. Press Enter to run the code.

Create a Java app

Execute the following Maven command in the Cloud Shell prompt to create a new app named helloworld:

mvn archetype:generate -DgroupId=example.demo -DartifactId=helloworld -DarchetypeArtifactId=maven-archetype-webapp

Configure the Maven plugin

To deploy from Maven, use the code editor in the Cloud Shell to open up the project pom.xml file in the helloworld directory.

code pom.xml

Then add the following plugin definition inside the <build> element of the pom.xml file.

<plugins>
    <!--*************************************************-->
    <!-- Deploy to Tomcat in App Service Windows         -->
    <!--*************************************************-->
    <plugin>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-webapp-maven-plugin</artifactId>
        <version>1.7.0</version>
        <configuration>
            <!-- Specify v2 schema -->
            <schemaVersion>v2</schemaVersion>
            <!-- App information -->
            <subscriptionId>${SUBSCRIPTION_ID}</subscriptionId>
            <resourceGroup>${RESOURCEGROUP_NAME}</resourceGroup>
            <appName>${WEBAPP_NAME}</appName>
            <region>${REGION}</region>
            <!-- Java Runtime Stack for App Service on Windows-->
            <runtime>
                <os>windows</os>
                <javaVersion>1.8</javaVersion>
                <webContainer>tomcat 9.0</webContainer>
            </runtime>
            <deployment>
                <resources>
                    <resource>
                        <directory>${project.basedir}/target</directory>
                        <includes>
                            <include>*.war</include>
                        </includes>
                    </resource>
                </resources>
            </deployment>
        </configuration>
    </plugin>
</plugins>

Note

In this article we are only working with Java apps packaged in WAR files. The plugin also supports JAR web applications, visit Deploy a Java SE JAR file to App Service on Linux to try it out.

Update the following placeholders in the plugin configuration:

Placeholder Description
SUBSCRIPTION_ID The unique ID of the subscription you want to deploy your app to. Default subscription's ID can be found from the Cloud Shell or CLI using the az account show command. For all the available subscriptions, use the az account list command.
RESOURCEGROUP_NAME Name for the new resource group in which to create your 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, myResourceGroup. 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 of the host name for the app when deployed to Azure (WEBAPP_NAME.azurewebsites.net). Update this value with a unique name for the new App Service app, which will host your Java app, for example contoso.
REGION An Azure region where the 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.

Deploy the app

Deploy your Java app to Azure using the following command:

mvn package azure-webapp:deploy

Once deployment has completed, browse to the deployed application using the following URL in your web browser, for example http://<webapp>.azurewebsites.net/.

Sample app running in Azure

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

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 by running the following command in the Cloud Shell:

az group delete --name myResourceGroup

This command may take a minute to run.

Next steps