Liquibase Extension in a Java Console App

I’m using Liquibase Core API to automate database deployments, I’m working on Postgres and I have an extension for it for custom functionality, I’m unable to find a way to use the extension in my Java code or I’m unable to figure out how to make it work.

public static void main(String[] args) throws Exception {
        Class.forName("org.postgresql.Driver");
        Connection connection = DriverManager.getConnection("ConnectionString", "UserName", "");
        Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));

        Map<String, Object> scopedSettings = new LinkedHashMap<>();
        scopedSettings.put(Scope.Attr.database.name(), database);

        // Register PrimaryKeyChangelogHistoryService
       Scope.getCurrentScope().getSingleton(ChangeLogHistoryServiceFactory.class).register(new PrimaryKeyChangelogHistoryService());

        String scopeId = Scope.enter(scopedSettings);
        Scope.child(scopedSettings, () -> {
            CommandScope commandScope = new CommandScope(UpdateCommandStep.COMMAND_NAME);
            commandScope.addArgumentValue(DbUrlConnectionArgumentsCommandStep.DATABASE_ARG, database);
            commandScope.addArgumentValue(UpdateCommandStep.CHANGELOG_FILE_ARG, "changelog/changelog-root.xml");
            commandScope.execute();
        });
        Scope.exit(scopeId);
    }
  1. This code works partially, but doesn’t load my Extension (Jar), should the Jar be added under resources folder?
  2. Can I add a reference to my Extension project instead of the Jar and make it load? (I’d prefer this more if its possible)

You extension JAR needs to be in your classpath in order to be loaded.

1 Like

I got it working, I copied my extension code into Java Project and added service details in META-INF/services folder and it worked fine.