Maven 项目中集成数据库文档生成工具

在 Maven 项目中,可以通过集成 数据库文档生成工具(如 screw-maven-pluginmybatis-generatorliquibase)来自动生成数据库文档。以下是使用 screw-maven-plugin(推荐)的完整配置步骤:


1. 添加插件配置到 pom.xml

将以下配置添加到 部分:

<build>
    <plugins>
        
        <plugin>
            <groupId>cn.smallbun.screwgroupId>
            <artifactId>screw-maven-pluginartifactId>
            <version>1.0.5version>
            <dependencies>
                
                <dependency>
                    <groupId>mysqlgroupId>
                    <artifactId>mysql-connector-javaartifactId>
                    <version>8.0.32version>
                dependency>
                
                <dependency>
                    <groupId>com.zaxxergroupId>
                    <artifactId>HikariCPartifactId>
                    <version>3.4.5version>
                dependency>
            dependencies>
            <configuration>
                
                <username>${db.username}username>
                <password>${db.password}password>
                <jdbcUrl>jdbc:mysql://${db.host}:${db.port}/${db.name}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=falsejdbcUrl>
                <driverClassName>com.mysql.cj.jdbc.DriverdriverClassName>

                
                <fileType>HTMLfileType>  
                <fileName>数据库文档fileName>
                <title>项目数据库设计title>
                <description>自动生成的数据库文档description>
                <version>${project.version}version>
                <openOutputDir>trueopenOutputDir>  
                
                
                <ignoreTablePrefix>temp_,test_ignoreTablePrefix>
            configuration>
            <executions>
                <execution>
                    <phase>compilephase>  
                    <goals>
                        <goal>rungoal>
                    goals>
                execution>
            executions>
        plugin>
    plugins>
build>

2. 配置数据库信息

pom.xmlsettings.xml 中定义数据库变量(避免明文密码):

方式一:在 pom.xml 中配置
<properties>
    <db.host>localhostdb.host>
    <db.port>3306db.port>
    <db.name>your_databasedb.name>
    <db.username>rootdb.username>
    <db.password>123456db.password>
properties>
方式二:在 settings.xml 中配置(更安全)
<settings>
    <profiles>
        <profile>
            <id>db-configid>
            <properties>
                <db.password>ENC(加密后的密码)db.password>
            properties>
        profile>
    profiles>
    <activeProfiles>
        <activeProfile>db-configactiveProfile>
    activeProfiles>
settings>

3. 执行生成命令

运行以下 Maven 命令生成文档:

mvn compile  # 插件绑定到compile阶段,会自动触发
# 或单独执行插件
mvn screw:run

生成的文档默认输出到:
target/doc/数据库文档.{html|md|docx}



5. 高级配置选项

参数 说明
fileType 输出格式:HTML(默认)、WORDMD
ignoreTablePrefix 忽略表前缀(如 test_
produceType 模板引擎:freemarker(默认)或 velocity
design 自定义描述信息(支持HTML标签)

6. 注意事项

  1. 数据库兼容性
    • 支持 MySQL/Oracle/PostgreSQL/SQL Server 等主流数据库(需正确配置驱动)。
  2. 密码安全
    • 生产环境建议使用 Maven 密码加密(官方指南)。
  3. 多模块项目
    • 在父 POM 中配置插件,子模块通过 true 继承。

替代方案对比

工具 优点 缺点
screw-maven-plugin 轻量、支持多格式、中文友好 仅生成文档,无数据库变更管理
mybatis-generator 可生成代码+文档 配置复杂,文档功能较弱
liquibase 支持数据库版本管理 文档生成需额外插件

推荐选择 screw-maven-plugin 快速生成简洁的数据库文档!

你可能感兴趣的:(Linux&运维安装,工具,linux,运维)