Deploy Java Web Apps to the cloud

In our first tutorial, we built a Java web application and ran it locally. In this tutorial, you will learn how to deploy and run it on Azure in the cloud.

Prepare your Azure account

Before running this sample application on Azure, you need to have an Azure subscription and the Azure command line tools.

If you don't have an Azure subscription, you can sign up for a free Azure account:

Create your free Azure account

The Azure Command-Line Interface (CLI):

Install Azure CLI 2.0

Login with Azure CLI

To run the Azure CLI, we'll use the Integrated Terminal in VS Code. To open the terminal, you can either:

  • Use the ⌃` (Windows, Linux Ctrl+`) keyboard shortcut with the backtick character.
  • Use the View | Integrated Terminal menu command.
  • From the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)), use the View: Toggle Integrated Terminal command.

Note: You can also open an external shell with the Explorer Open in Command Prompt command (Open in Terminal on macOS or Linux) if you prefer to work outside VS Code.

Sign into your Azure account by using the Azure CLI:

az login

Follow the instructions to complete the sign-in process.

Update your project to use Azure CLI for authentication

Open the pom.xml file in your project folder (complete). Find and remove the snippet below in the azure-webapp-maven-plugin configuration section so that you will authenticate with Azure using the Azure CLI, which you did in the previous step.

Remove Authentication

Note: This specific configuration will now use Azure Service Principal for authentication. More details could be found at Configure Maven to use your Azure Service Principal

Build and deploy your web app to Azure

From the command prompt or terminal window 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

Note: You need to be in the complete directory when running commands in the terminal.

Deploy your web app to Azure by using Maven:

mvn azure-webapp:deploy

Maven will deploy your web app to Azure. If the web app does not already exist, it will be created.

When your web app has been deployed, you will see a success message on the command line:

Deploy Success

By pasting the URL into a web browser, you can see the sample web app running on Azure!

Greeting cloud

You will also be able to manage it using the Azure portal.

Your web app will be listed under App Services:

App Service View

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

Overview

You have successfully deployed a Java web application to the cloud!

Greeting cloud

Next steps