|
| 1 | +--- |
| 2 | +title: Launch your first Java application in Azure Container Apps |
| 3 | +description: Learn how to deploy a java project in Azure Container Apps. |
| 4 | +services: container-apps |
| 5 | +author: craigshoemaker |
| 6 | +ms.service: container-apps |
| 7 | +ms.custom: devx-track-extended-java |
| 8 | +ms.topic: quickstart |
| 9 | +ms.date: 05/07/2024 |
| 10 | +ms.author: cshoe |
| 11 | +zone_pivot_groups: container-apps-java-artifacts |
| 12 | +--- |
| 13 | + |
| 14 | +# Quickstart: Launch your first Java application in Azure Container Apps |
| 15 | + |
| 16 | +This article shows you how to deploy the Spring PetClinic sample application to run on Azure Container Apps. Rather than manually creating a Dockerfile and directly using a container registry, you can deploy your Java application directly from a Java Archive (JAR) file or a web application archive (WAR) file. |
| 17 | + |
| 18 | +By the end of this tutorial you deploy a web application, which you can manage through the Azure portal. |
| 19 | + |
| 20 | +The following image is a screenshot of how your application looks once deployed to Azure. |
| 21 | + |
| 22 | +:::image type="content" source="media/java-deploy-war-file/azure-container-apps-petclinic-warfile.png" alt-text="Screenshot of petclinic app."::: |
| 23 | + |
| 24 | +## Prerequisites |
| 25 | + |
| 26 | +| Requirement | Instructions | |
| 27 | +|--|--| |
| 28 | +| Azure account | If you don't have one, [create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).<br><br>You need the *Contributor* or *Owner* permission on the Azure subscription to proceed. <br><br>Refer to [Assign Azure roles using the Azure portal](../role-based-access-control/role-assignments-portal.yml?tabs=current) for details. | |
| 29 | +| GitHub Account | Get one for [free](https://github.com/join). | |
| 30 | +| git | [Install git](https://git-scm.com/downloads) | |
| 31 | +| Azure CLI | Install the [Azure CLI](/cli/azure/install-azure-cli).| |
| 32 | +| Java | Install the [Java Development Kit](/java/openjdk/install). Use version 17 or later. | |
| 33 | +| Maven | Install the [Maven](https://maven.apache.org/download.cgi).| |
| 34 | + |
| 35 | +## Prepare the project |
| 36 | + |
| 37 | +Clone the Spring PetClinic sample application to your machine. |
| 38 | + |
| 39 | +::: zone pivot="jar" |
| 40 | + |
| 41 | +```bash |
| 42 | +git clone https://github.com/spring-projects/spring-petclinic.git |
| 43 | +``` |
| 44 | + |
| 45 | +::: zone-end |
| 46 | + |
| 47 | +::: zone pivot="war" |
| 48 | + |
| 49 | +```bash |
| 50 | +git clone https://github.com/spring-petclinic/spring-framework-petclinic.git |
| 51 | +``` |
| 52 | + |
| 53 | +::: zone-end |
| 54 | + |
| 55 | +## Build the project |
| 56 | + |
| 57 | +::: zone pivot="jar" |
| 58 | + |
| 59 | + |
| 60 | +Change into the *spring-petclinic* folder. |
| 61 | + |
| 62 | +```bash |
| 63 | +cd spring-petclinic |
| 64 | +``` |
| 65 | + |
| 66 | +Clean the Maven build area, compile the project's code, and create a JAR file, all while skipping any tests. |
| 67 | + |
| 68 | +```bash |
| 69 | +mvn clean package -DskipTests |
| 70 | +``` |
| 71 | + |
| 72 | +After you execute the build command, a file named *petclinic.jar* is generated in the */target* folder. |
| 73 | + |
| 74 | +::: zone-end |
| 75 | + |
| 76 | +::: zone pivot="war" |
| 77 | + |
| 78 | +> [!NOTE] |
| 79 | +> If necessary, you can specify the Tomcat version in the [Java build environment variables](java-build-environment-variables.md). |
| 80 | +
|
| 81 | +Change into the *spring-framework-petclinic* folder. |
| 82 | + |
| 83 | +```bash |
| 84 | +cd spring-framework-petclinic |
| 85 | +``` |
| 86 | + |
| 87 | +Clean the Maven build area, compile the project's code, and create a WAR file, all while skipping any tests. |
| 88 | + |
| 89 | +```bash |
| 90 | +mvn clean package -DskipTests |
| 91 | +``` |
| 92 | + |
| 93 | +After you execute the build command, a file named *petclinic.war* is generated in the */target* folder. |
| 94 | + |
| 95 | +::: zone-end |
| 96 | + |
| 97 | +## Deploy the project |
| 98 | + |
| 99 | +::: zone pivot="jar" |
| 100 | + |
| 101 | +Deploy the JAR package to Azure Container Apps. |
| 102 | + |
| 103 | +> [!NOTE] |
| 104 | +> If necessary, you can specify the JDK version in the [Java build environment variables](java-build-environment-variables.md). |
| 105 | +
|
| 106 | +Now you can deploy your WAR file with the `az containerapp up` CLI command. |
| 107 | + |
| 108 | +```azurecli |
| 109 | +az containerapp up \ |
| 110 | + --name <CONTAINER_APP_NAME> \ |
| 111 | + --resource-group <RESOURCE_GROUP> \ |
| 112 | + --subscription <SUBSCRIPTION_ID>\ |
| 113 | + --location <LOCATION> \ |
| 114 | + --environment <ENVIRONMENT_NAME> \ |
| 115 | + --artifact <JAR_FILE_PATH_AND_NAME> \ |
| 116 | + --ingress external \ |
| 117 | + --target-port 8080 \ |
| 118 | + --query properties.configuration.ingress.fqdn |
| 119 | +``` |
| 120 | + |
| 121 | +> [!NOTE] |
| 122 | +> The default JDK version is 17. If you need to change the JDK version for compatibility with your application, you can use the `--build-env-vars BP_JVM_VERSION=<YOUR_JDK_VERSION>` argument to adjust the version number. |
| 123 | +
|
| 124 | +You can find more applicable build environment variables in [Java build environment variables](java-build-environment-variables.md). |
| 125 | + |
| 126 | +::: zone-end |
| 127 | + |
| 128 | +::: zone pivot="war" |
| 129 | + |
| 130 | +Deploy the WAR package to Azure Container Apps. |
| 131 | + |
| 132 | +Now you can deploy your WAR file with the `az containerapp up` CLI command. |
| 133 | + |
| 134 | +```azurecli |
| 135 | +az containerapp up \ |
| 136 | + --name <CONTAINER_APP_NAME> \ |
| 137 | + --resource-group <RESOURCE_GROUP> \ |
| 138 | + --subscription <SUBSCRIPTION>\ |
| 139 | + --location <LOCATION> \ |
| 140 | + --environment <ENVIRONMENT_NAME> \ |
| 141 | + --artifact <WAR_FILE_PATH_AND_NAME> \ |
| 142 | + --build-env-vars BP_TOMCAT_VERSION=10.* \ |
| 143 | + --ingress external \ |
| 144 | + --target-port 8080 \ |
| 145 | + --query properties.configuration.ingress.fqdn |
| 146 | +``` |
| 147 | + |
| 148 | +> [!NOTE] |
| 149 | +> The default Tomcat version is 9. If you need to change the Tomcat version for compatibility with your application, you can use the `--build-env-vars BP_TOMCAT_VERSION=<YOUR_TOMCAT_VERSION>` argument to adjust the version number. |
| 150 | +
|
| 151 | +In this example, the Tomcat version is set to `10` (including any minor versions) by setting the `BP_TOMCAT_VERSION=10.*` environment variable. |
| 152 | + |
| 153 | +You can find more applicable build environment variables in [Java build environment variables](java-build-environment-variables.md). |
| 154 | + |
| 155 | +::: zone-end |
| 156 | + |
| 157 | +## Verify app status |
| 158 | + |
| 159 | +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. |
| 160 | + |
| 161 | +View the application by pasting this URL into a browser. Your app should resemble the following screenshot. |
| 162 | + |
| 163 | +:::image type="content" source="media/java-deploy-war-file/azure-container-apps-petclinic-warfile.png" alt-text="Screenshot of petclinic application."::: |
| 164 | + |
| 165 | +## Next steps |
| 166 | + |
| 167 | +> [!div class="nextstepaction"] |
| 168 | +> [Java build environment variables](java-build-environment-variables.md) |
0 commit comments