如何使用maven从现有数据库生成美人鱼ERD图?

问题描述 投票:0回答:1

我想将 mermaid ERD 图添加到项目文档中,但它需要在架构更改时自动更新。

数据库模式是使用 liquibase 定义的,我使用 Maven 进行构建。 在构建和测试期间,启动带有数据库(postgresql)的 docker 镜像,并且所有 liquibase 更改都应用到那里,因此应该可以从运行的数据库或 liquibase 脚本生成 ERD。

我知道,有像 MermerdSchemaCrawler 这样的工具,但我找不到任何可以集成它们的 Maven 插件。

以下是我在测试期间启动 Docker 化数据库的方法:

            <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>
postgresql maven liquibase testcontainers mermaid
1个回答
0
投票

感谢@bilak的建议,我添加了带有以下配置的schemacrawler maven插件

<plugin>
                <groupId>us.fatehi</groupId>
                <artifactId>schemacrawler-maven-plugin</artifactId>
                <version>16.21.2.2</version>
                <configuration>
                    <url>${db.url}</url>
                    <user>${db.username}</user>
                    <password>${db.password}</password>
                    <command>schema</command>
                    <infolevel>maximum</infolevel>
                    <title>Database Schema</title>
                    <!-- <schemas>PUBLIC.BOOKS</schemas> -->
                    <outputfile>database-schema.svg</outputfile>
                    <outputformat>svg</outputformat>
                    <config>../schemacrawler.config.properties</config>
                    <loadrowcounts>true</loadrowcounts>
                    <loglevel>OFF</loglevel>
                </configuration>
                <dependencies>
                    <!-- If you do not have Graphviz installed, provide a dependency
                      on
                    graphviz-java -->
                    <dependency>
                        <groupId>guru.nidi</groupId>
                        <artifactId>graphviz-java</artifactId>
                        <version>0.18.1</version>
                    </dependency>
                    <dependency>
                        <groupId>org.graalvm.js</groupId>
                        <artifactId>js</artifactId>
                        <version>22.1.0</version>
                    </dependency>
                    <!-- IMPORTANT: Make sure you have a dependency on the JDBC driver.
                    In this example, we are using SQLite as our database server. -->
                    <dependency>
                        <groupId>org.postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>42.6.0</version>
                    </dependency>
                    <!-- IMPORTANT: Make sure you have a dependency on the SchemaCrawler
                    plugin. In this example, we are using SQLite as our database server. -->
                    <dependency>
                        <groupId>us.fatehi</groupId>
                        <artifactId>schemacrawler-postgresql</artifactId>
                        <version>16.21.2</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>generate-schema</id>
                        <goals>
                            <goal>schemacrawler</goal>
                        </goals>
                        <phase>generate-sources</phase>
                    </execution>
                </executions>
            </plugin>

重要的是,schemacrawler 插件位于 groovy-maven-plugin 之后 它生成 SVG 或 PNG 格式的图表 - 仍然不是美人鱼,但总是有一些东西。

© www.soinside.com 2019 - 2024. All rights reserved.