Wildfly 21:可启动 JAR 找不到 H2 JDBC 驱动程序/数据源类

问题描述

我正在 Wildfly 21.0.1.Final 上部署一个可启动的 JAR。 JAR 是使用 wildfly-jar-maven-plugin 构建的。这是插件配置:

<plugin>
  <groupId>org.wildfly.plugins</groupId>
  <artifactId>wildfly-jar-maven-plugin</artifactId>
  <configuration>
    <feature-pack-location>wildfly@maven(org.jboss.universe:community-universe)#21.0.0.Final
    </feature-pack-location>
    <layers>
       <layer>jaxrs-server</layer>
       <layer>management</layer>
       <layer>observability</layer>
       <layer>microprofile-openapi</layer>
       <layer>h2-driver</layer>
       <layer>h2-datasource</layer>
     </layers>
     <excluded-layers>
       <layer>deployment-scanner</layer>
     </excluded-layers>
   </configuration>
   ...
 </plugin>

运行可启动 JAR 时出现以下异常:

java -jar -Djboss.bind.address=0.0.0.0 -Djboss.bind.address.management=0.0.0.0 -Djboss.http.port=8080 -Djboss.management.http.port=9990 customers/target/customers-bootable.jar 
14:04:58,500 INFO  [org.wildfly.jar] (main) WFLYJAR0007: Installed server and application in /tmp/wildfly-bootable-server15172013110481762654,took 1035ms
14:04:58,607 INFO  [org.wildfly.jar] (main) WFLYJAR0008: Server options: [--read-only-server-config=standalone.xml]
14:04:58,653 INFO  [org.jboss.msc] (main) JBoss MSC version 1.4.12.Final
14:04:58,658 INFO  [org.jboss.threads] (main) JBoss Threads version 2.4.0.Final
14:04:58,730 INFO  [org.jboss.as] (MSC service thread 1-1) WFLYSRV0049: WildFly Full 21.0.0.Final (WildFly Core 13.0.1.Final) starting
14:04:59,134 INFO  [org.wildfly.security] (ServerService Thread Pool -- 14) ELY00001: WildFly elytron version 1.13.1.Final
14:04:59,476 INFO  [org.jboss.as.server] (Controller Boot Thread) WFLYSRV0039: Creating http management service using socket-binding (management-http)
14:04:59,487 INFO  [org.xnio] (MSC service thread 1-2) XNIO version 3.8.2.Final
14:04:59,493 INFO  [org.xnio.nio] (MSC service thread 1-2) XNIO NIO Implementation Version 3.8.2.Final
14:04:59,504 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 31) WFLYCLINF0001: Activating Infinispan subsystem.
14:04:59,514 INFO  [org.wildfly.extension.microprofile.config.smallrye._private] (ServerService Thread Pool -- 37) WFLYCONF0001: Activating WildFly MicroProfile Config Subsystem
14:04:59,516 INFO  [org.jboss.as.jaxrs] (ServerService Thread Pool -- 33) WFLYRS0016: RESTEasy version 3.13.2.Final
14:04:59,517 INFO  [org.jboss.as.connector] (MSC service thread 1-3) WFLYJCA0009: Starting JCA Subsystem (WildFly/IronJacamar 1.4.23.Final)
14:04:59,521 INFO  [org.jboss.as.naming] (ServerService Thread Pool -- 42) WFLYNAM0001: Activating Naming Subsystem
14:04:59,519 INFO  [org.wildfly.extension.microprofile.openapi.smallrye] (ServerService Thread Pool -- 40) WFLYMPOAI0001: Activating Eclipse MicroProfile OpenAPI Subsystem
14:04:59,522 INFO  [org.wildfly.extension.io] (ServerService Thread Pool -- 32) WFLYIO001: Worker 'default' has auto-configured to 24 IO threads with 192 max task threads based on your 12 available processors
14:04:59,520 WARN  [org.jboss.as.txn] (ServerService Thread Pool -- 45) WFLYTX0013: The node-identifier attribute on the /subsystem=transactions is set to the default value. This is a danger for environments running multiple servers. Please make sure the attribute value is unique.               
14:04:59,524 INFO  [org.wildfly.extension.microprofile.health.smallrye] (ServerService Thread Pool -- 38) WFLYHEALTH0001: Activating Eclipse MicroProfile Health Subsystem
14:04:59,521 INFO  [org.wildfly.extension.microprofile.opentracing] (ServerService Thread Pool -- 41) WFLYTRACEXT0001: Activating MicroProfile OpenTracing Subsystem
14:04:59,537 INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 28) WFLYJCA0004: deploying JDBC-compliant driver class org.h2.Driver (version 1.4)
14:04:59,542 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-2) WFLYJCA0018: Started Driver service with driver-name = h2
14:04:59,545 INFO  [org.jboss.as.naming] (MSC service thread 1-7) WFLYNAM0003: Starting Naming Service
14:04:59,546 INFO  [org.wildfly.extension.microprofile.metrics.smallrye] (ServerService Thread Pool -- 39) WFLYMETRICS0001: Activating Eclipse MicroProfile Metrics Subsystem
14:04:59,549 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-8) WFLYUT0003: Undertow 2.2.2.Final starting
14:04:59,555 INFO  [org.jboss.remoting] (MSC service thread 1-3) JBoss Remoting version 5.0.19.Final
14:04:59,611 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-5) WFLYUT0012: Started server default-server.
14:04:59,612 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-2) WFLYUT0018: Host default-host starting
14:04:59,635 WARN  [org.jboss.as.domain.http.api.undertow] (MSC service thread 1-3) WFLYDMHTTP0003: Unable to load console module for slot main,disabling console                                                                                                                                      
14:04:59,641 INFO  [org.jboss.as.patching] (MSC service thread 1-6) WFLYPAT0050: WildFly Full cumulative patch ID is: base,one-off patches include: none
14:04:59,646 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-8) WFLYUT0006: Undertow HTTP listener default listening on [0:0:0:0:0:0:0:0]:8080
14:04:59,654 WARN  [org.jboss.as.domain.management.security] (MSC service thread 1-5) WFLYDM0111: Keystore /tmp/wildfly-bootable-server15172013110481762654/standalone/configuration/application.keystore not found,it will be auto generated on first use with a self signed certificate for host localhost                                                                                                                                               
14:04:59,662 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-5) WFLYSRV0027: Starting deployment of "customers.war" (runtime-name: "ROOT.war")
14:04:59,743 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-5) WFLYJCA0001: Bound data source [java:jboss/datasources/ExampleDS]
14:05:01,202 INFO  [org.jboss.as.jpa] (MSC service thread 1-5) WFLYJPA0002: Read persistence.xml for customers
14:05:01,324 INFO  [org.jboss.weld.deployer] (MSC service thread 1-1) WFLYWELD0003: Processing weld deployment ROOT.war
14:05:01,536 INFO  [org.hibernate.validator.internal.util.Version] (MSC service thread 1-1) HV000001: Hibernate Validator 6.0.21.Final
14:05:01,706 INFO  [io.jaegertracing.internal.JaegerTracer] (MSC service thread 1-1) No shutdown hook registered: Please call close() manually on application shutdown.
14:05:01,721 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-4) MSC000001: Failed to start service jboss.deployment.unit."ROOT.war".INSTALL: org.jboss.msc.service.StartException in service jboss.deployment.unit."ROOT.war".INSTALL: WFLYSRV0153: Failed to process phase INSTALL of deployment "ROOT.war"                                                                                                                                       
...
Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: java.lang.classNotFoundException: org.h2.jdbcx.JdbcDataSource from [Module "deployment.ROOT.war" from Service Module Loader]   

