You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在嵌入式WildFly+Arquillian环境中配置PostgreSQL JDBC驱动

解决嵌入式WildFly + Arquillian集成测试中PostgreSQL驱动加载问题

遇到这个问题很正常,毕竟嵌入式WildFly和独立版的驱动加载逻辑不一样——独立版可以手动复制驱动到模块目录,但嵌入式容器是由Arquillian动态启动的,得通过构建配置和Arquillian的部署逻辑来注入驱动。下面给你两个靠谱的解决方案:

方案一:把驱动打包到测试WAR中(最简单直接)

这种方式不需要手动定义模块,直接把PostgreSQL驱动作为测试WAR的依赖打包进去,WildFly会自动识别并加载:

  1. 在pom.xml中添加驱动依赖
    把PostgreSQL驱动的依赖加入到test scope下,确保只在测试阶段生效:

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.6.0</version> <!-- 替换成你用的版本 -->
        <scope>test</scope>
    </dependency>
    
  2. 在Arquillian部署中引入驱动
    在你的@Deployment方法里,用ShrinkWrap把驱动JAR添加到WAR的WEB-INF/lib目录下:

    @Deployment
    public static WebArchive createDeployment() {
        return ShrinkWrap.create(WebArchive.class, "test.war")
                // 添加你的业务类、配置文件等
                .addPackages(true, "com.yourpackage")
                .addAsResource("META-INF/persistence.xml")
                // 引入PostgreSQL驱动
                .addAsLibrary(Maven.resolver()
                        .resolve("org.postgresql:postgresql:42.6.0")
                        .withoutTransitivity()
                        .asSingleFile());
    }
    

部署后,WildFly会自动扫描WEB-INF/lib下的驱动,你就可以正常配置PostgreSQL数据源了。

方案二:为嵌入式WildFly注册驱动模块(贴近生产环境配置)

如果你想和生产环境保持一致,用模块方式管理驱动,可以按以下步骤操作:

  1. 配置pom.xml依赖
    除了PostgreSQL驱动,确保Arquillian WildFly嵌入式容器的依赖正确:

    <dependency>
        <groupId>org.wildfly.arquillian</groupId>
        <artifactId>wildfly-arquillian-container-embedded</artifactId>
        <version>3.0.1.Final</version> <!-- 匹配你的WildFly版本 -->
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.6.0</version>
        <scope>test</scope>
    </dependency>
    
  2. 创建模块定义文件
    在src/test/resources/modules/org/postgresql/jdbc/main/目录下创建module.xml,定义PostgreSQL驱动模块:

    <?xml version="1.0" encoding="UTF-8"?>
    <module xmlns="urn:jboss:module:1.3" name="org.postgresql.jdbc">
        <resources>
            <resource-root path="postgresql-42.6.0.jar"/> <!-- 对应你的驱动版本 -->
        </resources>
        <dependencies>
            <module name="javax.api"/>
            <module name="javax.transaction.api"/>
        </dependencies>
    </module>
    
  3. 配置Arquillian指向模块路径
    在src/test/resources/arquillian.xml中,告诉嵌入式WildFly去哪里找我们定义的模块:

    <arquillian xmlns="http://jboss.org/schema/arquillian"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://jboss.org/schema/arquillian
                http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
        <container qualifier="wildfly-embedded" default="true">
            <configuration>
                <property name="modulePath">src/test/resources/modules</property>
                <!-- 可选:指定测试用的standalone配置文件 -->
                <property name="configurationFile">src/test/resources/standalone-test.xml</property>
            </configuration>
        </container>
    </arquillian>
    
  4. 配置数据源
    在你的测试用standalone配置文件(比如standalone-test.xml)中,添加PostgreSQL数据源和驱动配置:

    <subsystem xmlns="urn:jboss:domain:datasources:6.0">
        <datasources>
            <datasource jndi-name="java:jboss/datasources/TestPostgresDS" 
                        pool-name="TestPostgresDS" enabled="true">
                <connection-url>jdbc:postgresql://localhost:5432/test_db</connection-url>
                <driver>postgresql</driver>
                <security>
                    <user-name>test_user</user-name>
                    <password>test_pass</password>
                </security>
            </datasource>
            <drivers>
                <driver name="postgresql" module="org.postgresql.jdbc">
                    <driver-class>org.postgresql.Driver</driver-class>
                </driver>
            </drivers>
        </datasources>
    </subsystem>
    

额外注意事项

  • 确保测试环境的PostgreSQL服务已经启动,测试数据库已经创建,连接参数(地址、端口、用户名、密码)正确。
  • 驱动版本要和WildFly版本兼容,比如WildFly 26+建议用PostgreSQL驱动42.2+版本。

内容的提问来源于stack exchange,提问作者badera

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:12:24