Using Liquibase Maven Plugin with Spring Boot
The Liquibase Maven Plugin using Spring Boot has two main features:
- It collects all the
.jar
files in the classpath and builds a singleuber-jar
, which helps to execute your service in more convenient way. - It searches for the
public static void main()
method to flag any classes with such method signature as a runnable class.
As an option, you can run Liquibase in a Spring environment by declaring a liquibase.spring.SpringLiquibase
bean:
<bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">
<property name="dataSource" ref="myDataSource" />
<property name="changeLog" value="classpath:db-changelog.xml" />
<!--
contexts specifies the runtime contexts to use.
-->
<property name="contexts" value="test, production" />
</bean>
Prerequisites
- Install Liquibase
Tip: If you are new to Liquibase, see Introduction to Liquibase.
Running Maven Plugin with Spring Boot
- Create your Liquibase project directory to store all Liquibase files.
- Create a Liquibase properties file or use the existing
liquibase.properties
file included with the Liquibase installation package. - Add attributes that you want to store in the Liquibase properties file instead of run at the command prompt. Specify the database connection information and other attributes by following Create and Configure a liquibase.properties File. See also: Liquibase Database Tutorials and Adding and Updating Liquibase Drivers.
- Create a text file called
pom.xml
or use thepom.xml
file created for your project. Specify your attributes based on the example: - Create a text file called
changelog.sql
or use the changelog file from theexamples
directory. Liquibase also supports the.xml
,.yaml
, or.json
changelog formats. - Specify the changelog file in
pom.xml
or the Liquibase properties file, as follows: - Windows example:
changeLogFile: ..\path\to\changelog.sql
- Linux example:
changeLogFile: ../path/to/changelog.sql
-
Add changesets to your changelog file. Use the following examples, depending on the format of the changelog you created.
- Download and unzip the
src.zip
file to your Liquibase directory. Thesrc.zip
file contains Java scripts to run a Spring application. - Run the following command to compile and test your Spring Boot application code:
- Run the
update-sql
goal to inspect the SQL before applying changes to your database: - Deploy your changes by using the
update
goal:
Note: The examples
directory provides XML, SQL, JSON, and YAML changelogs with the corresponding properties files.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.liquibase</groupId>
<artifactId>springbootProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootProject</name>
<description>Liquibase Project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<liquibase.propertyFile>${project.basedir}/liquibase.properties</liquibase.propertyFile>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<propertyFile>${liquibase.propertyFile}</propertyFile>
</configuration>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.6.Final</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.4.0-b180830.0359</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
pom.xml
: <changeLogFile>your/path/to/changelog.sql</changeLogFile>
liquibase.properties
:
If you use the Liquibase properties file, reference it from pom.xml
, as follows: <propertyFile>liquibase.properties</propertyFile>
.

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd
http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd">
<changeSet id="1" author="Liquibase">
<createTable tableName="test_table">
<column name="test_id" type="int">
<constraints primaryKey="true"/>
</column>
<column name="test_column" type="varchar"/>
</createTable>
</changeSet>
</databaseChangeLog>

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

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

{
"databaseChangeLog": [
{
"changeSet": {
"id": "1",
"author": "Liquibase",
"changes": [
{
"createTable": {
"columns": [
{
"column":
{
"name": "test_column",
"type": "INT",
"constraints":
{
"primaryKey": true,
"nullable": false
}
}
}]
,
"tableName": "test_table"
}
}]
}
}]
}
Note: In the src
directory, you will see the path to application code: src/main/java/com/application.java
and the path to unit tests: src/test/java/com/applicationTests.java
.
mvn compile package
mvn liquibase:update-sql
mvn liquibase:update
After your first update, you will see a new table along with the DATABASECHANGELOG table and DATABASECHANGELOGLOCK table added to the database.