|
| 1 | +--- |
| 2 | +author: cephalin |
| 3 | +ms.service: app-service |
| 4 | +ms.devlang: java |
| 5 | +ms.topic: include |
| 6 | +ms.date: 08/30/2023 |
| 7 | +ms.author: cephalin |
| 8 | +--- |
| 9 | + |
| 10 | +In this quickstart, you'll use the [Maven Plugin for Azure App Service Web Apps](https://github.com/microsoft/azure-maven-plugins/blob/develop/azure-webapp-maven-plugin/README.md) to deploy a Java web application with an embedded server to [Azure App Service](/azure/app-service/). App Service provides a highly scalable, self-patching web app hosting service. Use the tabs to switch between Tomcat, JBoss, or embedded server (Java SE) instructions. |
| 11 | + |
| 12 | +The quickstart uses a [Quarkus](https://quarkus.io) sample, which comes with a bundled web server. You can deploy your own application and server bundle in a single JAR file to App Service instead of the using the Tomcat or JBoss hosting options. If you want, you can also embed a Tomcat server in the JAR file and run that in App Service. |
| 13 | + |
| 14 | +> [!NOTE] |
| 15 | +> For Spring apps that requires all the Spring services, try [Azure Spring Apps](../../../spring-apps/quickstart.md) instead. However, you can deploy Spring Boot apps to App Service. |
| 16 | +
|
| 17 | +:::image type="content" source="../../media/quickstart-java/quarkus-hello-world-in-browser-azure-app-service.png" alt-text="Screenshot of Maven Hellow World web app running in Azure App Service in introduction."::: |
| 18 | + |
| 19 | +If Maven isn't your preferred development tool, check out our similar tutorials for Java developers: |
| 20 | ++ [Gradle](../../configure-language-java.md?pivots=platform-linux#gradle) |
| 21 | ++ [IntelliJ IDEA](/azure/developer/java/toolkit-for-intellij/create-hello-world-web-app) |
| 22 | ++ [Eclipse](/azure/developer/java/toolkit-for-eclipse/create-hello-world-web-app) |
| 23 | ++ [Visual Studio Code](https://code.visualstudio.com/docs/java/java-webapp) |
| 24 | + |
| 25 | +[!INCLUDE [quickstarts-free-trial-note](../../../../includes/quickstarts-free-trial-note.md)] |
| 26 | + |
| 27 | +## 1 - Use Azure Cloud Shell |
| 28 | + |
| 29 | +[!INCLUDE [cloud-shell-try-it-no-header.md](../../../../includes/cloud-shell-try-it-no-header.md)] |
| 30 | + |
| 31 | +## 2 - Create a Java app |
| 32 | + |
| 33 | +Execute the following Maven command in the Cloud Shell prompt to create a new app named `quarkus-hello-azure`: |
| 34 | + |
| 35 | +```azurecli-interactive |
| 36 | + mvn io.quarkus.platform:quarkus-maven-plugin:3.2.2.Final:create \ |
| 37 | + -DprojectGroupId=org.acme \ |
| 38 | + -DprojectArtifactId=quarkus-hello-azure \ |
| 39 | + -Dextensions='resteasy-reactive' |
| 40 | +``` |
| 41 | + |
| 42 | +Then change your working directory to the project folder: |
| 43 | + |
| 44 | +```azurecli-interactive |
| 45 | +cd quarkus-hello-azure |
| 46 | +``` |
| 47 | + |
| 48 | +## 3 - Configure the Maven plugin |
| 49 | + |
| 50 | +The deployment process to Azure App Service uses your Azure credentials from the Azure CLI automatically. If the Azure CLI isn't installed locally, then the Maven plugin authenticates with OAuth or device sign-in. For more information, see [authentication with Maven plugins](https://github.com/microsoft/azure-maven-plugins/wiki/Authentication). |
| 51 | + |
| 52 | +Run the Maven command shown next to configure the deployment. This command helps you to set up the App Service operating system, Java version, and Tomcat version. |
| 53 | + |
| 54 | +```azurecli-interactive |
| 55 | +mvn com.microsoft.azure:azure-webapp-maven-plugin:2.12.0:config |
| 56 | +``` |
| 57 | + |
| 58 | +1. For **Create new run configuration**, type **Y**, then **Enter**. |
| 59 | +1. For **Define value for OS**, type **2** for Linux, then **Enter**. |
| 60 | +1. For **Define value for javaVersion**, type **1** for Java 17, then **Enter**. |
| 61 | +1. For **Define value for pricingTier**, type **9** for P1v2, then **Enter**. |
| 62 | +1. For **Confirm**, type **Y**, then **Enter**. |
| 63 | + |
| 64 | + ``` |
| 65 | + Please confirm webapp properties |
| 66 | + AppName : quarkus-hello-azure-1690375364238 |
| 67 | + ResourceGroup : quarkus-hello-azure-1690375364238-rg |
| 68 | + Region : centralus |
| 69 | + PricingTier : P1v2 |
| 70 | + OS : Linux |
| 71 | + Java Version: Java 17 |
| 72 | + Web server stack: Java SE |
| 73 | + Deploy to slot : false |
| 74 | + Confirm (Y/N) [Y]: y |
| 75 | + [INFO] Saving configuration to pom. |
| 76 | + [INFO] ------------------------------------------------------------------------ |
| 77 | + [INFO] BUILD SUCCESS |
| 78 | + [INFO] ------------------------------------------------------------------------ |
| 79 | + [INFO] Total time: 8.139 s |
| 80 | + [INFO] Finished at: 2023-07-26T12:42:48Z |
| 81 | + [INFO] ------------------------------------------------------------------------ |
| 82 | + ``` |
| 83 | +
|
| 84 | +After you've confirmed your choices, the plugin adds the above plugin element and requisite settings to your project's `pom.xml` file that configure your web app to run in Azure App Service. |
| 85 | +
|
| 86 | +The relevant portion of the `pom.xml` file should look similar to the following example. |
| 87 | +
|
| 88 | +```xml-interactive |
| 89 | +<build> |
| 90 | + <plugins> |
| 91 | + <plugin> |
| 92 | + <groupId>com.microsoft.azure</groupId> |
| 93 | + <artifactId>>azure-webapp-maven-plugin</artifactId> |
| 94 | + <version>x.xx.x</version> |
| 95 | + <configuration> |
| 96 | + <schemaVersion>v2</schemaVersion> |
| 97 | + <resourceGroup>your-resourcegroup-name</resourceGroup> |
| 98 | + <appName>your-app-name</appName> |
| 99 | + ... |
| 100 | + </configuration> |
| 101 | + </plugin> |
| 102 | + </plugins> |
| 103 | +</build> |
| 104 | +``` |
| 105 | + |
| 106 | +You can modify the configurations for App Service directly in your `pom.xml`. Some common configurations are listed in the following table: |
| 107 | + |
| 108 | +Property | Required | Description | Version |
| 109 | +---|---|---|--- |
| 110 | +`<schemaVersion>` | false | Specify the version of the configuration schema. Supported values are: `v1`, `v2`. | 1.5.2 |
| 111 | +`<subscriptionId>` | false | Specify the subscription ID. | 0.1.0+ |
| 112 | +`<resourceGroup>` | true | Azure Resource Group for your Web App. | 0.1.0+ |
| 113 | +`<appName>` | true | The name of your Web App. | 0.1.0+ |
| 114 | +`<region>` | false | Specifies the region to host your Web App; the default value is **centralus**. All valid regions at [Supported Regions](https://azure.microsoft.com/global-infrastructure/services/?products=app-service) section. | 0.1.0+ |
| 115 | +`<pricingTier>` | false | The pricing tier for your Web App. The default value is **P1v2** for production workload, while **B2** is the recommended minimum for Java dev/test. For more information, see [App Service Pricing](https://azure.microsoft.com/pricing/details/app-service/linux/)| 0.1.0+ |
| 116 | +`<runtime>` | false | The runtime environment configuration. For more information, see [Configuration Details](https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Web-App:-Configuration-Details). | 0.1.0+ |
| 117 | +`<deployment>` | false | The deployment configuration. For more information, see [Configuration Details](https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Web-App:-Configuration-Details). | 0.1.0+ |
| 118 | + |
| 119 | +For the complete list of configurations, see the plugin reference documentation. All the Azure Maven Plugins share a common set of configurations. For these configurations see [Common Configurations](https://github.com/microsoft/azure-maven-plugins/wiki/Common-Configuration). For configurations specific to App Service, see [Azure Web App: Configuration Details](https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Web-App:-Configuration-Details). |
| 120 | + |
| 121 | +Be careful about the values of `<appName>` and `<resourceGroup>` (`quarkus-hello-azure-1690375364238` and `quarkus-hello-azure-1690375364238-rg` accordingly in the demo). They're used later. |
| 122 | + |
| 123 | +## 4 - Deploy the app |
| 124 | + |
| 125 | +With all the configuration ready in your *pom.xml* file, you can deploy your Java app to Azure with one single command. |
| 126 | + |
| 127 | +1. Rebuild the JAR file using the following command: |
| 128 | + |
| 129 | + ```bash |
| 130 | + mvn clean package -Dquarkus.package.type=uber-jar -D%prod.quarkus.http.port=80 |
| 131 | + ``` |
| 132 | + |
| 133 | + This command adds two Quarkus properties: |
| 134 | + |
| 135 | + - `quarkus.package.type=uber-jar` tells Maven to [generate an Uber-Jar](https://quarkus.io/guides/maven-tooling#uber-jar-maven), which includes all dependencies in the JAR file. |
| 136 | + - `%prod.quarkus.http.port=80` tells Quarkus to use port 80 for the prod environment at runtime, which is the default port that the Linux Java container uses. If you want, you can change the port number of the Java container with the `WEBSITES_PORT` app setting. |
| 137 | + |
| 138 | + You can configure these properties by using other means, but they're added to `mvn package` command here for simplicity. |
| 139 | +
|
| 140 | +2. Deploy to Azure by using the following command: |
| 141 | +
|
| 142 | + ```bash |
| 143 | + mvn azure-webapp:deploy |
| 144 | + ``` |
| 145 | +
|
| 146 | + If the deployment succeeds, you see the following output: |
| 147 | +
|
| 148 | + ```output |
| 149 | + [INFO] Successfully deployed the artifact to https://quarkus-hello-azure-1690375364238.azurewebsites.net |
| 150 | + [INFO] ------------------------------------------------------------------------ |
| 151 | + [INFO] BUILD SUCCESS |
| 152 | + [INFO] ------------------------------------------------------------------------ |
| 153 | + [INFO] Total time: 02:20 min |
| 154 | + [INFO] Finished at: 2023-07-26T12:47:50Z |
| 155 | + [INFO] ------------------------------------------------------------------------ |
| 156 | + ``` |
| 157 | +
|
| 158 | +Once deployment is completed, your application is ready at `http://<appName>.azurewebsites.net/` (`http://quarkus-hello-azure-1690375364238.azurewebsites.net` in the demo). Open the url with your local web browser, you should see |
| 159 | +
|
| 160 | +:::image type="content" source="../../media/quickstart-java/quarkus-hello-world-in-browser-azure-app-service.png" alt-text="Screenshot of Maven Hellow World web app running in Azure App Service."::: |
| 161 | +
|
| 162 | +**Congratulations!** You've deployed your first Java app to App Service. |
| 163 | + |
| 164 | +## 5 - Clean up resources |
| 165 | + |
| 166 | +In the preceding steps, you created Azure resources in a resource group. If you don't need the resources in the future, delete the resource group from portal, or by running the following command in the Cloud Shell: |
| 167 | +
|
| 168 | +```azurecli-interactive |
| 169 | +az group delete --name <your resource group name; for example: quarkus-hello-azure-1690375364238-rg> --yes |
| 170 | +``` |
| 171 | +
|
| 172 | +This command may take a minute to run. |
0 commit comments