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.
  3. Note: The examples directory provides XML, SQL, JSON, and YAML changelogs with the corresponding properties files.

  4. 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.
  5. Create a text file called pom.xml or use the pom.xml file created for your project. Specify your attributes based on the example:
  6. <?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>
  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 pom.xml or the Liquibase properties file, as follows:
  9. pom.xml: <changeLogFile>your/path/to/changelog.sql</changeLogFile>

    liquibase.properties:

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

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

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

  11. Download and unzip the src.zip file to your Liquibase directory. The src.zip file contains Java scripts to run a Spring application.
  12. 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.

  13. Run the following command to compile and test your Spring Boot application code:
  14. mvn compile package
  15. Run the update-sql goal to inspect the SQL before applying changes to your database:
  16. mvn liquibase:update-sql
  17. Deploy your changes by using the update goal:
  18. 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.