Using Liquibase with Spring Boot: A Practical Guide to Database Change Management
This article provides a step‑by‑step tutorial on integrating Liquibase into a Spring Boot project, covering the creation of a Maven plugin to generate XML/YAML changelogs, configuring the application, writing changeSets for tables, columns, indexes and data, generating documentation, and troubleshooting classpath scanning issues.
The author introduces Liquibase as a database change‑management tool and presents a complete Spring Boot example that demonstrates how to create, modify, and initialize database objects using Liquibase changelogs.
Two Maven projects are created: liquibase-changelog-generate , a plugin that can generate changelog files in XML or YAML, and springboot-liquibase , the main application that applies those changelogs.
Liquibase template generator plugin – The plugin is a Maven project with the following dependencies and plugins (excerpt): <dependencies> <dependency> <groupId>org.apache.maven</groupId> <artifactId>maven-plugin-api</artifactId> <version>3.8.6</version> </dependency> ... </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-plugin-plugin</artifactId> <version>3.6.4</version> </plugin> ... </plugins> </build>
An interface LiquibaseChangeLog is defined to validate change‑set IDs and generate a timestamped filename: public interface LiquibaseChangeLog { default String getChangeLogFileName(String sourceFolderPath) { ... } default boolean isExistedChangeId(String changeId, String sourceFolderPath) { ... } }
A utility class GitUtil obtains the Git user name for the author attribute of changeSets: public class GitUtil { public static String getGitUserName() { ... } }
Two Mojo classes generate changelogs:
@Mojo(name = "generateModelChangeXml", defaultPhase = LifecyclePhase.PACKAGE) public class LiquibaseChangeLogXml extends AbstractMojo implements LiquibaseChangeLog { ... }
@Mojo(name = "generateModelChangeYaml", defaultPhase = LifecyclePhase.PACKAGE) public class LiquibaseChangeLogYaml extends AbstractMojo implements LiquibaseChangeLog { ... }
The main Spring Boot project includes the usual dependencies (Spring Boot starter, MySQL driver, Druid, MyBatis‑Plus, Liquibase core) and the following Maven plugins (excerpt): <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.liquibase</groupId> <artifactId>liquibase-maven-plugin</artifactId> <version>4.16.1</version> <configuration> <propertyFile>src/main/resources/application.yml</propertyFile> <propertyFileWillOverride>true</propertyFileWillOverride> </configuration> </plugin> <plugin> <groupId>com.msdn.hresh</groupId> <artifactId>liquibase-changelog-generate</artifactId> <version>1.0-SNAPSHOT</version> <configuration> <sourceFolderPath>src/main/resources/liquibase/changelogs/</sourceFolderPath> </configuration> </plugin>
The application.yml configures the datasource, Liquibase settings, and MyBatis. The master changelog includes common properties and an <includeAll path="liquibase/changelogs/"/> directive.
Using the generated plugin, the author demonstrates common operations:
Creating a table by running the generated create_table_admin changelog.
Adding a column with add_column_address_in_admin .
Creating an index with create_index_in_admin and dropping it when needed.
Initializing data via an sql changeSet.
After each change, the project is restarted and the resulting database schema and databasechangelog table are verified.
Liquibase also provides a visual documentation generator. Executing the Maven goal liquibase:dbDoc creates HTML documentation under the target directory, which can be opened via index.html .
Troubleshooting : The author observed that Liquibase was reading many unnecessary files from the liquibase‑core JAR because the includeAll path matched both the project’s liquibase/changelog directory and the same path inside the dependency JAR. Renaming the project directory to liquibase/changelogs resolved the issue.
The article concludes with a call for readers to follow the author’s public account and join a knowledge community for further advanced Spring and database topics.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.