Liquibase, an open-source database migration tool, can seamlessly integrate with Spring Boot applications, managing database schemas, tables, and data changes effortlessly. Configuring Liquibase in your Spring Boot project ensures your database remains in sync with your application's evolving schema needs. Let's dive into an in-depth guide on how to set up Liquibase in your Spring Boot application.

Before we proceed, ensure you have the following prerequisites: Spring Boot, a relational database like MySQL or PostgreSQL, and the Liquibase dependency added to your project. We'll use Maven for our examples, but Gradle users can follow similar steps.

Setting Up Liquibase in Spring Boot
The first step is to add the Liquibase dependency to your build file. Open your pom.xml file and add the following dependency under the dependencies section:

<dependency>
<groupId>org.liquibase>
<artifactId>liquibase-core</artifactId>
<version>2.0.5</version>
</dependency>
Configuration Properties
Next, configure Liquibase properties in your Spring Boot application's application.properties or application.yml file. Here's an example using properties:

spring.liquibase.change-log=classpath:db/changelog/master.xml
spring.liquibase.enabled=true
spring.liquibase.default-schema=public
Note: Replace public with the appropriate schema name if needed.
In the above configuration, we've specified the location of the Liquibase master file (master.xml) and enabled Liquibase in the Spring Boot application.
Database Changelog Files

Create a directory named db/changelog in your project's root. Inside this directory, create another folder named v1.0 (or any version number you prefer). Within the v1.0 folder, create an XML file (e.g., 20220101-create-tables.xml) containing your initial schema changes:
xml version="1.0" encoding="UTF-8"?
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:lg="http://www.liquibase.org/xml/ns/pro/v1.9/pro"
xmlns:pro="http://www.liquibase.org/xml/ns/pro/v1.9/pro"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/pro/v1.9/pro http://www.liquibase.org/xml/ns/pro/v1.9/pro/proext-3.8.xsd">
<changeSet id="1" author="your_name">
<createTable tableName="example_table">
<column name="id" type="int" autoIncrement="true">
<constraints primaryKey="true"/>
<column name="name" type="varchar(50)"/>
</databaseChangeLog>
This example creates a table named example_table with two columns - id and name.
Running Liquibase in Your Spring Boot Application

Now that you've configured Liquibase, you can run your Spring Boot application, and Liquibase will automatically update your database according to the changes specified in the XML file. To verify this, you can use a database querying tool like DBeaver or your favorite IDE's database plugin to check if the example_table has been created.
Monitoring Liquibase Updates









While running your application, you can monitor Liquibase's progress in the logs. Look for messages indicating that Liquibase has successfully executed changesets, e.g., [Liquibase [...]] Updated 1 change sets.
To see the full Liquibase log, set the spring.liquibase.logging.level property to INFO in your application's configuration file:
spring.liquibase.loggging.level=INFO
That's it! You've successfully integrated Liquibase into your Spring Boot project, ensuring your database stays up-to-date with your application's evolving schema needs.
As your project grows, you can add more change sets to the db/changelog directory, addressing new schema requirements. Liquibase will maintain a version history of your database schema, allowing you to monitor and control schema changes throughout your application's lifecycle.
Happy coding, and may your database schemas remain ever in sync with your application!