类 org.h2.jdbcx.JdbcDataSource 显然没有找到。但是,在可启动 JAR 中,我在模块目录中看到:

[![模块][1]][1]

因此,如上所示,modules 目录包含一个名为“com.h2database.h2”的模块,这里是关联的 module.xml 文件内容

<?xml version="1.0" encoding="UTF-8"?>
<module name="com.h2database.h2" xmlns="urn:jboss:module:1.5">
  <resources>
    <resource-root path="h2-1.4.197.jar"/>
  </resources>
  <dependencies>
    <module name="javax.api"/>
    <module name="javax.transaction.api"/>
    <module name="javax.servlet.api" optional="true"/>
  </dependencies>
</module>

这里是 h2-1.4.197.jar 存档的内容

[![h2][2]][2]

相应地,Wildfly 服务器有一个名为“com.h2database.h2”的模块,其中包含所需的“org.h2.jdbcx.JdbcDtaSource”类。

那么为什么要提高CNFE呢?最后但并非最不重要的是,这里是数据源定义:

@Singleton
@DataSourceDeFinition(
  name = "java:jboss/datasources/ExampleDS",className = "org.h2.jdbcx.JdbcDataSource",url = "jdbc:h2:mem:customers",user = "sa",password = "sa",databaseName = "customers")
public class DatasourceProducer
{
  @Resource(lookup = "java:jboss/datasources/ExampleDS")
  private DataSource ds;

  @Produces
  public DataSource getDatasource()
  {
    return ds;
  }
}

和persistence.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="customers" transaction-type="JTA">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
    <properties>
      <property name="wildfly.jpa.twophasebootstrap" value="false" />
      <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle12cDialect"/>
      <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
      <property name="hibernate.format_sql" value="true"/>
      <property name="hibernate.show_sql" value="true"/>
    </properties>
  </persistence-unit>
</persistence>       

有人可以在这里解释一下这个问题,因为我被困了几天:-(

非常感谢。

亲切的问候,

西摩 [1]:https://i.stack.imgur.com/fUBOc.png [2]:https://i.stack.imgur.com/mHaGb.png

解决方法

回答我自己的问题。 似乎简单地定义 h2 依赖如下:

  <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
  </dependency>

代替:

  <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>test</scope>
  </dependency>

解决问题。