3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Jakarta EE10でもUberJarでお気軽開発をする〜Wildfly編〜

Posted at

はじめに

本記事はMDC Advent Calendar 2022の17日目の記事です。バックデートですが、これもアドベントカレンダーのやりかたの一つですね。間に合わなくても記事を積み重ねることの方が大事です。

Jakarta EE10でもUberJarで軽量に、素早く開発するぞ、ということで前回はPayaraをターゲットに動作確認しました。

今回はWildflyでやっていきます。
前回使ったソースコードをベースに修正していくことでWildflyに対応していきたいと思います。

Wildflyとは

Wildflyの歴史は長いのですが、端折って説明します。

Redhat社が開発しているアプリケーションサーバーにJBossというものがありました。その後、商用版とOSS版で名称を変えることとなり、商用版はJBoss Enterprise Application Platform(JBoss EAP)、OSS版はコミュニティ投票により決められたWildflyとなっています。

WildFly Bootable JARに対応していく

WildflyではUberJar、実行可能Jarのことを Bootable JARと呼んでいます。主にはMavenプラグインでツールが提供されており、ガイドもしっかり用意されています。

Wildflyのバージョンは、以下の箇所で指定します。

wildfly@maven(org.jboss.universe:community-universe)#27.0.0.0.Final

27.0.0.0.Finalになっていますが、ちょうど12/16に27.0.1がリリースされたところですのでそこを変えて取り込みます。

pom.xmlの修正

ビルドプラグインの設定のところを以下のように修正します。

pom.xml
            <plugin>
                <groupId>org.wildfly.plugins</groupId>
                <artifactId>wildfly-jar-maven-plugin</artifactId>
                <version>8.1.0.Final</version>
                <configuration>
                    <feature-pack-location>wildfly@maven(org.jboss.universe:community-universe)#27.0.1.Final</feature-pack-location>
                    <layers>
                        <layer>jaxrs</layer>
                    </layers>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>package</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

layersのところに、どのコンポーネントを使うのかを指定します。今回はJakarta RESTful Web Servicesだけ使えればよいので、jaxrsのみ設定しています。

特別な設定はこれだけです。簡単ですね。

動作確認

通常のビルド、実行と同様です。mvn packageでjarを作成し、java -jar 作られたjarでJakarta EEサーバーにデプロイされたアプリケーションのように試すことができます。

試した動作環境は以下の通りです。

\$ mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /Users/xxxx/.sdkman/candidates/maven/current
Java version: 11.0.7, vendor: AdoptOpenJDK, runtime: /Users/xxxx/.sdkman/candidates/java/11.0.7.hs-adpt
Default locale: ja_JP, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"

  • ビルド
