How to use Liquibase with Spring Boot and Maven
Posted By : Abhishek Purohit | 28-Jun-2021
Liquibase is a migration tool that provides services like creating database schema, testing the database to ensure reliability and manage revisions of your database schema scripts. It supports multiple databases and file formats for defining the DB structure.
- JDK 8+
- Maven (Any latest version ).
Below are the steps to create a Liquibase project with
- Create a new project folder with a good name.
- In your
Projectfolder, create a new
for liquibase configuration.
liquibase.propertiesfile should be edited in order to add the following properties:
changeLogFile property points to the changelog file that we will create later in this tutorial. Since the changelog is in the home directory, there is no need to specify a path to it. If the changelog is located somewhere else, then add a relative path.
changeLogFile: ..\\<path to changelog>\\changelogs\\dbchangelog.xml
../<path to changelog>/changelogs/dbchangelog.xml
url property is your database url. In this example, we use an in-memory h2 database. If there is a user and password associated with the database, then add the
password properties to the properties file as well.
If you already have a Liquibase Pro key and want to apply it to your project, add the following property to your liquibase.properties file:
liquibaseProLicenseKey: <paste license key>
- Create a new text file in your
"project name"folder and name it
dbchangelog.xml. The changelog files contain a sequence of changesets, each of which makes small changes to the structure of your database.
Note: To generate a changelog, instead of creating an empty changelog file, you can also use an existing database. In this tutorial, you will manually add a single change. To add this change, open the
dbchangelog.xml file and update the changelogfile with the following code snippet:
- Add a changeset to the changelog. The changesets are uniquely identified by
idattributes. Liquibase attempts to execute each changeset in a transaction that is committed at the end. In the
dbchangelog.xmlfile, add a new “department” create table changeset as follows:
Note: The preceding changeset is XML format. The corresponding SQL statement looks like the following:
- In your
"project name"folder create a text document named
- Open the
pom.xmlfile and update it with the following code snippet:
- Download and unzip the
"project name"directory. The file contains java scripts in order to run a Spring application.
- You need to open the command prompt and go to the
- To compile and test your Spring Boot application code, run the following command:
- Run the following command to deploy your changes:
- You can check in the console for the executed sql script: