Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Liquibase migration without Spring, only by help of Java?

I try to use Liquibase without any adjacent framework, only by help of Java and I faced with a problem how to run Liquibase. I found an example from official documentation:

java.sql.Connection connection = openConnection(); //your openConnection logic here
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
Liquibase liquibase = new liquibase.Liquibase("path/to/changelog.xml", new ClassLoaderResourceAccessor(), database);
liquibase.update(new Contexts(), new LabelExpression());

but I prefer to use a liquibase.property file with settings located in resources folder. When I used a Spring and Hibernate it works fine, I simply write a properties in application.properties file and that's all, but how to act in this situation?

like image 869
Jonas_Astley Avatar asked Oct 24 '25 15:10

Jonas_Astley


1 Answers

what about this?

Properties properties = new Properties();
// load properties from classpath
    
java.sql.Connection connection = openConnection(); //your openConnection logic here
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
try (Liquibase liquibase = new liquibase.Liquibase("path/to/changelog.xml", new ClassLoaderResourceAccessor(), database)){
  properties.forEach((key, value) -> liquibase.setChangeLogParameter(Objects.toString(key), value));
  liquibase.update(new Contexts(), new LabelExpression());
}
like image 99
bilak Avatar answered Oct 27 '25 03:10

bilak