Skip to content

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:

    plugins {
        id 'org.liquibase.gradle' version '2.2.0'

    The following legacy plugin application is also available to use:

    buildscript {
        repositories {
            dependencies {
                classpath "org.liquibase:liquibase-gradle-plugin:2.2.0"
    apply plugin: 'org.liquibase.gradle'
  3. 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:

    dependencies {
        liquibaseRuntime 'org.liquibase:liquibase-core:4.24.0'
        liquibaseRuntime 'org.liquibase:liquibase-groovy-dsl:2.1.1'
        liquibaseRuntime 'info.picocli:picocli:4.7.5'
        liquibaseRuntime 'org.yaml:snakeyaml:1.33'
        liquibaseRuntime 'mysql:mysql-connector-java:5.1.34'
    apply plugin: "org.liquibase.gradle"

    Replace org.liquibase:liquibase-core:4.24.0 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.

  4. Create a text file in your application directory and name it changelog.sql. Liquibase also supports the XML, YAML, and JSON changelog formats. Another way to use Liquibase and Gradle is with the changelog.groovy file.

  5. Add changesets to your changelog file. Use the following examples depending on the format of the changelog you created:

    databaseChangeLog = {
       changeSet(id: '1', author: 'liquibase') {
           createTable(tableName: 'test_table') {
               column(name:'test_id', type="int") {
                   constraints(primaryKey: true)
               column(name:'test_column', type="int")

    -- liquibase formatted sql
    -- changeset my_name:1
    CREATE TABLE test_table 
      test_id INT, 
      test_column INT, 
      PRIMARY KEY (test_id)

    <?xml version="1.0" encoding="UTF-8"?>
      <changeSet id="1" author="my_name">
        <createTable tableName="test_table">
          <column name="test_id" type="int">
            <constraints primaryKey="true"/>
          <column name="test_column" type="INT"/>

      - changeSet:
        id: 1
        author: my_name
        - createTable:
          tableName: test_table
          - column:
            name: test_id
              type: INT
                primaryKey:  true
                nullable:  false
          - column:
            name: test_column
              type: INT

      "databaseChangeLog": [
          "changeSet": {
            "id": "1",
            "author": "my_name",
            "changes": [
                "createTable": {
                  "tableName": "test_table",
                  "columns": [
                      "column": {
                        "name": "test_id",
                        "type": "INT",
                        "constraints": {
                          "primaryKey": true,
                          "nullable": false
                      "column": {
                        "name": "test_column",
                        "type": "INT"

  6. Set the following Liquibase properties in your build.gradle file:

    liquibase {
        activities {
            main {
              changelogFile "../changelog.sql"
              url "mysql://localhost:3306/testdatabase"
              username "username"
              password "password"


    Replace the values from the example with your values.


    The following error can often be fixed by changing the Java version or deleting IntelliJ caches. (source article)

    main in build cannot be applied to (groovy.lang.Closure)


    To store other Liquibase parameters in a file instead of passing them at runtime, you can either specify 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 Create and Configure a File.

  7. Do your first update by adding the task section to the build.gradle file:

    task('deploy changeLog') {
        doFirst() {
            liquibase {
                activities {
                    main {
    update.dependsOn('deploy changeLog')
  8. Run the gradle build command, and then run the following:

    gradle update

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


    Don't worry if you see "UPDATE SUMMARY" listed twice. The update is occuring once, but the output is getting displayed by both Liquibase and the Gradle extension.

    [2024-01-26 14:37:14] INFO [liquibase.ui] Database is up to date, no changesets to execute
    [2024-01-26 14:37:14] INFO [liquibase.changelog] Reading from public.databasechangelog
    [2024-01-26 14:37:14] INFO [liquibase.util] UPDATE SUMMARY
    [2024-01-26 14:37:14] INFO [liquibase.util] Run:                          0
    [2024-01-26 14:37:14] INFO [liquibase.util] Previously run:              27
    [2024-01-26 14:37:14] INFO [liquibase.util] Filtered out:                 0
    [2024-01-26 14:37:14] INFO [liquibase.util] -------------------------------
    [2024-01-26 14:37:14] INFO [liquibase.util] Total change sets:           27
    Run:                          0
    Previously run:              27
    Filtered out:                 0
    Total change sets:           27

  9. [Optional] Do your first rollback by using the rollback-count command:

    gradle build
    gradle rollbackCount -PliquibaseCommandValue=1


    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.


    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;
  10. Check the changes by inspecting your database or running the status command.

Related links