This is an AMP version of the article, its original content can be found here.
Liquibase with Maven
Liquibase is a migration management tool for relational databases. It versionalizes schema and data changes in a database; similar to the way Git or SVN works for source code. Thanks to their Maven plugin, Liquibase can be used as a part of a build automation scenario.
Let's assume you're using MySQL (PostgreSQL or any other database configuration will be very similar.)
pom.xml (get its latest
version in Maven Central):
To check that it works, run
I would recommend you keep database credentials
and in their respective profiles. For example:
When you run Maven, don't forget to turn
on one of the profiles. For example:
I assume you already have a database with a schema (tables, triggers, views, etc.) and some data. You should "reverse engineer" it and create an initial schema file for Liquibase. In other words, we should inform Liquibase where we are at the moment, so that it starts to apply changes from this point.
Maven plugin doesn't support it, so you will have to run
Liquibase directly. But, it's not that difficult. First,
mvn liquibase:help in order to download all artifacts.
Then, replace placeholders with your actual credentials:
Liquibase will analyze your current database schema
and copy its own schema into
Now, create XML master changeset and save it to
It is an entry point for Liquibase. It starts from this file
and loads all other changesets available in
They should be either
.sql. I recommend that you use
XML mostly because it is easier to maintain and works faster.
Let's create a simple changeset, which adds a new column to an existing table:
We save this file we in
In big projects, you can name your files by the names of the tickets
they are produced in. For example,
045-3432.xml, which means changeset
number 45 coming from ticket #3432.
The important thing is to have this numeric prefix in front of file names, in order to sort them correctly. We want changes to be applied in their correct chronological order.
That's it. We're ready to run
mvn liquibase:update -Pproduction and
our production database will be updated—a new column will be
added to the
Also, see how MySQL Maven Plugin can help you to automate integration testing of database-connected classes.