Skip to content

How can I move liquibase block from build.gradle to standalone plugin on java?

I faced with issue that I need use Liquibase migration without running application. (through the command ./gradlew update) I added the following section to one of my projects:

if (!project.hasProperty("url")) {
    project.ext.url = "jdbc:postgresql://localhost:5432/test"
if (!project.hasProperty("username")) {
    project.ext.username = "test"
if (!project.hasProperty("password")) {
    project.ext.password = "test"
if (!project.hasProperty("defaultSchemaName")) {
    project.ext.defaultSchemaName = "test"
if (!project.hasProperty("changelogFile")) {
    project.ext.changelogFile = "some-path/changelog-master.xml"

liquibase {
    activities {
        main {
            driver "org.postgresql.Driver"
            url project.ext.url
            username project.ext.username
            password project.ext.password
            defaultSchemaName project.ext.defaultSchemaName
            changelogFile project.ext.changelogFile

This works, but it takes up a lot of space in build.gradle, and the same task needs to be added to other projects as well.

Is it possible to bring this section into a plugin and organize work through the plugin?



The solution that helped me:

    private void configureExtension(Project project) {
    project.getExtensions().configure(LiquibaseExtension.class, liquibaseExtension -> {
        var activity = liquibaseExtension.getActivities().create("main");
        var arguments = new HashMap<>() {
                put("url", url);
                put("username", username);
                put("password", password);
                put("defaultSchemaName", defaultSchemaName);
                put("classpath", classpath);
                put("changeLogFile", changeLogFile);