为Liquibase任务添加Gradle任务

时间:2017-07-15 18:08:02

标签: java gradle jhipster liquibase liquibase-hibernate

我正在使用带有Gradle的JHipster 4.5.4作为构建系统,并希望添加Gradle Tasks来执行Liquibase任务,例如验证或重置。我使用了JHipster附带的liquibaseDiffChangelog任务作为模板。此任务定义如下:

task liquibaseDiffChangelog(dependsOn: compileJava, type: JavaExec) {
    group = "liquibase"

    if (OperatingSystem.current().isWindows()) {
        dependsOn pathingLiquibaseJar
        doFirst {
            classpath = files(pathingLiquibaseJar.archivePath)
        }
    } else {
        classpath sourceSets.main.runtimeClasspath
        classpath configurations.liquibase
    }
    main = "liquibase.integration.commandline.Main"

    args "--changeLogFile=src/main/resources/config/liquibase/changelog/" + buildTimestamp() +"_changelog.xml"
    args "--referenceUrl=hibernate:spring:at.project.domain?dialect=org.hibernate.dialect.MySQL5InnoDBDialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy"
    args "--username=testapp_db_user"
    args "--password=secret"
    args "--url=jdbc:mysql://localhost:3306/testapp_db"
    args "--driver=com.mysql.jdbc.Driver"
    args "diffChangeLog"
}

我开始只需使用验证 diffChangeLog 进行exchang,但接收类路径错误。我还添加了一个classpath参数,最后得到了一个像这样的任务定义:

task liquibaseValidate(dependsOn: compileJava, type: JavaExec) {
    group = "liquibase"

    if (OperatingSystem.current().isWindows()) {
        dependsOn pathingLiquibaseJar
        doFirst {
            classpath = files(pathingLiquibaseJar.archivePath)
        }
    } else {
        classpath sourceSets.main.runtimeClasspath
        classpath configurations.liquibase
    }
    main = "liquibase.integration.commandline.Main"

    args "--changeLogFile=./build/resources/main/config/liquibase/master.xml"
    args "--referenceUrl=hibernate:spring:at.project.domain?dialect=org.hibernate.dialect.MySQL5InnoDBDialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy"
    args "--username=testapp_db_user"
    args "--password=secret"
    args "--url=jdbc:mysql://localhost:3306/testapp_db"
    args "--driver=com.mysql.jdbc.Driver"
    args "--classpath=./build/resources/main/config/liquibase/changelog/"
    args "validate"
}

主文件被读取,因为它提到了正确的文件名,所以这部分很好。但它无法识别实际变更集文件的路径,这些文件在 master.xml 文件中引用。 这是错误消息:

19:51:04.292 [main] ERROR liquibase - classpath:config/liquibase/changelog/00000000000000_initial_schema.xml does not exist
liquibase.exception.ChangeLogParseException: liquibase.exception.SetupException: classpath:config/liquibase/changelog/00000000000000_initial_schema.xml does not exist
        at liquibase.parser.core.xml.AbstractChangeLogParser.parse(AbstractChangeLogParser.java:27)
        at liquibase.Liquibase.getDatabaseChangeLog(Liquibase.java:229)
        at liquibase.Liquibase.validate(Liquibase.java:1443)
        at liquibase.integration.commandline.Main.doMigration(Main.java:1102)
        at liquibase.integration.commandline.Main.run(Main.java:188)
        at liquibase.integration.commandline.Main.main(Main.java:103)
Caused by: liquibase.exception.SetupException: classpath:config/liquibase/changelog/00000000000000_initial_schema.xml does not exist
        at liquibase.changelog.DatabaseChangeLog.handleChildNode(DatabaseChangeLog.java:322)
        at liquibase.changelog.DatabaseChangeLog.load(DatabaseChangeLog.java:282)
        at liquibase.parser.core.xml.AbstractChangeLogParser.parse(AbstractChangeLogParser.java:25)
        ... 5 common frames omitted
Caused by: liquibase.exception.ChangeLogParseException: classpath:config/liquibase/changelog/00000000000000_initial_schema.xml does not exist
        at liquibase.parser.core.xml.XMLChangeLogSAXParser.parseToNode(XMLChangeLogSAXParser.java:100)
        at liquibase.parser.core.xml.AbstractChangeLogParser.parse(AbstractChangeLogParser.java:17)
        at liquibase.changelog.DatabaseChangeLog.include(DatabaseChangeLog.java:478)
        at liquibase.changelog.DatabaseChangeLog.handleChildNode(DatabaseChangeLog.java:320)
        ... 7 common frames omitted

如何正确添加类路径?

1 个答案:

答案 0 :(得分:2)

this JHipster issue相关,您需要从master.xml(位于classpath:)的更改日志路径中删除src/main/resources/config/liquibase/master.xml。这将允许您使用liquibaseDiffChangeLog之外的其他liquibase任务。这应该在下一个版本中修复(v4.6.2 +)。