Mikael Sukoinen

Deploying a Java web app to the Azure cloud

This tutorial shows you how to deploy a Java web application to Microsoft’s Azure cloud. Azure offers a 30-day free trial that includes 12 months of free commercial services. You can find details of their free and commercial services on the Azure website.

Note
You need a credit card to confirm your identity when signing up on Microsoft Azure.

We use the latest Vaadin Spring Boot starter app in our example. You can see the example app running on the Azure cloud here.

The tutorial includes two options to deploy the application:

  1. Directly from your command line.

  2. From a GitHub repository in a CI pipeline.

Prerequisites:

  1. Create an Azure account at https://signup.azure.com/.

  2. Verify that you have Java installed by running the java --version command in your terminal. If not, download and install the latest version on your computer.

  3. Verify that you have Git installed by running the git --version command in your terminal. If not, download and install the latest version on your computer.

  4. Verify that you have Maven (at least version 3.0) installed by running the mvn -v command in your terminal. If not, download and install the latest version on your computer.

Option 1: Direct upload from your command line

This method describes how to upload a built JAR file (.jar) directly on Microsoft Azure. Skip to Option 2 below, if you prefer to deploy from a GitHub repository for continuous iteration.

Create an Azure application

  1. Go to the Azure portal https://portal.azure.com/#home and log in.

  2. Select Create a resource under Azure Services.

    Azure Services view
  3. Go to Web > Web App

  4. In the Basics tab, fill in the details:

    1. Subscription: Free Trial.

    2. Resource Group: Select Create new, enter a group name and select OK.

    3. Name: The name of your application.

    4. Publish: Code.

    5. Runtime stack: Tomcat 9.0.

    6. Operating System: Linux.

    7. Region: your region.

    8. Linux Plan: Create New and enter a name.

    9. Sku and size: Free F1 for 1 GB size.

      wep app example details
  5. Select Review + create.

  6. Review the details and select Create.

Tip
You can also add monitoring options and tags before creating your app, if you like.

Prepare the application

  1. Install the Azure CLI.

    Note
    You can find installation instructions for different platforms in the Azure CLI documentation.
  2. Log in to your Azure dashboard by running the az login command in your terminal.

  3. Download and open a starter project from http://vaadin.com/start/latest. Select Spring Boot as the technology stack and fill the Maven Group ID and Project Name as you see fit (or leave them at the defaults).

    Vaadin starter download page
  4. Change the server port in your Spring project application.properties file in src/main/resources to: server.port=${port:8080}. You can find more details about this in the Spring documentation.

  5. Open your pom.xml file and add the following section in the <plugins> tag in the <build> section:

    pom.xml
    <plugin>
     <groupId>com.microsoft.azure</groupId>
     <artifactId>azure-webapp-maven-plugin</artifactId>
     <version>1.9.1</version>
    </plugin>
  6. Configure the application using the mvn azure-webapp:config command. Select the corresponding number to insert your OS: Linux and Java version: Java 11 and then enter Y on your keyboard to confirm.

  7. Run the mvn azure-webapp:config command again and press 1 on your keyboard to enter the details of your Azure Web app:

    pom.xml
        <resourceGroup>YOUR_RESOURCE_GROUP</resourceGroup>
           <appName>YOUR_APP_NAME</appName>
           <region>YOUR_REGION</region>
           <pricingTier>F1</pricingTier>
  8. Add this <appSettings> section to the <configuration> section in your pom.xml:

    pom.xml
     <configuration>
              <schemaVersion>V2</schemaVersion>
              <resourceGroup>YOUR_RESOURCE_GROUP</resourceGroup>
           <appName>YOUR_APP_NAME</appName>
           <pricingTier>F1</pricingTier>
           <region>YOUR_REGION</region>
              <runtime>
                <os>linux</os>
                <javaVersion>java11</javaVersion>
                <webContainer>java11</webContainer>
              </runtime>
              <appSettings>
                <property>
                  <name>JAVA_OPTS</name>
                  <value>-Dserver.port=80</value>
                </property>
              </appSettings>
              <deployment>

