Azureの小ネタ (改)

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

Maven Plugin for Azure で例外がでる場合の対処

Java 11を使っていると、mvn で例外が発生するので、その回避策のメモです。

> java --version
java 11.0.1 2018-10-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.1+13-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.1+13-LTS, mixed mode)

これは、一部ライブラリが標準でなくなってしまった影響です。実行結果を見ると、JAXBExceptionが発生していることがわかります。

www.oracle.com

> mvn azure-webapp:config
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< test.webapp:test-server2 >----------------------
[INFO] Building test-server2 Maven Webapp 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- azure-webapp-maven-plugin:1.5.4:config (default-cli) @ test-server2 ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.471 s
[INFO] Finished at: 2019-03-29T07:10:35+09:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.microsoft.azure:azure-webapp-maven-plugin:1.5.4:config (default-cli) on project test-server2: Execution default-cli of goal com.microsoft.azure:azure-webapp-maven-plugin:1.5.4:config failed: A required class was missing while executing com.microsoft.azure:azure-webapp-maven-plugin:1.5.4:config: javax/xml/bind/JAXBException

回避方法

とりあえず、plugin要素下に普通にJAXBへの依存を書けばよいです。

  <build>
    <finalName>test-server2</finalName>
    <plugins>
      <plugin>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-webapp-maven-plugin</artifactId>
        <version>1.5.4</version>
        <dependencies>
          <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>

pluginの下に記述できるのを知らなくてしばし悩みました。根本的には、Pluginで対処してもらうしかなさそうですが。

Maven Plugin for Azure App Service その2

OSとしてWindowsを選択した場合は以下のようになります。 また、App ServiceでJava11がサポートされているのに、選択肢として出現されないのでソースを読んでみたところ、最終的には以下に定義された値を表示しているだけのようなので、SDK for Javaが更新されないといけないようです。

azure-libraries-for-java/JavaVersion.java at master · Azure/azure-libraries-for-java · GitHub

Define value for OS(Default: Linux):
1. linux [*]
2. windows
3. docker
Enter index to use: 2
Define value for javaVersion(Default: 1.8.0_144):
1. 1.7
2. 1.7.0_51
3. 1.7.0_71
4. 1.8
5. 1.8.0_102
6. 1.8.0_111
7. 1.8.0_144 [*]
8. 1.8.0_25
9. 1.8.0_60
10. 1.8.0_73
11. 1.8.0_92
Enter index to use: 11
Define value for webContainer(Default: tomcat 8.5):
1. jetty 9.1
2. jetty 9.1.0.20131115
3. jetty 9.3
4. jetty 9.3.13.20161014
5. tomcat 7.0
6. tomcat 7.0.50
7. tomcat 7.0.62
8. tomcat 8.0
9. tomcat 8.0.23
10. tomcat 8.5 [*]
11. tomcat 8.5.20
12. tomcat 8.5.6
13. tomcat 9.0
14. tomcat 9.0.0
Enter index to use: 14
Please confirm webapp properties
AppName : test-server-1553774323828
ResourceGroup : test-server-1553774323828-rg
Region : westeurope
PricingTier : Premium_P1V2
OS : Windows
Java : 1.8.0_92
WebContainer : tomcat 9.0.0
Deploy to slot : false
Confirm (Y/N)? :

以上、補足でした。

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に配置できる感じになっております。