//

Flyway Tutorial – Execute Migrations using Maven

20.1.2017 | 3 minutes of reading time

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:

1<?xml version="1.0" encoding="UTF-8"?>
2<project xmlns="http://maven.apache.org/POM/4.0.0"
3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5    <modelVersion>4.0.0</modelVersion>
6    <groupId>flywayMigration</groupId>
7    <artifactId>com.codecentric</artifactId>
8    <version>1.0-SNAPSHOT</version>
9    <properties>
10        <flyway.version>4.0.3</flyway.version>
11        <postgres.driver.version>9.2-1002.jdbc4</postgres.driver.version>
12        <database.url>jdbc:postgresql://localhost:5432/flywaydemo</database.url>
13        <database.user>flywaydemo</database.user>
14        <databese.password>flywaydemo</databese.password>
15    </properties>
16    <dependencies>
17        <dependency>
18            <groupId>org.flywaydb</groupId>
19            <artifactId>flyway-core</artifactId>
20            <version>${flyway.version}</version>
21        </dependency>
22    </dependencies>
23    <build>
24        <plugins>
25            <plugin>
26                <groupId>org.flywaydb</groupId>
27                <artifactId>flyway-maven-plugin</artifactId>
28                <version>${flyway.version}</version>
29                <configuration>
30                    <sqlMigrationSeparator>__</sqlMigrationSeparator>
31                    <locations>
32                        <location>filesystem:src/main/resources/flyway/migrations</location>
33                    </locations>
34                    <url>${database.url}</url>
35                    <user>${database.user}</user>
36                    <password>${databese.password}</password>
37                </configuration>
38                <dependencies>
39                    <dependency>
40                        <groupId>postgresql</groupId>
41                        <artifactId>postgresql</artifactId>
42                        <version>${postgres.driver.version}</version>
43                    </dependency>
44                </dependencies>
45            </plugin>
46        </plugins>
47    </build>
48</project>
49

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.

1ThomassacBookcc:db_flyway_sample thomasjaspers$ flyway
2Flyway 4.0.3 by Boxfuse
3 
4Usage
5=====
6...
7

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.

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

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/ 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 :-).

share post

Likes

0

//

More articles in this subject area\n

Discover exciting further topics and let the codecentric world inspire you.

//

Gemeinsam bessere Projekte umsetzen

Wir helfen Deinem Unternehmen

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.