简体   繁体   English

如何将 liquibase 块从 build.gradle 移动到 java 上的独立插件?

[英]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.我遇到了需要在不运行应用程序的情况下使用 Liquibase 迁移的问题。 (through the command ./gradlew update ) I added the following section to one of my projects: (通过命令./gradlew update )我将以下部分添加到我的一个项目中:

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.这可行,但在build.gradle中占用了大量空间,并且同样的任务也需要添加到其他项目中。

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);
            }
        };
        activity.setArguments(arguments);
        liquibaseExtension.getActivities().add(activity);
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM