如何通过Maven执行Liquibase的execute-sql命令?
execute-sql命令? Liquibase提供了execute-sql命令,该命令的作用是:
使用execute-sql命令可直接运行SQL查询,无需修改和应用包含变更集的变更日志文件
但查看Liquibase Maven可用目标列表,其中并没有对应的execute-sql目标,可用目标包括:
changelogSync, changelogSyncSQL, changelogSyncToTag, changelogSyncToTagSQL, checks.run, checks.show, clearCheckSums, dbDoc, deactivateChangeLog, diff, dropAll, flow, flow.validate, futureRollbackSQL, generateChangeLog, help, history, listLocks, registerChangeLog, releaseLocks, rollback, rollbackOneChangeSet, rollbackOneChangeSetSQL, rollbackOneUpdate, rollbackOneUpdateSQL, rollbackSQL, status, syncHub, tag, tagExists, unexpectedChangeSets, update, updateSQL, updateTestingRollback, validate
请问是否有办法通过Maven执行execute-sql命令?
有两种可行方式实现通过Maven执行execute-sql命令:
方式一:用exec-maven-plugin调用Liquibase CLI
借助exec-maven-plugin直接调用Liquibase命令行工具执行execute-sql,Maven配置示例:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>liquibase-execute-sql</id> <phase>process-resources</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>liquibase</executable> <arguments> <argument>execute-sql</argument> <argument>--url=jdbc:your-db-url</argument> <argument>--username=your-username</argument> <argument>--password=your-password</argument> <!-- 直接指定SQL语句 --> <argument>--sql="SELECT * FROM your_table;"</argument> <!-- 或指定SQL文件路径 --> <!-- <argument>--sql-file=src/main/resources/your-script.sql</argument> --> </arguments> </configuration> </execution> </executions> </plugin>
执行命令:mvn exec:exec@liquibase-execute-sql
方式二:用Liquibase Maven插件的flow目标
Liquibase的flow目标支持自定义工作流,先创建Flow配置文件(如liquibase-flow.yml):
steps: - type: execute-sql # 直接写SQL语句 sql: "SELECT * FROM your_table;" # 或指定SQL文件 # sql-file: src/main/resources/your-script.sql
再配置Maven插件:
<plugin> <groupId>org.liquibase</groupId> <artifactId>liquibase-maven-plugin</artifactId> <version>4.24.0</version> <configuration> <url>jdbc:your-db-url</url> <username>your-username</username> <password>your-password</password> <flowFile>src/main/resources/liquibase-flow.yml</flowFile> </configuration> <executions> <execution> <id>liquibase-flow-execute-sql</id> <phase>process-resources</phase> <goals> <goal>flow</goal> </goals> </execution> </executions> </plugin>
执行命令:mvn liquibase:flow
内容的提问来源于stack exchange,提问作者lance-java

