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

如何通过Maven自动生成随数据库变更更新的Mermaid ERD图?

实现Liquibase+Maven项目中Mermaid ERD的自动更新

我需要在项目文档中加入Mermaid ERD图,要求数据库Schema变更时该图能自动更新。项目用Liquibase定义Schema,Maven做构建工具;构建/测试阶段会通过Testcontainers启动Docker化的PostgreSQL,并应用所有Liquibase变更。目前了解Mermerd、SchemaCrawler等工具,但找不到能直接集成它们的Maven插件。现有启动数据库的Groovy Maven插件配置如下:

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>groovy-maven-plugin</artifactId>
    <version>${maven-groovy-plugin.version}</version>

    <dependencies>
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>postgresql</artifactId>
            <version>${testcontainers.version}</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <goals>
                <goal>execute</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
                <source>db = new org.testcontainers.containers.PostgreSQLContainer(
                        "postgres:15.1")
                        .withUsername("${db.username}")
                        .withDatabaseName("postgres")
                        .withPassword("${db.password}");

                db.start();

                project.properties.setProperty('db.url', db.getJdbcUrl());</source>
            </configuration>
        </execution>
    </executions>
</plugin>

可行方案:扩展Groovy Maven插件实现自动生成

既然已经用Groovy插件启动了Testcontainers数据库,直接在同一个Groovy脚本里添加调用ERD生成工具的逻辑即可,无需额外Maven插件。以下是两种工具的实现方式:

方式一:用SchemaCrawler生成Mermaid ERD

SchemaCrawler支持直接从JDBC连接生成Mermaid格式的ER图,步骤如下:

  1. 给Groovy Maven插件添加SchemaCrawler及PostgreSQL驱动依赖
  2. 扩展原有Groovy脚本,在数据库启动并应用Liquibase变更后,调用SchemaCrawler生成Mermaid文件
配置示例:

修改原Groovy插件配置,添加依赖并扩展脚本:

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>groovy-maven-plugin</artifactId>
    <version>${maven-groovy-plugin.version}</version>
    <dependencies>
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>postgresql</artifactId>
            <version>${testcontainers.version}</version>
        </dependency>
        <!-- 添加SchemaCrawler依赖 -->
        <dependency>
            <groupId>us.fatehi</groupId>
            <artifactId>schemacrawler-mermaid</artifactId>
            <version>${schemacrawler.version}</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${postgresql.version}</version>
        </dependency>
        <!-- 添加Liquibase依赖用于手动执行变更 -->
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>${liquibase.version}</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <goals>
                <goal>execute</goal>
            </goals>
            <phase>generate-sources</phase>
            <configuration>
                <source>
                    // 原有启动数据库的代码
                    db = new org.testcontainers.containers.PostgreSQLContainer(
                            "postgres:15.1")
                            .withUsername("${db.username}")
                            .withDatabaseName("postgres")
                            .withPassword("${db.password}");
                    db.start();
                    project.properties.setProperty('db.url', db.getJdbcUrl());

                    // 手动应用Liquibase变更(若原有构建未自动执行)
                    def liquibase = new liquibase.Liquibase(
                            "db/changelog/master.xml",
                            new liquibase.resource.ClassLoaderResourceAccessor(),
                            new liquibase.database.jvm.JdbcConnection(db.createConnection(""))
                    );
                    liquibase.update(new liquibase.contexts.Contexts(), new liquibase.LabelExpression());

                    // 调用SchemaCrawler生成Mermaid ERD到文档目录
                    def command = [
                        "schemacrawler",
                        "--server=postgresql",
                        "--url=" + db.getJdbcUrl(),
                        "--user=" + db.getUsername(),
                        "--password=" + db.getPassword(),
                        "--output-format=mermaid",
                        "--output-file=docs/erd.mmd"
                    ];
                    def process = command.execute();
                    process.waitFor();
                    if (process.exitValue() != 0) {
                        throw new RuntimeException("SchemaCrawler生成ERD失败: " + process.err.text);
                    }

                    // 可选:生成完成后停止数据库(若后续测试无需保留)
                    // db.stop();
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

方式二:用Mermerd生成Mermaid ERD

Mermerd是专门生成Mermaid ERD的工具,可通过Java API直接调用:

  1. 添加Mermerd依赖到Groovy插件
  2. 在Groovy脚本中连接数据库,读取Schema并生成Mermaid文件
核心脚本片段:
// 连接数据库
def connection = db.createConnection("");
// 调用Mermerd生成ERD内容
def mermerd = new io.github.mermerd.mermerd.Mermerd();
def mermaidContent = mermerd.generate(connection, "postgres");
// 将内容写入项目文档目录
new File("docs/erd.mmd").write(mermaidContent);

关键注意事项

  • Liquibase变更优先级:必须确保在生成ERD前已执行所有Liquibase变更,否则生成的ERD会缺失最新Schema结构
  • 执行阶段选择:建议将生成逻辑放在generate-sources或process-resources阶段,确保文档构建前已生成最新ERD
  • 文档嵌入:在项目Markdown文档中直接引用生成的erd.mmd文件内容,或用Mermaid渲染器直接加载该文件
  • 资源清理:若不需要保留测试数据库,可在生成ERD后调用db.stop()释放Docker资源

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 13:18:23