如何在gradle项目中的应用程序启动时运行liquibase changelogs?

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

我有一个用gradle创建的Spring Boot项目。以下是一段来自 build.gradle 显示添加了liquibase。

plugins {
    id 'org.springframework.boot' version '2.2.6.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
    id 'war'
    id 'org.liquibase.gradle' version '2.0.3'
}

dependencies {

    //other dependencies

    runtimeOnly 'org.postgresql:postgresql'
    runtime 'com.h2database:h2'

    liquibaseRuntime 'org.liquibase:liquibase-core:3.8.1'   
    liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:2.1.1'     
    liquibaseRuntime 'org.postgresql:postgresql:42.1.4'     
    liquibaseRuntime 'com.h2database:h2:1.3.160'    
    liquibaseRuntime 'org.liquibase.ext:liquibase-hibernate5:3.6'   
    liquibaseRuntime 'ch.qos.logback:logback-classic:1.2.3'     
    liquibaseRuntime sourceSets.main.output
    liquibaseRuntime 'org.springframework.data:spring-data-jpa'

    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' 
}


diffChangeLog.dependsOn assemble

liquibase {
    activities {
        main {
            changeLogFile 'src/main/resources/db/changelog/changelog-master.xml'
            url 'jdbc:postgresql://localhost:5432/testdb'
            username 'postgres'
            password 'postgres'
            referenceUrl 'hibernate:spring:com.example.project.model?dialect=org.hibernate.dialect.ProgressDialect'
        }
    }
}

通过这个设置,我可以运行 gradle generateChangeLog 命令来生成 changelog-master.xml 文件,然后运行 gradle diffChangeLog 生成变更日志,并将变更追加到同一文件中。接下来,我运行 gradle update 命令来创建数据库中的表,其中包括 databasechangelogdatabasechangeloglock 表。这样一切都能正常工作。

现在的问题是,我不想再运行 gradle update 命令,但我希望在应用程序运行时创建表。我需要这样做,因为当应用程序部署到生产中时,我无法运行这个 gradle update 命令来创建表格。

那么,有没有一种方法可以在应用程序运行时使用 changelog 文件创建表格?

spring-boot gradle database-migration liquibase changelog
© www.soinside.com 2019 - 2024. All rights reserved.