Flyway Tutorial – Execute Migrations using Maven

No Comments

This is the second part of our threepart series on Flyway. The first part showing the installation, basic concepts and a sample project can be found from here. Executing Migrations using Maven is nothing that is really required to work successfully with Flyway. But it can be quite convenient if Maven is anyway used as part of the project’s technology stack. In this case a local installation of the command-line tool might not be needed at all.

Flyway Maven PlugIn

If there is one tool that really has a lot of PlugIns then it is Maven. Thus it is not really surprising there is a PlugIn for Flyway as well. As any other PlugIn it can be included to the Maven pom.xml-file in the build/plugins-section. Let’s take a look at a complete pom-file containing the needed configuration-sections to utilize the Flyway PlugIn:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

Hopefully the example stands mostly for itself. Still there are a few things to mention though. First of the version of the Maven PlugIn should match the version of the command-line installation of Flyway. That version can easily be found out by just executing the flyway-command without further parameters. Please note that even if the command-line version is not used locally it will be used on test-systems and in production. The versions used in all those different places should really match to avoid strange problems. On my machine we see this is release 4.0.3.

ThomassacBookcc:db_flyway_sample thomasjaspers$ flyway
Flyway 4.0.3 by Boxfuse

You might need to scroll up a bit to see it as the usage-message is quite long.

The sample project introduced in the first blog post of this series does also include the proper Maven configuration.

Then some required configuration from the flyway.conf-file must be repeated in the pom-file. This includes at least the required information to connect to the database and where to find the Migrations and what is the separator used in the filenames. Anyway, no big deal in the end especially as that needs to be done only once for the whole team. Typically this configurations stays pretty stable during a project.

The last part that requires some attention is to add the proper database driver as a dependency to the Flyway Maven PlugIn. In this example this is the driver for PostgreSQL.

Execution and Directory Structure

Flyway can now be executed using the Maven-PlugIn by issuing the following command in the base-directory of the Maven-project containing the Flyway Migrations.

ThomassacBookcc:db_flyway_sample thomasjaspers$ mvn flyway:migrate
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building com.codecentric 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] --- flyway-maven-plugin:4.0.3:migrate (default-cli) @ com.codecentric ---
[WARNING] Unknown configuration property: flyway.version
[INFO] Flyway 4.0.3 by Boxfuse
[INFO] Database: jdbc:postgresql://localhost:5432/flywaydemo (PostgreSQL 9.4)
[INFO] Successfully validated 3 migrations (execution time 00:00.011s)
[INFO] Current version of schema "public": 2.1
[INFO] Schema "public" is up to date. No migration necessary.
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.534 s
[INFO] Finished at: 2017-01-20T11:47:50+01:00
[INFO] Final Memory: 10M/309M
[INFO] ------------------------------------------------------------------------

As with the command-line version of Flyway information on executing the Migrations is printed out. In this case everything is already up-to-date. When it comes to the directory structure for storing the Migrations it seems scr/main/resources/migrations/<release> is a quite natural choice when using Maven anyway. The flyway.conf configuration file is then located in the top-level Maven-directory along with the pom.xml-file. When packing the files for an installation package of course this directory structure must be preserved to keep everything working when installing test or productive systems using the command-line version of Flyway.

Conclusion & Outlook

Straightforward as this is there is not really an awfully lot to conclude here. The Maven PlugIn is a convenient choice to execute Flyway on the developer’s machines if Maven is anyway already used in the project. The final part of this short series will deal with Migrations not written in SQL but in Java. The link will be added here once that post is out :-).


Thomas Jaspers

Long-term experience in agile software projects using
Java enterprise technologies. Interested in test automation tools and concepts.

Share on FacebookGoogle+Share on LinkedInTweet about this on TwitterShare on RedditDigg thisShare on StumbleUpon


Your email address will not be published. Required fields are marked *