尝试从 Java 代码 运行 时,liquibase 未创建 DDL 命令

DDL command not created by liquibase when trying to run it from Java code

更新:

我认为问题出在这一行,因为当我调用 changelog.getChangeSets() 时它 returns 是一个空列表,所以这段代码有什么问题?

    static liquibase.changelog.DatabaseChangeLog changelog = new DatabaseChangeLog(
            "src/main/resources/changelog.xml");

原文post:

我的目标是 运行 数据库 DDL 命令与 Java class 中的现有变更日志。 目前我的设置是这样的:

public class LiquibaseWithJavaCode {
    //find the location of the existing changelog.xml file
    static liquibase.changelog.DatabaseChangeLog changelog = new DatabaseChangeLog(
            "src/main/resources/changelog.xml");

    static void executeUpdate1() throws SQLException, LiquibaseException {

        java.sql.Connection connection = getConnection();

        Database database = DatabaseFactory.getInstance()
                .findCorrectDatabaseImplementation(
                        new JdbcConnection(connection));

        Liquibase liquibase = new liquibase.Liquibase(changelog,
                new ClassLoaderResourceAccessor(), database);

        liquibase.update(new Contexts(), new LabelExpression());
    }

    public static Connection getConnection() throws SQLException {

        Connection conn = null;
        Properties connectionProps = new Properties();
        connectionProps.put("user", "liquibase");
        connectionProps.put("password", "password");

        conn = DriverManager
                .getConnection(
                        "jdbc:sqlserver://111.11.1.1;databaseName=liquibase;SelectMethod=cursor",
                        connectionProps);
        return conn;
    }
}

    public static void main(String[] args) throws SQLException,
        LiquibaseException {
            executeUpdate1();
    }

当我运行时,这里是输出:

我创建了 changelog.xml,它包含大量处理大量 DDL 命令的变更集。但似乎 changeLogFile 不包含任何变更集,它是空的。我仔细检查了数据库,确实没有实际创建 table,只创建了两个 table 和 DATABASECHANGELOGLOCK

我做错了什么吗?我该如何纠正它?老实说,我对此并不熟悉,请指教。

new DatabaseChangeLog 只是创建一个新的空变更日志文件。要解析现有文件,请使用:

ChangeLogParser parser = ChangeLogParserFactory.getInstance().getParser("src/main/resources/changelog.xml", resourceAccessor);
DatabaseChangeLog databaseChangeLog = parser.parse("src/main/resources/changelog.xml", new ChangeLogParameters(), resourceAccessor);

对于资源访问器,您可能需要 new ClassLoaderResourceAccessor()