Azureの小ネタ (改)

~Azureネタを中心に、色々とその他の技術的なことなどを~

Maven Plugin for Azure App Service

Maven Plugin for Azure App Service というのを見つけたので試してみましたが、その備忘録です。Docs的には以下に書かれています。

プロジェクト作成

mvnコマンドでサーバーアプリのひな形を作ります。

 mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=test.webapp -DartifactId=test-server -Dpackage =test.webapp -DinteractiveMode=false

その後、pom.xmlに以下のプラグインを追加します。

      <plugin> 
        <groupId>com.microsoft.azure</groupId>  
        <artifactId>azure-webapp-maven-plugin</artifactId>  
        <version>1.5.4</version>  
      </plugin> 

このプラグインにはゴールとして、configdeployがあります。

Config

configでは、App Serviceの構成情報を対話的に設定し、pom.xmlに必要な構成情報が追加されます。

mvn azure-webapp:config

上記を実行すると、以下のような対話的な操作で必要な情報が聞かれるので、答えていきます。

C:\ws\sandbox\java\webapps\sample\test-server>mvn azure-webapp:config
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< test.webapp:test-server >-----------------------
[INFO] Building test-server Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- azure-webapp-maven-plugin:1.5.4:config (default-cli) @ test-server ---
[WARNING] The plugin may not work if you change the os of an existing webapp.
Define value for OS(Default: Linux):
1. linux [*]
2. windows
3. docker
Enter index to use: 1
Define value for runtimeStack(Default: jre8):
1. tomcat 8.5
2. tomcat 9.0
3. jre8 [*]
4. wildfly 14
Enter index to use: 2
Please confirm webapp properties
AppName : test-server-1553750486219
ResourceGroup : test-server-1553750486219-rg
Region : westeurope
PricingTier : Premium_P1V2
OS : Linux
RuntimeStack : TOMCAT 9.0-jre8
Deploy to slot : false
Confirm (Y/N)? : y
[INFO] Saving configuration to pom.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 06:42 min
[INFO] Finished at: 2019-03-28T14:28:05+09:00
[INFO] ------------------------------------------------------------------------

聞かれるのは、

  • OS(Linix/Windows/docker)
  • Runtime stack(tomcat/jre/wildfly)

なのですが、OSをWindowsにしたりすると聞かれるRuntime stackが変化します。 他にApp Service、Region などは勝手に決められますが、pom.xmlを直接編集できます。

coniguration要素以下に構成が追加されます。Regionくらいは日本(japaneast/japanwest)に直した方がいいかもしれません。

     <plugin> 
        <groupId>com.microsoft.azure</groupId>  
        <artifactId>azure-webapp-maven-plugin</artifactId>  
        <version>1.5.4</version>  
        <configuration>
          <schemaVersion>V2</schemaVersion>
          <resourceGroup>test-server-1553750486219-rg</resourceGroup>
          <appName>test-server-1553750486219</appName>
          <region>westeurope</region>
          <pricingTier>P1V2</pricingTier>
          <runtime>
            <os>linux</os>
            <javaVersion>jre8</javaVersion>
            <webContainer>tomcat 9.0</webContainer>
          </runtime>
          <deployment>
            <resources>
              <resource>
                <directory>${project.basedir}/target</directory>
                <includes>
                  <include>*.war</include>
                </includes>
              </resource>
            </resources>
          </deployment>
        </configuration>
      </plugin> 

deloloy

deployでは、App Serviceにデプロイします。ただし、その前にazコマンドで該当サブスクリプションにログインしている必要があるので注意が必要です。

mvn clean package
mvn azure-webapp:deply

初回に必要なリソースが作られるので少々時間がかかります。

C:\ws\sandbox\java\webapps\sample\test-server>mvn azure-webapp:deploy
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< test.webapp:test-server >-----------------------
[INFO] Building test-server Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- azure-webapp-maven-plugin:1.5.4:deploy (default-cli) @ test-server ---
[INFO] Authenticate with Azure CLI 2.0
[INFO] Target Web App doesn't exist. Creating a new one...
[INFO] Creating App Service Plan 'ServicePlane255457b-e875-4539'...
[INFO] Successfully created App Service Plan.
[INFO] Successfully created Web App.
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource to C:\ws\sandbox\java\webapps\sample\test-server\target\azure-webapp\test-server-1553750486219
[INFO] Trying to deploy artifact to test-server-1553750486219...
[INFO] Deploying the war file test-server.war...
[INFO] Successfully deployed the artifact to https://test-server-1553750486219.azurewebsites.net
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:14 min
[INFO] Finished at: 2019-03-28T14:34:44+09:00
[INFO] ------------------------------------------------------------------------

以下のようにリソースが作成されます。

f:id:StateMachine:20190328144305p:plain

該当URLにアクセスすれば、Webページが表示されるでしょう。というわけで、Mavenを使って簡単にJavaアプリをWeb Appsに配置できる感じになっております。