Getting Started with Liquibase and Ant

Apache Ant is a Java library and command-line build tool for Java applications. Liquibase has a set of Ant tasks to automate your database changes at build time.

Note: Liquibase Ant tasks are implemented on the <database> type. For more information about the attributes you can configure for the <database> type, see the Ant.

To use Liquibase with Ant:

  1. Ensure you have installed Ant. To verify that Ant is installed, run ant -version at the command prompt. You will get the output that looks like Apache Ant(TM) version 1.10.11 compiled on July 10 2021. Liquibase Ant tasks require Ant 1.7.1 or later versions.
  2. Create a Liquibase project directory to store all Liquibase and Ant files.
  3. Create an Ant build file called build.xml to specify your configuration settings with tasks, targets, and dependencies. The build.xml file also lets you define the needed Liquibase properties. See the example of the build.xml example with a basic configuration.
  4. Include Liquibase in your Ant classpath and load it by adding the <taskdef> task in the build.xml file:
  5. <project basedir="the/runtime/location/of/Ant" name="example" xmlns:liquibase="antlib:liquibase.integration.ant">
         <taskdef resource="liquibase/integration/ant/antlib.xml" uri="antlib:liquibase.integration.ant">
         <classpath path="the/path/to/the/liquibase.jar/file";the/path/to/the/driver.jar/file;the/path/to/the/directory/containing/the/changelog/file />
         </taskdef>
    </project>

Tip: You can put the Liquibase jar in your ANT_HOME/lib folder.

  1. Create a text file called changelog.sql in your Liquibase project 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. Execute the updateDatabase task by including the values in your Ant build.xml file:
  2. <target name="updateDatabase" depends="prepare">
    <liquibase:updateDatabase changelog-file="com/example/changelog.sql">
    <liquibase:database driver="org.postgresql.Driver" url="${db.url}" user="${db.user}" password="${db.pass}"/>
    </liquibase:updateDatabase>
    </target>
  1. Run the following in the CLI to implement the task and update your database:
  2. ant -f build.xml updateDatabase

After your first update, you will see a new table along with the DATABASECHANGELOG table and DATABASECHANGELOGLOCK table added to the database.

Related links