|
| 1 | +--- |
| 2 | +title: "Tutorial: Deploy Spring Boot applications using Maven" |
| 3 | +titleSuffix: Azure Spring Cloud |
| 4 | +description: Use Maven to deploy applications to Azure Spring Cloud. |
| 5 | +ms.author: jialuogan |
| 6 | +author: KarlErickson |
| 7 | +ms.service: spring-cloud |
| 8 | +ms.topic: tutorial |
| 9 | +ms.date: 04/07/2022 |
| 10 | +ms.custom: devx-track-java |
| 11 | +--- |
| 12 | + |
| 13 | +# Deploy Spring Boot applications using Maven |
| 14 | + |
| 15 | +**This article applies to:** ✔️ Java ❌ C# |
| 16 | + |
| 17 | +**This article applies to:** ✔️ Basic/Standard tier ✔️ Enterprise tier |
| 18 | + |
| 19 | +This article shows you how to use the Azure Spring Cloud Maven plugin to configure and deploy applications to Azure Spring Cloud. |
| 20 | + |
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +* An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 24 | +* An already provisioned Azure Spring Cloud instance. |
| 25 | +* [JDK 8 or JDK 11](/azure/developer/java/fundamentals/java-jdk-install) |
| 26 | +* [Apache Maven](https://maven.apache.org/download.cgi) |
| 27 | +* [Azure CLI version 2.0.67 or higher](/cli/azure/install-azure-cli) with the Azure Spring Cloud extension. You can install the extension by using the following command: `az extension add --name spring-cloud` |
| 28 | + |
| 29 | +## Generate a Spring Cloud project |
| 30 | + |
| 31 | +To create a Spring Cloud project for use in this article, use the following steps: |
| 32 | + |
| 33 | +1. Navigate to [Spring Initializr](https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.5.7&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=hellospring&name=hellospring&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.hellospring&dependencies=web,cloud-eureka,actuator,cloud-config-client) to generate a sample project with the recommended dependencies for Azure Spring Cloud. This link uses the following URL to provide default settings for you. |
| 34 | + |
| 35 | + ```url |
| 36 | + https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.5.7&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=hellospring&name=hellospring&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.hellospring&dependencies=web,cloud-eureka,actuator,cloud-config-client |
| 37 | + ``` |
| 38 | + |
| 39 | + The following image shows the recommended Spring Initializr setup for this sample project. |
| 40 | + |
| 41 | + :::image type="content" source="media/how-to-maven-deploy-apps/initializr-page.png" alt-text="Screenshot of Spring Initializr."::: |
| 42 | + |
| 43 | + This example uses Java version 8. If you want to use Java version 11, change the option under **Project Metadata**. |
| 44 | + |
| 45 | +1. Select **Generate** when all the dependencies are set. |
| 46 | +1. Download and unpack the package, then create a web controller for a web application. Add the file *src/main/java/com/example/hellospring/HelloController.java* with the following contents: |
| 47 | + |
| 48 | + ```java |
| 49 | + package com.example.hellospring; |
| 50 | + |
| 51 | + import org.springframework.web.bind.annotation.RestController; |
| 52 | + import org.springframework.web.bind.annotation.RequestMapping; |
| 53 | + |
| 54 | + @RestController |
| 55 | + public class HelloController { |
| 56 | + |
| 57 | + @RequestMapping("/") |
| 58 | + public String index() { |
| 59 | + return "Greetings from Azure Spring Cloud!"; |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + ``` |
| 64 | + |
| 65 | +## Build the Spring applications locally |
| 66 | + |
| 67 | +To build the project by using Maven, run the following commands: |
| 68 | + |
| 69 | +```azurecli |
| 70 | +cd hellospring |
| 71 | +mvn clean package -DskipTests -Denv=cloud |
| 72 | +``` |
| 73 | + |
| 74 | +Compiling the project takes several minutes. After it's completed, you should have individual JAR files for each service in their respective folders. |
| 75 | + |
| 76 | +## Provision an instance of Azure Spring Cloud |
| 77 | + |
| 78 | +The following procedure creates an instance of Azure Spring Cloud using the Azure portal. |
| 79 | + |
| 80 | +1. In a new tab, open the [Azure portal](https://portal.azure.com/). |
| 81 | + |
| 82 | +2. From the top search box, search for **Azure Spring Cloud**. |
| 83 | + |
| 84 | +3. Select **Azure Spring Cloud** from the results. |
| 85 | + |
| 86 | +  |
| 87 | + |
| 88 | +4. On the Azure Spring Cloud page, select **Create**. |
| 89 | + |
| 90 | +  |
| 91 | + |
| 92 | +5. Fill out the form on the Azure Spring Cloud **Create** page. Consider the following guidelines: |
| 93 | + |
| 94 | + - **Subscription**: Select the subscription you want to be billed for this resource. |
| 95 | + - **Resource group**: Creating new resource groups for new resources is a best practice. You will use this resource group in later steps as **\<resource group name\>**. |
| 96 | + - **Service Details/Name**: Specify the **\<service instance name\>**. The name must be between 4 and 32 characters long and can contain only lowercase letters, numbers, and hyphens. The first character of the service name must be a letter and the last character must be either a letter or a number. |
| 97 | + - **Location**: Select the region for your service instance. |
| 98 | + |
| 99 | +  |
| 100 | + |
| 101 | +6. Select **Review and create**. |
| 102 | + |
| 103 | + |
| 104 | +## Generate configurations and deploy to the Azure Spring Cloud |
| 105 | + |
| 106 | +To generate configurations and deploy the app, follow these steps: |
| 107 | + |
| 108 | +1. Run the following command from the *hellospring* root folder, which contains the POM file. If you've already signed-in with Azure CLI, the command will automatically pick up the credentials. Otherwise, the command will prompt you with sign-in instructions. For more information, see [Authentication](https://github.com/microsoft/azure-maven-plugins/wiki/Authentication) in the [azure-maven-plugins](https://github.com/microsoft/azure-maven-plugins) repository on GitHub. |
| 109 | + |
| 110 | + ```azurecli |
| 111 | + mvn com.microsoft.azure:azure-spring-cloud-maven-plugin:1.7.0:config |
| 112 | + ``` |
| 113 | + |
| 114 | + You'll be asked to select: |
| 115 | + |
| 116 | + * **Subscription ID** - the subscription you used to create an Azure Spring Cloud instance. |
| 117 | + * **Service instance** - the name of your Azure Spring Cloud instance. |
| 118 | + * **App name** - an app name of your choice, or use the default value `artifactId`. |
| 119 | + * **Public endpoint** - *true* to expose the app to public access; otherwise, *false*. |
| 120 | + |
| 121 | +1. Verify that the `appName` element in the POM file has the correct value. The relevant portion of the POM file should look similar to the following example. |
| 122 | + |
| 123 | + ```xml |
| 124 | + <build> |
| 125 | + <plugins> |
| 126 | + <plugin> |
| 127 | + <groupId>com.microsoft.azure</groupId> |
| 128 | + <artifactId>azure-spring-cloud-maven-plugin</artifactId> |
| 129 | + <version>1.7.0</version> |
| 130 | + <configuration> |
| 131 | + <subscriptionId>xxxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx</subscriptionId> |
| 132 | + <clusterName>v-spr-cld</clusterName> |
| 133 | + <appName>customers-service</appName> |
| 134 | + ``` |
| 135 | + |
| 136 | + The POM file now contains the plugin dependencies and configurations. |
| 137 | + |
| 138 | +1. Deploy the app using the following command. |
| 139 | + |
| 140 | + ```azurecli |
| 141 | + mvn azure-spring-cloud:deploy |
| 142 | + ``` |
| 143 | + |
| 144 | +## Verify the services |
| 145 | + |
| 146 | +After deployment has completed, you can access the app at `https://<service instance name>-hellospring.azuremicroservices.io/`. |
| 147 | + |
| 148 | +:::image type="content" source="media/how-to-maven-deploy-apps/access-app-browser.png" alt-text="Screenshot of app in browser."::: |
| 149 | + |
| 150 | +## Clean up resources |
| 151 | + |
| 152 | +If you plan to continue working with the example application, you might want to leave the resources in place. When no longer needed, delete the resource group containing your Azure Spring Cloud instance. To delete the resource group by using Azure CLI, use the following commands: |
| 153 | + |
| 154 | +```azurecli |
| 155 | +echo "Enter the Resource Group name:" && |
| 156 | +read resourceGroupName && |
| 157 | +az group delete --name $resourceGroupName && |
| 158 | +echo "Press [ENTER] to continue ..." |
| 159 | +``` |
| 160 | + |
| 161 | +## Next steps |
| 162 | + |
| 163 | +* [Prepare Spring application for Azure Spring Cloud](how-to-prepare-app-deployment.md) |
| 164 | +* [Learn more about Azure Spring Cloud Maven Plugin](https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Spring-Cloud) |
0 commit comments