Using Liquibase with Spring Boot and Maven Project

Tip: To use the liquibase-maven-plugin and Spring Boot, see Using Liquibase Maven Plugin with Spring Boot.

Use Liquibase with Spring Boot to create and configure standalone Spring applications and automate your database updates. Spring Boot with build systems like Maven allows you to create Java applications started by running java -jar or war deployments.

The Liquibase Spring Boot integration ensures the application database is updated along with the application code by using Spring Boot auto-configuration and features.

To use Liquibase and Spring Boot:

  1. Install Maven and add it to your path.
  2. Ensure you have Java Development Kit (JDK 8, 11, or 16).
  3. Create a project by using the Spring Boot application:
  • If you have an existing Spring Boot project, add the liquibase-core dependency to your project pom.xml.
  • To manually create a new Spring Boot project, follow the Spring Boot Getting Started documentation.
  • To create a basic Spring Boot project, use a web-based service called Spring Initializr.
  • Enter the following information in Spring Initializr:

    • Project: Maven
    • Language: Java
    • Spring Boot: the version you need
    • Project Metadata:
      • Group: com.example.liquibase
      • Artifact: springbootProject
      • Name: springbootProject
      • Description: Liquibase Project for Spring Boot
      • Package name: com.example.liquibase.springbootProject
      • Packaging: Jar
      • Java: 8, 11, or 16
    • Dependencies: Spring Data JPA and Liquibase Migration. The service lets you add your database driver dependency and any developer tool.
  1. Select GENERATE to download your project template as a .zip file.
  2. Extract the files and open pom.xml in your IDE or text editor. By default, the Liquibase dependency will find the latest liquibase-core version. You can edit the Liquibase dependency to include the exact version of Liquibase you want to use.
  3. Follow the instructions depending on your project.

Running Liquibase with Spring Boot and the Maven project

  1. Open the existing Spring Boot file. To find the file, navigate to src/main/resources/

  2. Add the following properties to run Liquibase migrations. Update the values depending on your database requirements:

  3. spring.datasource.url=jdbc:postgresql://localhost:5432/yourdatabase

    Note: To find the URL format for your database, see Liquibase Database Tutorials.

  4. Create a new text file called pom.xml or use the pom.xml file created for your project by the Spring Initializr.
  5. Specify attributes in your pom.xml based on the example:
  6. <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="" 
             <relativePath/> <!-- lookup parent from repository -->
    <description>Liquibase Project for Spring Boot</description>
    <!-- -->
  7. Create a text file called changelog.sql or use the changelog file from the examples directory. Liquibase also supports the .xml, .yaml, or .json changelog formats.
  8. Specify the changelog file in the file as follows:
  9. spring.liquibase.change-log=classpath:db/changelog/changelog.sql

    Alternatively, specify the changelog file in pom.xml or the Liquibase properties file:

    pom.xml: <changelog-file>your/path/to/changelog.sql</changelog-file>

    • Windows example: changelog-file: ..\path\to\changelog.sql
    • Linux example: changelog-file: ../path/to/changelog.sql
  10. Add changesets to your changelog file. Use the following examples depending on the format of the changelog you created:
  11. XML example:

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

    SQL example:

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

    YAML example:

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

    JSON example

      "databaseChangeLog": [
        "changeSet": {
          "id": "1",
          "author": "Liquibase",
          "changes": [
              "createTable": {
                "columns": [
                    "name": "test_column",
                    "type": "INT",
                    "primaryKey": true,
                    "nullable": false
                "tableName": "test_table"
  12. Run your first migration with the following command:
  13. mvn compile package

    The command creates the Spring Boot application JAR file in the target directory. If the metadata specified in Spring Initializr was used in the Spring Boot project setup, you can directly execute the jar file by running:

    java -jar springbootProject-0.0.1-SNAPSHOT.jar

To roll back any changes, create the liquibase.rollback-file file in Spring Boot and generate a rollback script for changes associated with changesets:

-- rollback drop table test_table;

Note: You can also use tags for the rollback.

Related links