How to deploy Web app using maven for Azure WebApps

In this article, I explain how to build your web application using maven on linux(Ubuntu) into Azure WebApps for buginners.

# Generate Maven project
To generate a maven project, you need to prepare an empty directory:

```bash
[Ubuntu: tempwork]$mkdir samplemaven
[Ubuntu: tempwork]$cd samplemaven/
```

Generate a maven project:

```bash
[Ubuntu: samplemaven]$mvn archetype:generate "-DgroupId=example.demo" "-DartifactId=helloworld" "-DarchetypeArtifactId=maven-archetype-webapp" "-Dversion=1.0-SNAPSHOT"
...
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------< org.apache.maven:standalone-pom >-------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.2.0:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.2.0:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.2.0:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  20.534 s
[INFO] Finished at: 2021-07-05T00:15:39+09:00
[INFO] ------------------------------------------------------------------------
```

- archetype:generate
means to ``Generate`` a maven project

- -DarchetypeArtifactId
means a based project name

- -DgroupId
means a project groupID

- -DartifactId
means a Project ID


You can see what Mave creates as follows:

```bash
[Ubuntu: samplemaven]$tree
.
└── helloworld
    ├── pom.xml
    └── src
        └── main
            ├── resources
            └── webapp
                ├── WEB-INF
                │   └── web.xml
                └── index.jsp
```

``src/main`` directory is for Java source code.

# Congiruge Azure Webapp infomation using maven

Configure Azure Webapp resources for deplying your Web app on your Azure using ``$mvn com.microsoft.azure:azure-webapp-maven-plugin:1.14.0:config``.

Note that this step doesn't create a Resouce group and Webapps service yet. It just configures those info at your local repogitory.

```bash
[Ubuntu: samplemaven]$ll
total 12
drwxr-xr-x 3 tkane tkane 4096 Jul  4 23:57 ./
drwxr-xr-x 4 tkane tkane 4096 Jul  4 17:05 ../
drwxr-xr-x 3 tkane tkane 4096 Jul  4 23:57 helloworld/
[Ubuntu: samplemaven]$cd helloworld/
[Ubuntu: helloworld]$mvn com.microsoft.azure:azure-webapp-maven-plugin:1.14.0:config
...
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< example.demo:helloworld >-----------------------
[INFO] Building helloworld Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- azure-webapp-maven-plugin:1.14.0:config (default-cli) @ helloworld ---
[WARNING] The POM for com.microsoft.azure.applicationinsights.v2015_05_01:azure-mgmt-insights:jar:1.0.0-beta is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details
[INFO] Auth type: DEVICE_CODE
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code ******* to authenticate.
```

Go to ``https://microsoft.com/devicelogin``, enter the code(the info is displayed in your terminal.) 

![Screenshot 2021-07-05 000530.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/414729/18d17fd6-02ab-a71d-6bf6-b813d3f72ea2.png)
then, login Azure using your account:
![Screenshot 2021-07-05 000649.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/414729/17d219b6-e71c-c1e7-b3d4-b76da08607c3.png)

Then, continues the maven process. In these steps, you need to specify WebApp plan like which you want to use OS, CPU and Memory size etc. Please refer to [App Service Pricing | Microsoft Azure](https://azure.microsoft.com/en-us/pricing/details/app-service/windows/) if you want to know it in detail.

```bash
Username: hogepiyo@hogepiyo.com
Available subscriptions:
*  1: mytestsubsctiption
*  Please choose a subscription [mytestsubsctiption]: 1
[INFO] It may take a few minutes to load all Java Web Apps, please be patient.
[WARNING] There are no Java Web Apps in current subscription, please follow the following steps to create a new one.
Define value for OS [Linux]:
* 1: Linux
  2: Windows
  3: Docker
Enter your choice: 1
Define value for pricingTier [P1v2]:
   1: B1
   2: B2
   3: B3
   4: D1
   5: F1
*  6: P1v2
   7: P2v2
   8: P3v2
   9: S1
  10: S2
  11: S3
Enter your choice: 2
Define value for javaVersion [Java 8]:
* 1: Java 8
  2: Java 11
Enter your choice: 2
Define value for runtimeStack:
* 1: Tomcat 8.5
  2: Tomcat 9.0
Enter your choice: 1
Please confirm webapp properties
Subscription Id : *********-*******-****-*************
AppName : helloworld-1625412287998
ResourceGroup : helloworld-1625412287998-rg
Region : westeurope
PricingTier : Basic_B2
OS : Linux
Java : Java 11
Web server stack: Tomcat 8.5
Deploy to slot : false
Confirm (Y/N) [Y]: y
[INFO] Saving configuration to pom.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:55 min
[INFO] Finished at: 2021-07-05T00:25:32+09:00
[INFO] ------------------------------------------------------------------------
```
Login to Azure using ``Azure CLI`` like this:

```bash
[Ubuntu: helloworld]$az login
```

# Create resouces on Azure and Deploy your Web app

Create a Resouce group and WebApps and deploy your Webapp:

```bash
[Ubuntu: helloworld]$mvn package azure-webapp:deploy
```

You may be required to login to Azure again, go to ``https://microsoft.com/devicelogin`` web site and enter the code which shows up on your terminal:

```bash
[INFO] Creating resource group helloworld-1625412287998-rg in region westeurope...
[INFO] Successfully created resource group helloworld-1625412287998-rg.
[INFO] Creating app service plan...
[INFO] Successfully created app service plan asp-helloworld-1625412287998.
[INFO] Creating web app helloworld-1625412287998...
[INFO] Successfully created Web App helloworld-1625412287998.
[INFO] Trying to deploy artifact to helloworld-1625412287998...
[INFO] Successfully deployed the artifact to https://helloworld-1625412287998.azurewebsites.net
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  03:12 min
[INFO] Finished at: 2021-07-05T00:44:41+09:00
[INFO] ------------------------------------------------------------------------
[Ubuntu: helloworld]$
```

You can check the above resouce group and WebApps on your Azure portal:
![gazou.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/414729/52b26e2f-d4cc-16eb-6c7b-0490b4e0cc2d.png)

Also, you can access the Web application(the URL is ``http://<appName>.azurewebsites.net/``):
![Screenshot 2021-07-05 005732.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/414729/b121fc3d-ff6b-1fd8-ddd6-345fc4037cf5.png)
Cool!

# Referrence
[Quickstart: Create a Java app on Azure App Service - Azure App Service | Microsoft Docs](https://docs.microsoft.com/en-us/azure/app-service/quickstart-java?tabs=javase&pivots=platform-linux)
[App Service Pricing | Microsoft Azure](https://azure.microsoft.com/en-us/pricing/details/app-service/windows/)