LoginSignup
3
1

More than 1 year has passed since last update.

Payara MicroのWebアプリケーションをAzure App Serviceで動かす

Posted at

以下のドキュメントを参考にPayara MicroのWebアプリをAzure App Serviceで動かしてみました。

プロジェクトをMicroProfile Starterで作成します。

スクリーンショット 2021-05-08 9.41.23.png

プロジェクトに含まれるコードはDemoRestApplication.javaとHelloController.javaのみです。

package com.example.demo;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

/**
 *
 */
@ApplicationPath("/data")
@ApplicationScoped
public class DemoRestApplication extends Application {
}
package com.example.demo;

import javax.inject.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

/**
 *
 */
@Path("/hello")
@Singleton
public class HelloController {

    @GET
    public String sayHello() {
        return "Hello World";
    }
}

コードは特に変更せず、そのまま使いました。

pom.xmlのbuildセクションに以下を追加します。

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

以下のコマンドを実行して、Linux、Java 11、Tomcat 9.0を選びました。

mvn com.microsoft.azure:azure-webapp-maven-plugin:1.14.0:config

その後、pom.xmlに以下の変更を加えます。

  • webContainerをTomcatからjava11(またはJava SE)に変更
  • appSettingsを追加
  • includeのwarをjarに変更

実際のpom.xmlはこちらです。

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>com.example</groupId>  
  <artifactId>demo</artifactId>  
  <version>1.0-SNAPSHOT</version>  
  <packaging>war</packaging>  
  <properties> 
    <maven.compiler.target>11</maven.compiler.target>  
    <failOnMissingWebXml>false</failOnMissingWebXml>  
    <maven.compiler.source>11</maven.compiler.source>  
    <payaraVersion>5.2020.2</payaraVersion>  
    <final.name>demo</final.name> 
  </properties>  
  <dependencies> 
    <dependency> 
      <groupId>org.eclipse.microprofile</groupId>  
      <artifactId>microprofile</artifactId>  
      <version>3.3</version>  
      <type>pom</type>  
      <scope>provided</scope> 
    </dependency> 
  </dependencies>  
  <build> 
    <finalName>demo</finalName>  
    <plugins>
      <plugin>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>azure-webapp-maven-plugin</artifactId>
        <version>1.14.0</version>
        <configuration>
          <schemaVersion>v2</schemaVersion>
          <subscriptionId>xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx</subscriptionId>
          <resourceGroup>demo-xxxxxxxxxxxxxx-rg</resourceGroup>
          <appName>demo-xxxxxxxxxxxxxx</appName>
          <pricingTier>P1v2</pricingTier>
          <region>westeurope</region>
          <runtime>
            <os>Linux</os>
            <javaVersion>java11</javaVersion>
            <webContainer>java11</webContainer>
          </runtime>
          <appSettings>
            <property>
              <name>PORT</name>
              <value>8080</value>
            </property>
              <property>
              <name>WEBSITES_PORT</name>
              <value>8080</value>
            </property>
            <property>
              <name>WEBSITES_CONTAINER_START_TIME_LIMIT</name>
              <value>600</value>
            </property>
          </appSettings>
          <deployment>
            <resources>
              <resource>
                <directory>${project.basedir}/target</directory>
                <includes>
                  <include>*.jar</include>
                </includes>
              </resource>
            </resources>
          </deployment>
        </configuration>
      </plugin>
    </plugins>
  </build>  
  <profiles> 
    <profile> 
      <id>payara-micro</id>  
      <activation> 
        <activeByDefault>true</activeByDefault> 
      </activation>  
      <build> 
        <plugins> 
          <plugin> 
            <groupId>fish.payara.maven.plugins</groupId>  
            <artifactId>payara-micro-maven-plugin</artifactId>  
            <version>1.0.5</version>  
            <executions> 
              <execution> 
                <phase>package</phase>  
                <goals> 
                  <goal>bundle</goal> 
                </goals> 
              </execution> 
            </executions>  
            <configuration> 
              <payaraVersion>${payaraVersion}</payaraVersion> 
            </configuration> 
          </plugin>  
        </plugins> 
      </build> 
    </profile> 
  </profiles> 
</project>

あとはビルドしてデプロイするだけです。

mvn clean package
mvn com.microsoft.azure:azure-webapp-maven-plugin:1.14.0:deploy

アクセスすると以下の表示になります。

スクリーンショット 2021-05-08 9.03.19.png

helloエンドポイントにアクセスるとHello Worldが表示されます。

スクリーンショット 2021-05-08 9.03.08.png

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1