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 single uber-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

Running Maven Plugin with Spring Boot

  1. Create your Liquibase project directory to store all Liquibase files.
  2. Create a Liquibase properties file or use the existing liquibase.properties file included with the Liquibase installation package.

Note: The examples directory provides XML, SQL, JSON, and YAML changelogs with the corresponding properties files.

  1. 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 Specifying Properties in a Connection Profile. See also: Supported Databases and Adding and Updating Liquibase Drivers.
  1. Create a text file called pom.xml or use the pom.xml file created for your project. Specify your attributes based on the example:
<?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>
  1. 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.
  1. Specify the changelog file in pom.xml or the Liquibase properties file, as follows:
  2. pom.xml: <changelog-file>your/path/to/changelog.sql</changelog-file>

    liquibase.properties:

    • Windows example: changelog-file: ..\path\to\changelog.sql
    • Linux example: changelog-file: ../path/to/changelog.sql

    If you use the Liquibase properties file, reference it from pom.xml, as follows: <propertyFile>liquibase.properties</propertyFile>.

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

  1. Download and unzip the src.zip file to your Liquibase directory. The src.zip file contains Java scripts to run a Spring application.

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.

  1. Run the following command to compile and test your Spring Boot application code:

  2. mvn compile package
  1. Run the update-sql goal to inspect the SQL before applying changes to your database:

  2. mvn liquibase:update-sql
  1. Deploy your changes by using the update goal:

  2. 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.