\$ mvn package                          
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.example:jeedemo:war:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 61, column 21
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] ------------------------< com.example:jeedemo >-------------------------
[INFO] Building jeedemo 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
・・・
[INFO] Building war: /Users/xxxx/Downloads/jeedemo/target/jeedemo.war
[INFO] 
[INFO] --- wildfly-jar-maven-plugin:8.1.0.Final:package (default) @ jeedemo ---
[INFO] Provisioning server configuration based on the set of configured layers
[INFO] Building server based on [[wildfly@maven(org.jboss.universe:community-universe)#27.0.1.Final inherit-packages=false inheritConfigs=false]] galleon feature-packs
[INFO] Found boot artifact org.wildfly.core:wildfly-jar-boot:jar:19.0.1.Final:provided in org.wildfly:wildfly-ee-galleon-pack:27.0.1.Final
12月 19, 2022 12:49:23 午前 org.wildfly.core.embedded.LoggerContext$JBossLoggingModuleLogger greeting
INFO: JBoss Modules version 2.0.3.Final
12月 19, 2022 12:49:23 午前 org.jboss.msc.service.ServiceContainerImpl <clinit>
INFO: JBoss MSC version 1.4.13.Final
12月 19, 2022 12:49:23 午前 org.jboss.threads.Version <clinit>
INFO: JBoss Threads version 2.4.0.Final
12月 19, 2022 12:49:24 午前 org.jboss.as.server.ApplicationServerService start
INFO: WFLYSRV0049: WildFly Full 27.0.1.Final (WildFly Core 19.0.1.Final) starting
12月 19, 2022 12:49:24 午前 org.jboss.as.server.suspend.SuspendController resume
INFO: WFLYSRV0212: Resuming server
12月 19, 2022 12:49:24 午前 org.jboss.as.server.BootstrapListener done
INFO: WFLYSRV0025: WildFly Full 27.0.1.Final (WildFly Core 19.0.1.Final) started in 1442ms - Started 28 of 31 services (3 services are lazy, passive or on-demand) - Server configuration file in use: standalone.xml
12月 19, 2022 12:49:26 午前 org.jboss.as.server.ApplicationServerService stop
INFO: WFLYSRV0050: WildFly Full 27.0.1.Final (WildFly Core 19.0.1.Final) stopped in 48ms
[INFO] CLI executions are done in forked process
[INFO] Executing CLI, Server configuration
[INFO] CLI scripts execution done.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  43.798 s
[INFO] Finished at: 2022-12-19T00:49:51+09:00
[INFO] ------------------------------------------------------------------------

ビルドのログを見ると一度warファイルを作った後に、
wildfly-jar-maven-pluginを使ってJarファイルを作成していることが分かります。

targetディレクトリ以下を見ると以下のようにファイル、フォルダが作られていました。

\$ ls -ltr target/ 
total 123560
drwxr-xr-x  3 xxxx  staff        96 Dec 10 00:21 maven-status
drwxr-xr-x  3 xxxx  staff        96 Dec 10 00:21 generated-sources
drwxr-xr-x  6 xxxx  staff       192 Dec 10 00:21 jeedemo
drwxr-xr-x  3 xxxx  staff        96 Dec 10 00:21 maven-archiver
drwxr-xr-x  5 xxxx  staff       160 Dec 13 09:01 classes
-rw-r--r--  1 xxxx  staff      4432 Dec 19 00:49 jeedemo.war
-rw-r--r--  1 xxxx  staff  63140763 Dec 19 00:49 jeedemo-bootable.jar
drwxr-xr-x  5 xxxx  staff       160 Dec 19 00:49 bootable-jar-build-artifacts

実行させると以下のような形でした。

\$ java -jar target/jeedemo-bootable.jar
01:03:40,322 INFO  [org.wildfly.jar] (main) WFLYJAR0007: Installed server and application in /var/folders/1m/v_pnk3kd3hj0558d1z3nsnzh0000gn/T/wildfly-bootable-server10190944843109880212, took 1907ms
01:03:41,031 INFO  [org.wildfly.jar] (main) WFLYJAR0008: Server options: [--read-only-server-config=standalone.xml]
01:03:41,220 INFO  [org.jboss.msc] (main) JBoss MSC version 1.4.13.Final
01:03:41,233 INFO  [org.jboss.threads] (main) JBoss Threads version 2.4.0.Final
01:03:41,427 INFO  [org.jboss.as] (MSC service thread 1-2) WFLYSRV0049: WildFly Full 27.0.1.Final (WildFly Core 19.0.1.Final) starting
01:03:42,387 INFO  [org.wildfly.extension.microprofile.config.smallrye] (ServerService Thread Pool -- 16) WFLYCONF0001: Activating MicroProfile Config Subsystem
01:03:42,393 INFO  [org.jboss.as.naming] (ServerService Thread Pool -- 17) WFLYNAM0001: Activating Naming Subsystem
01:03:42,419 INFO  [org.jboss.as.jaxrs] (ServerService Thread Pool -- 15) WFLYRS0016: RESTEasy version 6.2.1.Final
01:03:42,451 INFO  [org.xnio] (ServerService Thread Pool -- 14) XNIO version 3.8.8.Final
01:03:42,467 INFO  [org.jboss.as.naming] (MSC service thread 1-5) WFLYNAM0003: Starting Naming Service
01:03:42,488 INFO  [org.xnio.nio] (ServerService Thread Pool -- 14) XNIO NIO Implementation Version 3.8.8.Final
01:03:42,528 INFO  [org.wildfly.extension.io] (ServerService Thread Pool -- 14) WFLYIO001: Worker 'default' has auto-configured to 8 IO threads with 64 max task threads based on your 4 available processors
01:03:42,545 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-8) WFLYUT0003: Undertow 2.3.0.Final starting
01:03:42,740 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-4) WFLYUT0012: Started server default-server.
01:03:42,748 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-3) Queuing requests.
01:03:42,749 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-3) WFLYUT0018: Host default-host starting
01:03:42,836 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-1) WFLYDS0013: Started FileSystemDeploymentService for directory /var/folders/1m/v_pnk3kd3hj0558d1z3nsnzh0000gn/T/wildfly-bootable-server10190944843109880212/standalone/deployments
01:03:42,851 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-3) WFLYSRV0027: Starting deployment of "jeedemo.war" (runtime-name: "ROOT.war")
01:03:42,997 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-4) WFLYUT0006: Undertow HTTP listener default listening on 127.0.0.1:8080
01:03:43,767 INFO  [org.jboss.weld.deployer] (MSC service thread 1-4) WFLYWELD0003: Processing weld deployment ROOT.war
01:03:44,201 INFO  [org.hibernate.validator.internal.util.Version] (MSC service thread 1-4) HV000001: Hibernate Validator 8.0.0.Final
01:03:44,674 INFO  [org.jboss.weld.Version] (MSC service thread 1-7) WELD-000900: 5.1.0 (Final)
01:03:44,896 INFO  [org.jboss.weld.Bootstrap] (MSC service thread 1-7) WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
01:03:46,905 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 20) RESTEASY002225: Deploying jakarta.ws.rs.core.Application: class com.example.jeedemo.JeedemoRestApplication$Proxy$_$$_WeldClientProxy
01:03:47,026 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 20) WFLYUT0021: Registered web context: '/' for server 'default-server'
01:03:47,028 INFO  [org.jboss.as.server] (Controller Boot Thread) WFLYSRV0010: Deployed "jeedemo.war" (runtime-name : "ROOT.war")
01:03:47,097 INFO  [org.jboss.as.server] (Controller Boot Thread) WFLYSRV0212: Resuming server
01:03:47,105 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 27.0.1.Final (WildFly Core 19.0.1.Final) started in 6062ms - Started 138 of 143 services (20 services are lazy, passive or on-demand) - Server configuration file in use: standalone.xml

6秒ぐらいで起動しています。早いですね!

動作アプリの動きはこれまで通りです。

image.png

まとめ

今回はWildflyを使ってUberJar、Wildflyドキュメントの言い方ですと、Bootable Jar化を実現してみました。
使ったソースコードは以下に置いてますので、参考にしてください。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?