Getting Started with Liquibase and Gradle

Using the Liquibase Gradle plugin helps to manage database scripts, build and automate your software processes. When Gradle applies the plugin to the target, it creates a Gradle task for each command supported by Liquibase. To see the list of those tasks, run the gradle tasks command.

To use Liquibase and Gradle:

  1. Create a text file called build.gradle in your project folder or use the existing build.gradle file.
  2. Add the following section to your build.gradle file to include the liquibase plugin into Gradle builds:
  3. plugins {
      id 'org.liquibase.gradle' version '2.0.4'

    The following legacy plugin application is also available to use:

    buildscript {
        repositories {
        dependencies {
            classpath "org.liquibase:liquibase-gradle-plugin:2.0.4"
    apply plugin: 'org.liquibase.gradle'
  1. Add the dependencies section to include files on which Liquibase will depend to run commands. The plugin needs to find Liquibase when it runs a task, and Liquibase needs to find database drivers, changelog parsers, and other files on the classpath. When adding liquibaseRuntime dependencies to the dependencies section in the build.gradle file, include the Liquibase value along with your database driver:
  2. dependencies {
      liquibaseRuntime 'org.liquibase:liquibase-core:4.2.2'
      liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:2.1.1'
      liquibaseRuntime 'mysql:mysql-connector-java:5.1.34'
    apply plugin: "org.liquibase.gradle"

    Replace org.liquibase:liquibase-core:4.2.2 and mysql:mysql-connector-java:5.1.34 with your values. If you use Groovy scripts for database changes, the example code includes the Liquibase Groovy DSL dependency, which parses changelogs written in a Groovy DSL. You do not need to add org.liquibase:liquibase-groovy-dsl:2.1.1 if you do not use the Groovy changelog format. For more information, see Step 4.

  1. Create a text file in your application directory and name it changelog.sql. Liquibase also supports the .xml, .yaml, or .json changelog formats. Another way to use Liquibase and Gradle is with the changelog.groovy file.
  2. Add changesets to your changelog file. Use the following examples depending on the format of the changelog you created:
  1. Set the following Liquibase properties in your build.gradle file:
  2. liquibase {
      activities {
        main {
              changelog-file "../changelog.sql"
              url "mysql://localhost:3306/testdatabase"
              username "username"
              password "password"

    Note: Replace the values from the example with your values.

    Tip: To store other Liquibase properties in a file instead of passing them at runtime, you can continue specifying the properties in the build.gradle file or create a new text file called and set them there. If you create a Liquibase properties file, specify propsFile "../<>" in the main section of the build.gradle file, where <> represents the name of the Liquibase properties file. For more information, see Specifying Properties in a Connection Profile.

  1. Do your first update by adding the task section to the build.gradle file:
  2. task('deploy changeLog') {
        liquibase {
          activities {
            main {
  1. Run the gradle build command, and then run the following:
  2. gradle update

    After your first update, you will see a new table along with the DATABASECHANGELOG table and DATABASECHANGELOGLOCK table added to the database.

  1. [Optional] Do your first rollback by using the rollback-count command:
  2. gradle build
    gradle rollback-count -PliquibaseCommandValue=1

    Note: You can also specify the command value in the build.gradle file or use other rollback commands: rollback, rollback-to-date, rollback-one-changeset, rollback-one-update.

    Tip: Automatic rollback is not supported for formatted SQL changesets. You need to add custom rollback statements to formatted SQL changesets if you want to use rollback commands:

    -- changeset liquibase:1
    create table test_table ( id int primary key, name varchar(255) );
    -- rollback drop table test_table;
  1. Check the changes by inspecting your database or running the status command.

Related links