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

liquibase-maven-plugin结合testcontainers对接PostgreSQL的配置咨询

核心实现思路

通过testcontainers-maven-plugin在构建生命周期前置阶段启动PostgreSQL容器,自动将连接参数注入为Maven属性,无需编写Java代码,可同时供liquibase和schemacrawler插件直接使用。


1. 新增Testcontainers Maven插件配置

<plugin>
    <groupId>org.testcontainers</groupId>
    <artifactId>testcontainers-maven-plugin</artifactId>
    <version>0.40.17</version>
    <executions>
        <execution>
            <id>start-postgres</id>
            <phase>process-test-sources</phase>
            <goals>
                <goal>start</goal>
            </goals>
        </execution>
        <execution>
            <id>stop-postgres</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <containers>
            <postgres>
                <image>postgres:14-alpine</image>
                <ports>
                    <port>5432/tcp</port>
                </ports>
                <env>
                    <POSTGRES_USER>test_user</POSTGRES_USER>
                    <POSTGRES_PASSWORD>test_pass</POSTGRES_PASSWORD>
                    <POSTGRES_DB>sample_db</POSTGRES_DB>
                </env>
                <wait>
                    <log>database system is ready to accept connections</log>
                    <times>2</times>
                </wait>
                <properties>
                    <postgres.jdbc.url>jdbc:postgresql://${host}:${ports.5432/tcp}/${env.POSTGRES_DB}</postgres.jdbc.url>
                    <postgres.username>${env.POSTGRES_USER}</postgres.username>
                    <postgres.password>${env.POSTGRES_PASSWORD}</postgres.password>
                </properties>
            </postgres>
        </containers>
    </configuration>
</plugin>

2. 修改原有Liquibase插件配置

<plugin>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-maven-plugin</artifactId>
    <version>3.4.1</version>
    <executions>
        <execution>
            <id>build</id>
            <phase>generate-test-resources</phase>
            <goals>
                <goal>update</goal>
            </goals>
            <configuration>
                <changeLogFile>${basedir}/src/main/resources/liquibase/db.changelog-master.xml</changeLogFile>
                <driver>org.postgresql.Driver</driver>
                <url>${postgres.jdbc.url}</url>
                <username>${postgres.username}</username>
                <password>${postgres.password}</password>
                <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
                <logging>off</logging>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.3.8</version>
        </dependency>
    </dependencies>
</plugin>

3. SchemaCrawler插件适配

直接使用Testcontainers注入的Maven属性即可,无需额外修改逻辑:

<plugin>
    <groupId>schemacrawler</groupId>
    <artifactId>schemacrawler-maven-plugin</artifactId>
    <!-- 保留你原有版本号 -->
    <configuration>
        <url>${postgres.jdbc.url}</url>
        <user>${postgres.username}</user>
        <password>${postgres.password}</password>
        <!-- 其余原有配置保持不变 -->
    </configuration>
</plugin>

备选方案(无需Docker)

如果不想依赖Docker环境,可使用嵌入式PostgreSQL插件实现相同效果:

<plugin>
    <groupId>io.zonky.test</groupId>
    <artifactId>embedded-postgres-maven-plugin</artifactId>
    <version>2.0.1</version>
    <executions>
        <execution>
            <id>start-postgres</id>
            <phase>process-test-sources</phase>
            <goals>
                <goal>start</goal>
            </goals>
            <configuration>
                <port>5432</port>
                <dbName>sample_db</dbName>
                <username>test_user</username>
                <password>test_pass</password>
                <postgresVersion>14.4.0</postgresVersion>
            </configuration>
        </execution>
        <execution>
            <id>stop-postgres</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>

该方案下Liquibase和SchemaCrawler的配置和上述Testcontainers方案完全兼容。


注意事项

  • 使用Testcontainers方案需要本地提前安装并启动Docker服务
  • 原有Liquibase变更日志中如果存在HSQLDB专属语法(比如特有的数据类型、内置函数),需要调整为PostgreSQL兼容写法
  • 可根据业务需要自行调整PostgreSQL镜像/嵌入式版本号

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 00:09:00