Want to deploy your JVM, Node.js and Go apps effortlessly to AWS? Try our service Boxfuse

First Steps: Maven

Prerequisites

  • Java 6+
  • A working Maven 2 or 3 install

Creating the project

We're going to create our project using the Maven Archetype Plugin by issuing the following command:

> mvn archetype:generate -B ^
        -DarchetypeGroupId=org.apache.maven.archetypes ^
        -DarchetypeArtifactId=maven-archetype-quickstart ^
        -DarchetypeVersion=1.1 ^
        -DgroupId=foo ^
        -DartifactId=bar ^
        -Dversion=1.0-SNAPSHOT ^
        -Dpackage=foobar

We are now ready to get started. Let's jump into our project:

> cd bar

Integrating Flyway

Let's integrate Flyway and H2 into our new pom.xml and configure Flyway so it can successfully connect to H2:

<project ...>
    ...
    <build>
        <plugins>
            <plugin>
                <groupId>org.flywaydb</groupId>
                <artifactId>flyway-maven-plugin</artifactId>
                <version>4.2.0</version>
                <configuration>
                    <url>jdbc:h2:file:./target/foobar</url>
                    <user>sa</user>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>com.h2database</groupId>
                        <artifactId>h2</artifactId>
                        <version>1.4.191</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

Creating the first migration

We create the migration directory src/main/resources/db/migration.
Followed by a first migration called src/main/resources/db/migration/V1__Create_person_table.sql:

create table PERSON (
    ID int not null,
    NAME varchar(100) not null
);

Migrating the database

It's now time to execute Flyway to migrate our database:

bar> mvn flyway:migrate

If all went well, you should see the following output:

[INFO] Creating Metadata table: "PUBLIC"."schema_version"
[INFO] Current version of schema "PUBLIC": << Empty Schema >>
[INFO] Migrating schema "PUBLIC" to version 1 - Create person table
[INFO] Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.062s).

Adding a second migration

If we now add a second migration called src/main/resources/db/migration/V2__Add_people.sql:

insert into PERSON (ID, NAME) values (1, 'Axel');
insert into PERSON (ID, NAME) values (2, 'Mr. Foo');
insert into PERSON (ID, NAME) values (3, 'Ms. Bar');

and execute it by issuing:

bar> mvn flyway:migrate

We now get:

[INFO] Current version of schema "PUBLIC": 1
[INFO] Migrating schema "PUBLIC" to version 2 - Add people
[INFO] Successfully applied 1 migration to schema "PUBLIC" (execution time 00:00.090s).

Summary

In this brief tutorial we saw how to

  • integrate the Flyway Maven plugin into a project
  • configure it so it can talk to our database
  • write our first couple of migrations

These migrations were then successfully found and executed.

Read the documentation