Deploy your application

  1. Generate a JAR file (.jar) of your application using the mvn package -Pproduction command.

  2. Deploy to Azure using the mvn azure-webapp:deploy command.

  3. In the Azure portal go to Home > App Services and select your application in the list to configure it.

    Azure Services view
  4. Open the application URL in your browser.

    open URL
    example application
    Note
    It might take a few minutes for the page to display.

Source code on GitHub.

Option 2: Setting up a GitHub CI pipeline

Create an Azure application

  1. Go to the Azure portal https://portal.azure.com/#home and log in.

  2. Select Create a resource under Azure Services.

    Azure Services view
  3. Search the marketplace for 'API App' and select Create.

    create API app
  4. Fill in the details:

    1. App name: The name of your application.

    2. Subscription: S1

      Note
      The S1 subscription tier is commercial and included in the free 30 day trial.
    3. Resource Group: Name and create new.

    4. App Service plan/Location: Your location

    5. Application insights: Name and create new.

  5. Select Create.

  6. In the Azure portal go to Home > App Services and select your application in the list to configure it.

    Azure Services view
  7. Go to Settings > Configuration > General settings and use the following:

    1. Stack: Java

    2. Java version: Java 11

    3. Java minor version: 11 (Auto update)

    4. Java container: Tomcat

    5. Java container version: 9 (Auto update)

    6. Platform: 64-bit

    7. Managed pipeline version: Integrated

    8. FTP state: All Alowed

    9. HTTP version: 1.1

    10. Web sockets: OFF

    11. Awlays on: ON

      general settings view
  8. Go to Development Tools > App Service Editor (preview) > Go→ and delete all pre-configuration files in the WWWROOT folder (.html file pictured below):

    files in wwwroot folder

    Making the folder empty for now.

    empty wwwroot folder

    Prepare the application

  9. Download and open a starter project from http://vaadin.com/start/latest. Select Spring Boot as the technology stack and fill the Maven Group ID and Project Name as you see fit (or leave them at the defaults).

    Vaadin starter download page
  10. Change the server port in your Spring project application.properties file in src/main/resources to: server.port=${port:8080}. You can find more details about this in the Spring documentation.

  11. Generate a JAR file (.jar) of your application using the mvn package -Pproduction command.

  12. Create a web.config file with the following content. Replace “YOUR_APP_NAME” with the name of your application:

    web.config
    
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
       <system.webServer>
           <handlers>
               <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
           </handlers>
           <httpPlatform processPath="%JAVA_HOME%\bin\java.exe"
                         arguments="-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar%HOME%\site\wwwroot\YOUR_APP_NAME.jar>
           </httpPlatform>
       </system.webServer>
    </configuration>
  13. Push the .jar and web.config file to a GitHub repository.

    Tip
    You can create a separate 'deployment' branch containing only the deployment files if your application is already hosted on a GitHub repository.

Configure GitHub and deploy your application

  1. Go to the Azure portal at https://portal.azure.com/#home.

  2. Select the application you just created in Recent resources.

  3. In the left navigation, select Deployment > Deployment Center.

  4. In Continuous Deployment (CI / CD) select GitHub. and then Continue.

    CI pipeline setup
    Warning
    You need to authorize GitHub if you haven’t already done so.
  5. Select Continue.

  6. Select App Service as the build service.

  7. Select your organization, repository, branch and then continue. This will automatically configure your app for deployment.

  8. Go to Home > App Services and select your application in the list.

  9. Open the application URL in your browser.

    example app from GitHub

    Source code on GitHub.

Vaadin is an open-source framework offering the fastest way to build web apps on Java backends
GET STARTED

Comments (9)

Mikael Sukoinen
5 months ago Apr 30, 2021 12:32pm
Mikael Sukoinen
1 year ago Jul 08, 2020 8:41am
Karan Kumar
1 year ago Jul 09, 2020 7:42am
Mikael Sukoinen
1 year ago Jul 10, 2020 12:52pm
A.Mahdy AbdelAziz
2 years ago Jun 13, 2019 11:49pm