would like to generate an SQL file from liquibase via Java code replicating this command
java -jar liquibase.jar --changeLogFile="./data/filename" --diffTypes="data" generateChangeLog
I am using this as a replacement for a home grown SQL backup Java class
Something like the following should do what you want I think:
ResourceAccessor resourceAccessor = new FileSystemResourceAccessor();
Database db = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(yourJdbcConnection));
Liquibase liquibase = new Liquibase("data/filename", resourceAccessor, db);
try (Writer writer = new OutputStreamWriter(System.out)) {
liquibase.update((Contexts) null, writer);
}
You can use an util method liquibase.integration.commandline.CommandLineUtils#doGenerateChangeLog
from the liquibase-core.jar
. It uses same parameters as in your example with a command line.
In my project there is example of using this method:https://github.com/0x100/liquibase-backuper-spring-boot-starter . This is a starter for Spring Boot, but I think it's easy to understand how to use the method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.