付録D WAR ファイルを使用した Spring Boot アプリケーションのデプロイ
ファット JAR ファイルを使用してサポートされているアプリケーションのパッケージ化とデプロイのワークフローの代わりに、Spring Boot アプリケーションを WAR (Web アプリケーションアーカイブ) ファイルとしてパッケージ化してデプロイできます。アプリケーションが OpenShift で正しくビルドし、デプロイするように、ビルドおよびデプロイメントの設定を行う必要があります。
前提条件
- サンプル などの Spring Boot アプリケーション。
- アプリケーションを OpenShift にデプロイするのに使用される Fabric8 Maven Plugin。
- アプリケーションのパッケージ化に使用される Spring Boot Maven プラグイン。
手順
war
パッケージをプロジェクトのpom.xml
ファイルに追加します。pom.xml
の例<project ...> ... <packaging>war</packaging> ... </project>
spring-boot-starter-tomcat
をアプリケーションの依存関係として指定します。pom.xml
の例<project ...> ... <dependencies> ... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> ... </dependencies> ... </project>
Spring Boot Maven プラグインの Maven ゴール
repackage
がpom.xml
ファイルで定義されているようにしてください。pom.xml
の例<project ...> ... <build> ... <plugins> ... <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ... </project>
これにより、アプリケーションの起動に使用される Spring Boot クラスが WAR ファイルに含まれ、これらのクラスの対応するプロパティーが WAR ファイルの
MANIFEST.mf
ファイルで定義されているようになります。-
Main-Class: org.springframework.boot.loader.WarLauncher
-
Spring-Boot-Classes: WEB-INF/classes/
-
Spring-Boot-Lib: WEB-INF/lib/
-
Spring-Boot-Version: 2.2.11
-
ARTIFACT_COPY_ARGS
環境変数をpom.xml
ファイルに追加します。Fabric8 Maven プラグインは、ビルドプロセス中にこの変数を使用し、Build および Deploy ツールは WAR ファイル (デフォルトのファット JAR ファイルよりも) を使用してアプリケーションコンテナーイメージを作成します。
pom.xml
の例... <profile> <id>openshift</id> <build> <plugins> <plugin> <groupId>io.fabric8</groupId> <artifactId>fabric8-maven-plugin</artifactId> <executions> ... </executions> <configuration> <images> <image> <name>${project.artifactId}:%t</name> <alias>${project.artifactId}</alias> <build> <from>registry.access.redhat.com/redhat-openjdk-18/openjdk18-openshift:${openjdk18-openshift.version}</from> <assembly> <basedir>/deployments</basedir> <descriptorRef>artifact</descriptorRef> </assembly> <env> <ARTIFACT_COPY_ARGS>*.war</ARTIFACT_COPY_ARGS> <JAVA_APP_DIR>/deployments</JAVA_APP_DIR> </env> <ports> <port>8080</port> </ports> </build> </image> </images> </configuration> </plugin> </plugins> </build> </profile> ...
JAVA_APP_JAR
環境変数をsrc/main/fabric8/deployment.yml
ファイルに追加します。この変数は Fabric8 Maven Plugin に対して、コンテナーに含まれる WAR ファイルを使用してアプリケーションを起動します。
src/main/fabric8/deployment.yml
が存在しない場合は、これを作成できます。deployment.yml
の例spec: template: spec: containers: ... env: - name: JAVA_APP_JAR value: ${project.artifactId}-${project.version}.war
アプリケーションをビルドしてデプロイします。
mvn clean fabric8:deploy -Popenshift