Muokkaa

Jaa


Quickstart: Launch your first Java application in Azure Container Apps using a WAR or JAR file

This article shows you how to deploy the Spring PetClinic sample application to Azure Container Apps using a web application archive (WAR) file or a Java Archive (JAR) file.

There are several options available for deploying Java applications, including the following options:

  • Deployment from a local file system or from a code repository.
  • Deployment using Maven or an IDE.
  • Deployment using a WAR file, a JAR file, or directly from source code.

By the end of this tutorial, you deploy a web application that you can manage through the Azure portal. The following screenshot shows the home page of the PetClinic application deployed to Azure Container Apps:

Screenshot of the home page of the PetClinic app.

Prerequisites

Build the project

Build the project by using the following steps:

  1. Clone the Azure Container Apps Java Samples repo by using the following command:

    git clone https://github.com/Azure-Samples/azure-container-apps-java-samples.git
    
  1. Clone the Spring PetClinic Sample Application repo by using the following command:

    git clone https://github.com/spring-petclinic/spring-framework-petclinic.git
    
  1. Navigate to the spring-petclinic folder by using the following command:

    cd azure-container-apps-java-samples/spring-petclinic/spring-petclinic/
    
  2. Initialize and update the Spring PetClinic Sample Application repo to the latest version by using the following command:

    git submodule update --init --recursive
    
  3. Use the following command to clean the Maven build area, compile the project's code, and create a JAR file, skipping all tests during these processes:

    mvn clean verify
    

You now have a /target/petclinic.jar file.

  1. Navigate to the spring-framework-petclinic folder by using the following command:

    cd spring-framework-petclinic
    
  2. Use the following command to clean the Maven build area, compile the project's code, and create a JAR file, skipping all tests during these processes:

    mvn clean verify
    

You now have a /target/petclinic.war file.

Deploy the project

Deploy the JAR package to Azure Container Apps by using the following command:

Note

The default JDK version is 17. You have the option of specifying the version by using environment variables. To change the JDK version for compatibility with your application, use the --build-env-vars BP_JVM_VERSION=<your-JDK-version> argument. For more information, see Build environment variables for Java in Azure Container Apps (preview).

az containerapp up \
    --resource-group <resource-group> \
    --name <container-app-name> \
    --subscription <subscription-ID>\
    --location <location> \
    --environment <environment-name> \
    --artifact <JAR-file-path-and-name> \
    --ingress external \
    --target-port 8080 \
    --query properties.configuration.ingress.fqdn

Deploy the WAR file to Azure Container Apps by using the following command:

Note

The default Tomcat version is 9. To change the version for compatibility with your application, use the --build-env-vars BP_TOMCAT_VERSION=<your-Tomcat-version> argument. In this example, the Tomcat version is set to 10 - including any minor versions - by setting BP_TOMCAT_VERSION=10.*. For more information, see Build environment variables for Java in Azure Container Apps (preview).

az containerapp up \
    --resource-group <resource-group> \
    --name <container-app-name> \
    --subscription <subscription>\
    --location <location> \
    --environment <environment-name> \
    --artifact <WAR-file-path-and-name> \
    --build-env-vars BP_TOMCAT_VERSION=10.* \
    --ingress external \
    --target-port 8080 \
    --query properties.configuration.ingress.fqdn

Verify the app status

In this example, containerapp up command includes the --query properties.configuration.ingress.fqdn argument, which returns the fully qualified domain name (FQDN), also known as the app's URL.

View the application by pasting this URL into a browser.

Clean up resources

If you plan to continue working with more quickstarts and tutorials, you might want to leave these resources in place. When you no longer need the resources, you can remove them to avoid Azure charges, by using the following command:

az group delete --name <resource-group>