ããã¯ããªã«ãããããŠæžãããã®ïŒ
Jakarta EEã¢ããªã±ãŒã·ã§ã³ãéçºããæã«ãWildFly Maven Pluginã®devãŽãŒã«ã䜿ã£ãããããšæ¥œããã ãªãšæã£ãã®ã§ãã¡ãã£ãš
è©ŠããŠã¿ãããšã«ããŸããã
åããããªçºæ³ã§WildFly Bootable JARãè©Šããããšã¯ãããŸãããWildFly Maven Pluginã¯ãããã€ïŒã¢ã³ãããã€ãããã§ãã
䜿ã£ãããšããªãã®ã§ã
ããããéå»ã®ãšã³ããªãŒãèŠããšWildFlyããŒã¹ã ã£ãInfinispan Serverãžã®ãããã€ã«äœ¿ã£ããããŠããŸããâŠã
WildFly Maven Pluginで、WildFly/Infinispan Serverにデプロイする - CLOVER🍀
WildFly Maven Plugin
WildFly Maven Pluginã¯ãGallenã䜿ã£ãŠWildFlyãµãŒããŒãããããžã§ãã³ã°ããWildFlyãµãŒããŒãžã®ãããã€ãåãããã€ã
ã¢ã³ãããã€ãã¢ããªã±ãŒã·ã§ã³ã®å®è¡ãã§ããMavenãã©ã°ã€ã³ã§ãã
The wildfly-maven-plugin is used to provision a server using Galleon, package your application in Galleon provisioned server, deploy, redeploy, undeploy or run your application.
WildFly Maven Plugin – WildFly Maven Plugin (wildfly-maven-plugin)
çŸããŒãžã§ã³ã¯ã4.2.0.Finalã§ãã
䜿ãæ¹ã¯ããã¡ããèŠã€ã€
ããããèå³ã®ãããŽãŒã«ã®åã ã®ããŒãžãèŠããšããã§ãããã
WildFly Maven Plugin (wildfly-maven-plugin) / Goals Overview
ãµã³ãã«ããããŸãã
WildFly Maven Plugin (wildfly-maven-plugin) / Examples
çšæãããŠãããŽãŒã«ãšããŠã¯ããã£ãã以äžã®æãã¿ããã§ããã
- WildFlyã®èµ·åãåæ¢
- WildFlyãžã®ã¢ããªã±ãŒã·ã§ã³ã®ãããã€ãåãããã€ãã¢ã³ãããã€
- WildFlyãµãŒããŒã®ããããžã§ãã³ã°ãã¢ããªã±ãŒã·ã§ã³ãå«ãã WildFlyãµãŒããŒã®ããããžã§ãã³ã°
- ã³ã³ããã€ã¡ãŒãžã®äœæ
- WildFlyãµãŒããŒãžã®ãªãœãŒã¹ã®è¿œå ãã³ãã³ãã®å®è¡
- WildFlyãµãŒããŒãèµ·åããã¢ããªã±ãŒã·ã§ã³ã®ãœãŒã¹ã³ãŒãã®å€æŽãç£èŠããŠãã«ããåãããã€
ä»åã¯äž»ã«æåŸã«æžããdevãŽãŒã«ã«ã€ããŠãè©ŠããŠãããããšæããŸãã
WildFly Maven Plugin – wildfly:dev
wildfly:dev
devãŽãŒã«ã¯ãWildFlyãµãŒããŒãèµ·åããŠã¢ããªã±ãŒã·ã§ã³ãããã±ãŒãžã³ã°ããããã€ããŸãããã®åŸãã¢ããªã±ãŒã·ã§ã³ã®ãœãŒã¹ã³ãŒãã®
å€æŽãç£èŠããŠãå€æŽããã£ãå Žåã¯å床ããã±ãŒãžã³ã°ãåãããã€ãè¡ããŸãã
WildFly Maven Plugin – wildfly:dev
WildFly Maven Plugin 4.1.0.Finalãã䜿ãããŽãŒã«ã¿ãããªã®ã§ãå²ãšæè¿ïŒ2023幎4æïŒã®æ©èœãªãã§ããã
Release 4.1.0.Final · wildfly/wildfly-maven-plugin · GitHub
ä»åã¯ããã¡ãã䜿ã£ãŠç°¡åãªJakarta EEã¢ããªã±ãŒã·ã§ã³ãäœã£ãŠè©ŠããŠã¿ãããšæããŸãã
ç°å¢
ä»åã®ç°å¢ã¯ããã¡ãã
$ java --version openjdk 17.0.8.1 2023-08-24 OpenJDK Runtime Environment (build 17.0.8.1+1-Ubuntu-0ubuntu122.04) OpenJDK 64-Bit Server VM (build 17.0.8.1+1-Ubuntu-0ubuntu122.04, mixed mode, sharing) $ mvn --version Apache Maven 3.9.4 (dfbb324ad4a7c8fb0bf182e6d91b0ae20e3d2dd9) Maven home: $HOME/.sdkman/candidates/maven/current Java version: 17.0.8.1, vendor: Private Build, runtime: /usr/lib/jvm/java-17-openjdk-amd64 Default locale: ja_JP, platform encoding: UTF-8 OS name: "linux", version: "5.15.0-83-generic", arch: "amd64", family: "unix"
æºå
Mavenãããžã§ã¯ãã®æ§æã¯ããããªæãã«ããŸããã
<groupId>...</groupId> <artifactId>...</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <dependencies> <dependency> <groupId>jakarta.platform</groupId> <artifactId>jakarta.jakartaee-web-api</artifactId> <version>10.0.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>ROOT</finalName> <plugins> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.4.0</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <groupId>org.wildfly.plugins</groupId> <artifactId>wildfly-maven-plugin</artifactId> <version>4.2.0.Final</version> <configuration> <version>29.0.1.Final</version> <server-config>standalone.xml</server-config> </configuration> </plugin> </plugins> </build>
Jakarta EEã¯Web Profileã§10ã䜿ãããšã«ããŸãã
WildFly Maven Pluginã¯ãã¡ãã§ããã
<plugin> <groupId>org.wildfly.plugins</groupId> <artifactId>wildfly-maven-plugin</artifactId> <version>4.2.0.Final</version> <configuration> <version>29.0.1.Final</version> <server-config>standalone.xml</server-config> </configuration> </plugin>
èšå®ã¯ãæäœéversion
ã¯æå®ããæ¹ãè¯ãããªãšãæªæå®ã ãšãææ°å®å®çã®WildFlyãããŠã³ããŒãããŠããŸãã
ãŸããserver-config
ã§ã¯äœ¿çšããèšå®ãã¡ã€ã«ãæå®ããŠããŸãã
ã¢ããªã±ãŒã·ã§ã³ãäœæãã
ã²ãšãŸããç°¡åãªJakarata EEã¢ããªã±ãŒã·ã§ã³ãäœããŸãããŸãã¯Jakarta RESTful Web ServicesïŒJAX-RSïŒããã
JAX-RSã®æå¹åã
src/main/java/org/littlewings/wildfly/maven/JaxrsActivator.java
package org.littlewings.wildfly.maven; import jakarta.ws.rs.ApplicationPath; import jakarta.ws.rs.core.Application; @ApplicationPath("") public class JaxrsActivator extends Application { }
JAX-RSãªãœãŒã¹ã¯ã©ã¹ã
src/main/java/org/littlewings/wildfly/maven/HelloResource.java
package org.littlewings.wildfly.maven; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; import jakarta.ws.rs.Produces; import jakarta.ws.rs.core.MediaType; @Path("hello") public class HelloResource { @GET @Produces(MediaType.TEXT_PLAIN) public String message() { return "Hello World"; } }
ããã§wildfly:dev
ãŽãŒã«ãå®è¡ããŸãã
$ mvn wildfly:dev
ååã¯WildFlyã«é¢ããã¢ãŒãã£ãã¡ã¯ããããŠã³ããŒãããŠããã®ã§ããããªãã«æéãããããŸãã
ããããWildFlyãããããžã§ãã³ã°ããŠèµ·åã
[INFO] --- wildfly:4.2.0.Final:dev (default-cli) @ ... --- [INFO] Provisioning default server in /path/to/target/server [INFO] JBoss Threads version 2.4.0.Final [INFO] JBoss Remoting version 5.0.27.Final [INFO] XNIO version 3.8.9.Final [INFO] XNIO NIO Implementation Version 3.8.9.Final [INFO] ELY00001: WildFly Elytron version 2.2.1.Final [INFO] JAVA_HOME : /usr/lib/jvm/java-17-openjdk-amd64 [INFO] JBOSS_HOME: /path/to/target/server [INFO] JAVA_OPTS : -Xms64m -Xmx512m -Djava.net.preferIPv4Stack=true -Djava.awt.headless=true -Djboss.modules.system.pkgs=org.jboss.byteman --add-exports=java.base/sun.nio.ch=ALL-UNNAMED --add-exports=jdk.unsupported/sun.reflect=ALL-UNNAMED --add-exports=jdk.unsupported/sun.misc=ALL-UNNAMED --add-modules=java.se [INFO] STANDALONE server is starting up. ãçç¥ã 21:33:26,953 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management 21:33:26,953 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990 21:33:26,954 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 29.0.1.Final (WildFly Core 21.1.1.Final) started in 2855ms - Started 280 of 522 services (317 services are lazy, passive or on-demand) - Server configuration file in use: standalone.xml
ã¢ããªã±ãŒã·ã§ã³ãããã±ãŒãžã³ã°ããŠãããã€ããŸãã
[INFO] Copying 0 resource from src/main/resources to target/classes [INFO] Changes detected - recompiling the module! :source [INFO] Compiling 2 source files with javac [debug target 17] to target/classes [INFO] Exploding webapp [INFO] Assembling webapp [...] in [/path/to/target/ROOT] [INFO] Processing war project 21:33:28,359 INFO [org.jboss.as.server.deployment] (MSC service thread 1-1) WFLYSRV0027: Starting deployment of "ROOT.war" (runtime-name: "ROOT.war") 21:33:29,187 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 22) RESTEASY002225: Deploying jakarta.ws.rs.core.Application: class org.littlewings.wildfly.maven.JaxrsActivator 21:33:29,220 INFO [org.hibernate.validator.internal.util.Version] (ServerService Thread Pool -- 22) HV000001: Hibernate Validator 8.0.0.Final 21:33:29,239 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 22) WFLYUT0021: Registered web context: '/' for server 'default-server' 21:33:29,299 INFO [org.jboss.as.server] (management-handler-thread - 1) WFLYSRV0010: Deployed "ROOT.war" (runtime-name : "ROOT.war")
ãã®ç¶æ ã§ã¢ããªã±ãŒã·ã§ã³ã¯ãããã€ãããŠããã®ã§ãã¢ã¯ã»ã¹ããŠç¢ºèªããããšãã§ããŸãã
$ curl localhost:8080/hello Hello World
ããã§mvn wildfly:dev
ã¯å®è¡ãããŸãŸããœãŒã¹ã³ãŒããå°ãå€æŽããŠã¿ãŸãã
@GET @Produces(MediaType.TEXT_PLAIN) public String message() { return "Hello World!!"; }
ãããšãå€æŽãæ€ç¥ãããŠå床ããã±ãŒãžã³ã°ãšåãããã€ãè¡ãããŸãã
[INFO] Copying 0 resource from src/main/resources to target/classes [INFO] Changes detected - recompiling the module! :source [INFO] Compiling 2 source files with javac [debug target 17] to target/classes [INFO] Exploding webapp [INFO] Assembling webapp [...] in [/path/to/target/ROOT] [INFO] Processing war project 21:33:28,359 INFO [org.jboss.as.server.deployment] (MSC service thread 1-1) WFLYSRV0027: Starting deployment of "ROOT.war" (runtime-name: "ROOT.war") 21:33:29,187 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 22) RESTEASY002225: Deploying jakarta.ws.rs.core.Application: class org.littlewings.wildfly.maven.JaxrsActivator 21:33:29,220 INFO [org.hibernate.validator.internal.util.Version] (ServerService Thread Pool -- 22) HV000001: Hibernate Validator 8.0.0.Final 21:33:29,239 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 22) WFLYUT0021: Registered web context: '/' for server 'default-server' 21:33:29,299 INFO [org.jboss.as.server] (management-handler-thread - 1) WFLYSRV0010: Deployed "ROOT.war" (runtime-name : "ROOT.war") [INFO] Changes detected - recompiling the module! :source [INFO] Compiling 2 source files with javac [debug target 17] to target/classes [INFO] Exploding webapp [INFO] Assembling webapp [...] in [/path/to/target/ROOT] [INFO] Processing war project 21:36:25,861 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 22) WFLYUT0022: Unregistered web context: '/' from server 'default-server' 21:36:25,880 INFO [org.jboss.as.server.deployment] (MSC service thread 1-5) WFLYSRV0028: Stopped deployment ROOT.war (runtime-name: ROOT.war) in 22ms 21:36:25,885 INFO [org.jboss.as.server.deployment] (MSC service thread 1-2) WFLYSRV0027: Starting deployment of "ROOT.war" (runtime-name: "ROOT.war") 21:36:26,004 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 81) RESTEASY002225: Deploying jakarta.ws.rs.core.Application: class org.littlewings.wildfly.maven.JaxrsActivator 21:36:26,006 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 81) WFLYUT0021: Registered web context: '/' for server 'default-server' 21:36:26,026 INFO [org.jboss.as.server] (management-handler-thread - 1) WFLYSRV0013: Redeployed "ROOT.war" [INFO] Nothing to compile - all classes are up to date [INFO] Exploding webapp [INFO] Assembling webapp [...] in [/path/to/target/ROOT] [INFO] Processing war project 21:36:26,042 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 22) WFLYUT0022: Unregistered web context: '/' from server 'default-server' 21:36:26,047 INFO [org.jboss.as.server.deployment] (MSC service thread 1-3) WFLYSRV0028: Stopped deployment ROOT.war (runtime-name: ROOT.war) in 5ms 21:36:26,048 INFO [org.jboss.as.server.deployment] (MSC service thread 1-5) WFLYSRV0027: Starting deployment of "ROOT.war" (runtime-name: "ROOT.war") 21:36:26,248 INFO [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 22) RESTEASY002225: Deploying jakarta.ws.rs.core.Application: class org.littlewings.wildfly.maven.JaxrsActivator 21:36:26,251 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 22) WFLYUT0021: Registered web context: '/' for server 'default-server' 21:36:26,268 INFO [org.jboss.as.server] (management-handler-thread - 1) WFLYSRV0013: Redeployed "ROOT.war"
確èªã
$ curl localhost:8080/hello Hello World!!
å€æŽãåæ ãããŠããŸãã
䟿å©ã§ããã
ãšããã§ããWildFlyãµãŒããŒãããããžã§ãã³ã°ããããšèšã£ãŠããŸããããããã¯ã©ãããããšããšãããštarget/server
ãã£ã¬ã¯ããª
ïŒprovisioningDir
ã§å€æŽå¯èœïŒã«WildFlyãæ§ç¯ãããããšãæããŠããŸãã
$ ll target/server åèš 524 drwxrwxr-x 12 xxxxx xxxxx 4096 9æ 18 21:33 ./ drwxrwxr-x 7 xxxxx xxxxx 4096 9æ 18 21:33 ../ drwxrwxr-x 3 xxxxx xxxxx 4096 9æ 18 21:33 .galleon/ drwxrwxr-x 2 xxxxx xxxxx 4096 9æ 18 21:33 .installation/ drwxrwxr-x 3 xxxxx xxxxx 4096 9æ 18 21:33 .well-known/ -rw-rw-r-- 1 xxxxx xxxxx 26530 9æ 18 21:32 LICENSE.txt -rw-rw-r-- 1 xxxxx xxxxx 2161 9æ 18 21:32 README.txt drwxrwxr-x 3 xxxxx xxxxx 4096 9æ 18 21:32 appclient/ drwxrwxr-x 3 xxxxx xxxxx 4096 9æ 18 21:32 bin/ -rw-rw-r-- 1 xxxxx xxxxx 19358 9æ 18 21:32 copyright.txt drwxrwxr-x 6 xxxxx xxxxx 4096 9æ 18 21:32 docs/ drwxrwxr-x 4 xxxxx xxxxx 4096 9æ 18 21:33 domain/ -rw-rw-r-- 1 xxxxx xxxxx 431393 9æ 18 21:33 jboss-modules.jar drwxrwxr-x 3 xxxxx xxxxx 4096 9æ 18 21:32 modules/ drwxrwxr-x 8 xxxxx xxxxx 4096 9æ 18 21:33 standalone/ drwxrwxr-x 2 xxxxx xxxxx 4096 9æ 18 21:32 welcome-content/
èšå®ãã¡ã€ã«ãªã©ããã£ãããããŸãã
$ ll target/server/standalone/configuration åèš 244 drwxrwxr-x 3 xxxxx xxxxx 4096 9æ 18 21:36 ./ drwxrwxr-x 8 xxxxx xxxxx 4096 9æ 18 21:33 ../ -rw------- 1 xxxxx xxxxx 711 9æ 18 21:32 application-roles.properties -rw------- 1 xxxxx xxxxx 935 9æ 18 21:32 application-users.properties -rw-rw-r-- 1 xxxxx xxxxx 1754 9æ 18 21:33 logging.properties -rw------- 1 xxxxx xxxxx 669 9æ 18 21:32 mgmt-groups.properties -rw------- 1 xxxxx xxxxx 1112 9æ 18 21:32 mgmt-users.properties -rw-rw-r-- 1 xxxxx xxxxx 38419 9æ 18 21:33 standalone-full-ha.xml -rw-rw-r-- 1 xxxxx xxxxx 32949 9æ 18 21:33 standalone-full.xml -rw-rw-r-- 1 xxxxx xxxxx 34548 9æ 18 21:33 standalone-ha.xml -rw-rw-r-- 1 xxxxx xxxxx 12643 9æ 18 21:33 standalone-load-balancer.xml -rw-rw-r-- 1 xxxxx xxxxx 26082 9æ 18 21:33 standalone-microprofile-ha.xml -rw-rw-r-- 1 xxxxx xxxxx 22920 9æ 18 21:33 standalone-microprofile.xml -rw-rw-r-- 1 xxxxx xxxxx 29855 9æ 18 21:36 standalone.xml drwxrwxr-x 4 xxxxx xxxxx 4096 9æ 18 21:36 standalone_xml_history/
Jakarta PersistenceïŒJPAïŒã䜿ã
JAX-RSã ãã§çµãã£ãŠããªããªã®ã§ãJPAã䜿ã£ãŠã¿ãŸãããã
wildfly:dev
ãŽãŒã«ãå®è¡ããŠããã¿ãŒããã«ã¯ã1床åæ¢ããŸãã
JPAã䜿ããšããããšã¯ãJDBCãã©ã€ããŒãããŒã¿ãœãŒã¹ãç»é²ããããšã«ãªããŸãã
ãããã£ããµã³ãã«ã¯ãã¡ãã«ãããŸãããdevãŽãŒã«ãšã¯ã¡ãã£ãšçžæ§ãæªãã§ããPostgreSQLã®JDBCãã©ã€ããŒãšããŒã¿ãœãŒã¹ã
ãããã€ãç»é²ãããµã³ãã«ã«ãªã£ãŠããã®ã§ãããããã¯ãã§ã«èµ·åæžã¿ã®WildFlyãå©çšãããã®ã ããã§ããã
â»devãŽãŒã«ã§ããªã¢ãŒãã®WildFlyã䜿ãããã«èšå®ããããšã¯ã§ããŸã
WildFly Maven Plugin – Adding Resources Examples
èµ·åæžã¿ã®WildFlyã䜿ãå Žåã¯ã以éã®èšèŒã¯èªã¿é£ã°ããŠããã§ãããã
ä»åã®æ§æã§é²ããå Žåã¯ã©ãããããšãããšãJDBCãã©ã€ããŒãããŒã¿ãœãŒã¹ãçµã¿èŸŒãã WildFlyãããããžã§ãã³ã°ããŸãã
ããã«ã¯Galleonã䜿ãããšã«ãªããŸãã
以åãWildFly Bootable JARãè©ŠããŠããæã«å°ã調ã¹ãŠã¿ãŸããã
WildFly Bootable JARを作る時に指定するGalleon layer、Galleon feature-packというものを少し見てみたい - CLOVER🍀
Galleonã䜿ãããšã§ãWildFlyã奜ããªæ§æã«çµã¿äžããããšãã§ããŸãã
WildFly Maven Pluginã§ããã¡ãã«ç¹åãããšã³ããªãŒã¯ãã¡ããåç §ããŠãã ããã
WildFly Maven Pluginで、WildFlyをプロビジョニングしたり、アプリケーションを含めたりする - CLOVER🍀
ããŒã¿ãœãŒã¹ãšJDBCãã©ã€ããŒã®ãããã€ã«ã¯ãWildFly DataSources Galleon Feature Packã䜿çšããŸãã
GitHub - wildfly-extras/wildfly-datasources-galleon-pack: WildFly Feature Pack for DataSources
ããã¯WildFly Bootable JARã§ã䜿ã£ãããšããããŸãã
WildFly Bootable JARに、DataSourceを組み込む - CLOVER🍀
䜿çšããããŒã¿ããŒã¹ã¯ãMySQLãšããŸããMySQLã¯8.0.34ã§ãIPã¢ãã¬ã¹ã¯172.17.0.2ã§åäœããŠãããã®ãšããŸãã
MySQL localhost:3306 ssl practice SQL > select version(); +-----------+ | version() | +-----------+ | 8.0.34 | +-----------+ 1 row in set (0.0006 sec)
WildFly Maven Pluginã®èšå®ã¯ãæçµçã«ãããªããŸããã
<plugin> <groupId>org.wildfly.plugins</groupId> <artifactId>wildfly-maven-plugin</artifactId> <version>4.2.0.Final</version> <configuration> <feature-packs> <feature-pack> <location>wildfly@maven(org.jboss.universe:community-universe)#29.0.1.Final</location> </feature-pack> <feature-pack> <location>org.wildfly:wildfly-datasources-galleon-pack:5.0.0.Final</location> </feature-pack> </feature-packs> <layers> <layer>jaxrs-server</layer> <layer>mysql-datasource</layer> </layers> <env> <MYSQL_DATASOURCE>MySqlDs</MYSQL_DATASOURCE> <MYSQL_JNDI>java:jboss/datasources/MySqlDs</MYSQL_JNDI> <MYSQL_URL>jdbc:mysql://172.17.0.2:3306/practice?characterEncoding=utf-8&connectionCollation=utf8mb4_0900_bin</MYSQL_URL> <MYSQL_USER>kazuhira</MYSQL_USER> <MYSQL_PASSWORD>password</MYSQL_PASSWORD> <MYSQL_ENABLED>true</MYSQL_ENABLED> </env> </configuration> </plugin>
WildFly DataSources Galleon Feature Packã®æ¹ã«WildFly Maven PluginãšWildFly JAR Maven Pluginã®ãµã³ãã«ãããã®ã§ããã
ãã¡ããåèã«ã
Provisioning using the WildFly Maven Plugin or the WildFly JAR Maven plugin
Galleonã®Feature PackãšããŠãWildFly 29.0.1.FinalãšWildFly DataSources Galleon Feature Packãæå®ã
<feature-packs> <feature-pack> <location>wildfly@maven(org.jboss.universe:community-universe)#29.0.1.Final</location> </feature-pack> <feature-pack> <location>org.wildfly:wildfly-datasources-galleon-pack:5.0.0.Final</location> </feature-pack> </feature-packs>
ã¬ã€ã€ãŒã«ã¯jaxrs-server
ãšmysql-datasource
ãéžæã
<layers> <layer>jaxrs-server</layer> <layer>mysql-datasource</layer> </layers>
jaxrs-server
ã¬ã€ã€ãŒã«ã€ããŠã¯ããã¡ãã«èšèŒããããŸãããã£ãããJAX-RSãCDIãJPAã䜿ããWildFlyãæ§ç¯ã§ããããããã«
æããŠããã°ãããšæããŸãã
mysql-datasource
ã¯WildFly DataSources Galleon Feature PackãæäŸããã¬ã€ã€ãŒã§ãã
MySQLã®JDBCãã©ã€ããŒã¯ãmysql-datasource
ã¬ã€ã€ãŒãå ãããšãããã€ãããŸãã
ããšã¯ãããŒã¿ãœãŒã¹ã®èšå®ãè¡ããŸããèšå®ã¯æ¥ç¶å
ã®ããŒã¿ããŒã¹ããšã«ããã®ã§ãããMySQLã®å Žåã¯ãã¡ãã
èšå®ã¯ãä»åã¯ç°å¢å€æ°ã§è¡ãããšã«ããŸããã
<env> <MYSQL_DATASOURCE>MySqlDs</MYSQL_DATASOURCE> <MYSQL_JNDI>java:jboss/datasources/MySqlDs</MYSQL_JNDI> <MYSQL_URL>jdbc:mysql://172.17.0.2:3306/practice?characterEncoding=utf-8&connectionCollation=utf8mb4_0900_bin</MYSQL_URL> <MYSQL_USER>kazuhira</MYSQL_USER> <MYSQL_PASSWORD>password</MYSQL_PASSWORD> <MYSQL_ENABLED>true</MYSQL_ENABLED> </env>
ããã§ãWildFlyåŽã®æºåã¯ã§ããŸããã
ããšã¯JPAã䜿ãããã«ã¢ããªã±ãŒã·ã§ã³ã«ãœãŒã¹ã³ãŒããè¿œå ããŸãã
Persistence Unitã®èšå®ãWildFly Maven Pluginã«ãã£ãŠæ§æãããããŒã¿ãœãŒã¹ã䜿ãããã«ããŠããŸãã
src/main/resources/META-INF/persistence.xml
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="https://jakarta.ee/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd" version="3.0"> <persistence-unit name="sample.pu" transaction-type="JTA"> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <jta-data-source>java:jboss/datasources/MySqlDs</jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="jakarta.persistence.schema-generation.database.action" value="drop-and-create"/> </properties> </persistence-unit> </persistence>
ã¹ããŒãçæã¯ãä»åã¯ç°¡åã«drop-and-create
ã«ããŠãããŸããâŠã
JPAã®ãšã³ãã£ãã£ã
src/main/java/org/littlewings/wildfly/maven/Book.java
package org.littlewings.wildfly.maven; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.Table; @Entity @Table(name = "book") public class Book { @Id private String isbn; private String title; private Integer price; // getterïŒsetterã¯çç¥ }
EntityManager
ã䜿ããJAX-RSãªãœãŒã¹ã¯ã©ã¹ã
src/main/java/org/littlewings/wildfly/maven/BookResource.java
package org.littlewings.wildfly.maven; import jakarta.enterprise.context.ApplicationScoped; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import jakarta.transaction.Transactional; import jakarta.ws.rs.*; import jakarta.ws.rs.core.MediaType; import java.util.List; @ApplicationScoped @Path("book") public class BookResource { @PersistenceContext EntityManager entityManager; @GET @Path("{isbn}") @Produces(MediaType.APPLICATION_JSON) @Transactional public Book find(@PathParam("isbn") String isbn) { return entityManager.find(Book.class, isbn); } @GET @Produces(MediaType.APPLICATION_JSON) @Transactional public List<Book> findAll() { return entityManager .createQuery("select b from Book b order by b.price desc", Book.class) .getResultList(); } @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Transactional public Book register(Book book) { if (entityManager.find(Book.class, book.getIsbn()) != null) { entityManager.merge(book); } else { entityManager.persist(book); } return book; } }
ãããŸã§èšå®ãããã1床mvn clean
ããŠãããŸããããããžã§ãã³ã°æžã¿ã®WildFlyããããšãèšå®ãå€ããŠãåããããžã§ãã³ã°ãããªã
ã¿ãããªã®ã§ã
$ mvn clean
ã§ã¯ãèµ·åã
$ mvn wildfly:dev
ãããšãWildFlyãµãŒããŒãããããžã§ãã³ã°ãããã®ã§ãããå°ããã°ã®é°å²æ°ãå€ãããŸãã
[INFO] --- wildfly:4.2.0.Final:dev (default-cli) @ ... --- [INFO] Provisioning server in /path/to/target/server [INFO] Resolving feature-packs [INFO] Installing packages [INFO] Resolving artifacts [INFO] Generating configurations [INFO] JBoss Modules version 2.1.0.Final ãçç¥ã
Galleonã®Feature Packã䜿ã£ãŠããããšãããããŸãã
確èªã
$ curl -XPOST -H 'Content-Type: application/json' http://localhost:8080/book -d '{"isbn": "978-4774183169", "title": "ããŒãã§ã¯ã Java EE", "price": 3520}' {"isbn":"978-4774183169","price":3520,"title":"ããŒãã§ã¯ã Java EE"} $ curl http://localhost:8080/book [{"isbn":"978-4774183169","price":3520,"title":"ããŒãã§ã¯ã Java EE"}] $ curl http://localhost:8080/book/978-4774183169 {"isbn":"978-4774183169","price":3520,"title":"ããŒãã§ã¯ã Java EE"}
OKã§ããã
ãªããä»åã¯persistence.xml
ã«ä»¥äžã®ããã«æå®ããã®ã§ããœãŒã¹ã³ãŒããå€æŽãã床ã«ããŒãã«ãåäœæãããã®ã§æ³šæãâŠã
<property name="jakarta.persistence.schema-generation.database.action" value="drop-and-create"/>
ãããã«
WildFly Maven Pluginã®devãŽãŒã«ãè©ŠããŠã¿ãŸããã
ãªããšãªããç°¡åã«JAX-RSãè©ŠããŠããããJPAãè©ŠããŠãããâŠãããã«æã£ãŠããŠãGalleonã¯ä»åã¯åºããªãã€ããã ã£ãã®ã§ããã
ãã®äœ¿ãæ¹ã ãšGalleonã䜿ã£ãŠæ§æããªããšã©ãã«ããªããªããªã£ãã®ã§ããããªããŸããâŠã
æã£ã以äžã«é·ããªããŸãããâŠã
WildFly Maven PluginãšGalleonã䜿ã£ãããããžã§ãã³ã°ã¯ãå¥ã®æ©äŒã«ãŸãæ·±å ãããããªãšæããŸãã