To convert Liquibase XML changelogs to SQL with Maven, you run the Liquibase Maven plugin's updateSQL goal, which writes the generated SQL to a file instead of executing it against the database. The shortest working command is mvn liquibase:updateSQL -Dliquibase.outputFile=update.sql, run from the directory containing your pom.xml. This produces a plain SQL script for the target database without touching it. You can also use mvn liquibase:updateSQL without the output file property to print the SQL to the console.
What You'll Need
- A Maven project with a
pom.xmlthat includes the Liquibase Maven plugin, or the ability to invoke it directly. - A Liquibase changelog file (typically XML, YAML, JSON, or SQL) referenced in your plugin configuration or passed via properties.
- A database connection configured in the plugin or via a
liquibase.propertiesfile: URL, username, password, and driver. - The JDBC driver for your database available on the Maven plugin classpath (often added as a plugin dependency).
- Java and Maven installed and working from the command line.
Step-by-Step
- Add the Liquibase Maven plugin to your
pom.xml. In the<build><plugins>section, add an entry fororg.liquibase:liquibase-maven-plugin. Specify a version. Inside its<configuration>, set the changelog file path, the database URL, username, password, and driver class. Example configuration:
Labels and exact configuration element names can differ slightly by plugin version, so check the plugin documentation for your version if something is not recognized.<plugin> <groupId>org.liquibase</groupId> <artifactId>liquibase-maven-plugin</artifactId> <version>4.27.0</version> <configuration> <changeLogFile>src/main/resources/db/changelog/db.changelog-master.xml</changeLogFile> <url>jdbc:postgresql://localhost:5432/mydb</url> <username>myuser</username> <password>mypassword</password> <driver>org.postgresql.Driver</driver> </configuration> <dependencies> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.7.3</version> </dependency> </dependencies> </plugin> - Ensure your changelog path is correct. The
changeLogFilecan be a filesystem path or a classpath resource. If it is undersrc/main/resources, use a classpath-relative path likedb/changelog/db.changelog-master.xmland make sure the resources are on the classpath. A wrong path is the most common cause of "file not found" errors. - Run the conversion goal. From the project root, execute:
This tells Liquibase to generate the SQL for all pending changesets and write it tomvn liquibase:updateSQL -Dliquibase.outputFile=target/update.sqltarget/update.sql. The database is not modified; Liquibase only reads the changelog and the current database state (to determine which changesets are already applied). - Review the generated SQL. Open the output file. It contains the SQL statements that would be executed, in order, with comments identifying each changeset. Verify that the statements match your expectations for the target database.
- Run without an output file if you only need a preview. Omit the
-Dliquibase.outputFileproperty to print the SQL to the Maven console. This is useful for quick checks but not for large changelogs. - Use a properties file instead of inline configuration (optional). If you prefer not to hardcode credentials in
pom.xml, create aliquibase.propertiesfile in the project root with keys likechangeLogFile,url,username,password, anddriver. Then run the same Maven goal; Liquibase picks up the properties file automatically if it is on the classpath or in the working directory. - Specify a different output format if needed. The
updateSQLgoal always produces SQL. If you need to convert XML to another format (for example, YAML), that is not a built-in Maven goal; you would need to use the Liquibase CLI'schangelogSyncSQLor write a custom script. For SQL generation,updateSQLis the correct goal.
Common Mistakes
- Wrong changelog path. Using a filesystem path when the file is on the classpath, or vice versa. Check that the path matches the actual location and that resources are included in the build.
- Missing database driver. The plugin needs the JDBC driver on its classpath. Add it as a plugin dependency, not just a project dependency.
- Incorrect database URL or credentials. The plugin must connect to the database to determine which changesets are already applied. If the connection fails, no SQL is generated.
- Assuming the output SQL is database-agnostic. Liquibase generates SQL specific to the target database. If you need SQL for a different database, change the URL and driver accordingly.
- Forgetting that
updateSQLdoes not update the database. It only generates SQL. To apply changes, usemvn liquibase:update. - Using an outdated plugin version. Older versions may have different configuration element names or lack support for certain changelog features. Use a recent version and consult its documentation.
Tips
- Use
-Dliquibase.outputFileto write to a file; without it, the SQL goes to the console and can be hard to read for large changelogs. - Keep database credentials out of
pom.xmlby using aliquibase.propertiesfile or Maven properties that are supplied at runtime. - Run
mvn liquibase:validatefirst to check the changelog for errors before generating SQL. - If you need to see the SQL for a specific number of changesets, use the
countproperty to limit the output. - For repeatable builds, pin the plugin and driver versions in
pom.xml.
Troubleshooting
If the command fails, work through these checks in order:
- "ChangeLog file not found": Verify the path. If the file is in
src/main/resources, use a classpath-relative path and ensure the resources are copied totarget/classes(runmvn resources:resourcesif needed). - "No suitable driver": Add the JDBC driver as a dependency of the Liquibase plugin, not just the project. Check the driver class name matches your database.
- Connection refused or authentication failed: Confirm the database is running, the URL is correct, and the username/password have access. Test the connection with a simple JDBC client if possible.
- SQL output is empty: This usually means there are no pending changesets (all are already applied). Check the
DATABASECHANGELOGtable in your database. If you expected changes, ensure the changelog includes new changesets and that thechangeLogFilepoints to the master file that includes them. - Plugin configuration not recognized: Ensure you are using the correct element names for your plugin version. Refer to the official Liquibase Maven plugin documentation for the version you are using.
- Build fails with a missing plugin: Check that the plugin is declared in
pom.xmland that Maven can download it from the central repository. If you are offline, the plugin must be in your local repository.
If none of these resolve the issue, run Maven with -X to enable debug logging and inspect the Liquibase output for more details.
Frequently Asked Questions
Can I convert XML to SQL without a database connection?
No. Liquibase needs to connect to the database to determine which changesets are already applied, so it can generate only the pending SQL. Without a connection, the goal fails.
Does updateSQL modify the database?
No. It only generates SQL and writes it to a file or console. To apply changes, use mvn liquibase:update.
Can I generate SQL for a different database than the one I'm connected to?
Yes, but you must change the URL and driver to point to the target database type. Liquibase generates SQL based on the database URL you provide.
Where does the output file go if I don't specify a path?
If you omit -Dliquibase.outputFile, the SQL is printed to the console. If you specify a filename without a directory, it is created in the current working directory.
Why is the generated SQL missing some changesets?
Liquibase only includes changesets that are not yet recorded in the DATABASECHANGELOG table. If a changeset was already applied, it is skipped. Check the table to confirm.
Can I use this with a Spring Boot project?
Yes. The Liquibase Maven plugin works independently of Spring Boot. Configure it in pom.xml as described, and run the Maven goal. Spring Boot's auto-configuration does not interfere with the plugin.