Getting Started with Liquibase and Maven

Apache Maven is a software project management and comprehension tool, which is based on the concept of a project object model (POM). The Liquibase Maven integration lets you manage the build process and your database schema scripts from a central file called pom.xml.

To use Liquibase and Maven:

  1. Create a Liquibase Maven project directory to store all Liquibase files.
  2. Tip: Maven includes the Standard Directory Layout. Having a common directory layout with the pom.xml file, text files, the src subfolder, and the target subfolder allows you to use the Maven project easier. For more details about the Maven project structure, see Maven Getting Started Guide or Introduction to the Standard Directory Layout.

  1. In your Liquibase Maven project directory, create a pom.xml file. Alternatively, use your existing pom.xml file.
  2. Add the following section to the pom.xml file:
  3. <plugin>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-maven-plugin</artifactId>
    <version>4.2.0</version>
    </plugin>

    The pom.xml file presents an option to store attribute instead of passing them at runtime as Liquibase Maven goals require database connection information. For more information, see Configuring Liquibase Attributes in your Maven POM File and Using Liquibase and your Maven POM File.

    Tip: Use the Liquibase properties file to include only Liquibase attributes in your pom.xml file. The properties file must reside in the src/main/resources directory or another location in the search path. Additionally, the Liquibase properties must be specified in the configuration section of the pom.xml file, as follows: <propertyFile>liquibase.properties</propertyFile>. For more information, see Specifying Properties in a Connection Profile.

  1. Create a text file called changelog.sql in the src/main/resources directory. Liquibase also supports the .xml, .yaml, or .json changelog formats.
  2. Add changesets to your changelog file. Use the following examples depending on the format of the changelog you created:
  1. Specify your changelog file along with the database URL, username, and password in the pom.xml file to run Maven goals:
  2. <changelog-file>changelog.sql</changelog-file>
    <url>YourJDBCConnection</url>
    <username>dbuser</username>
    <password>dbpassword</password>
  1. Run the update-sql goal to inspect the SQL before applying changes to your database:
  2. mvn liquibase:updateSQL
  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.

Related links