I am pleased to announce the availability of the In-Memory Database Maven Plugin version 1.3.0.  This Maven plugin can be used to launch and shutdown an embedded in-memory SQL database within the Maven build life-cycle. During the launch the database can be seeded using DDL/DML scripts and/or DBUnit data sets.

The following database implementations are supported:

  • derby – An embedded/in-memory Apache Derby database.
  • h2 – An embedded/in-memory H2 database.
  •  hsqldb – An embedded/in-memory HSQLDB database.

The following source file formats are supported:

  • .sql – Contains DDL/DML SQL commands to create the database structure and/or insert test data
  • .csv – A file containing a comma separated value (CSV) data set. The first row of the data set contains the column names and the file name corresponds to the table name.
  • .xml – A file containing aflat DBUnit XML data set.
  • .dbunit.xml – A file containing aDBUnit XML data set.
  • .xls – A Microsoft Excel spread sheet containing one or more work sheets. The name of the work sheet correspond to the table name and the first row of each work sheet contains the column names.

Example

The In-Memory Database Maven Plugin can be used to automate integration tests having a dependency on an external database server.

pom.xml

The POM here is from taken from the webpp integration test for the In-Memory Database Maven Plugin.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="
             http://maven.apache.org/POM/4.0.0
             http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>webapp</groupId>
    <artifactId>webapp</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>com.btmatthews.maven.plugins.inmemdb</groupId>
                <artifactId>inmemdb-maven-plugin</artifactId>
                <version>1.3.0</version>
                <configuration>
                    <monitorKey>inmemdb</monitorKey>
                    <monitorPort>11527</monitorPort>
                </configuration>
                <executions>
                    <execution>
                        <id>run</id>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <phase>pre-integration-test</phase>
                        <configuration>
                            <daemon>true</daemon>
                            <type>derby</type>
                            <database>test</database>
                            <username>sa</username>
                            <password></password>
                            <sources>
                                <script>
                                    <sourceFile>src/test/resources/create_database.sql</sourceFile>
                                </script>
                                <dataSet>
                                    <sourceFile>src/test/resources/users.dbunit.xml</sourceFile>
                                </dataSet>
                            </sources>
                        </configuration>
                    </execution>

The In-Memory Database Maven Plugin is configured here to launch an in-memory Apache Derby database server as a daemon process during the pre-integration-test phase of the build life cycle. The database is initialised with a database schema and some seed data.

                    <execution>
                        <id>stop</id>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                        <phase>post-integration-test</phase>
                    </execution>

The In-Memory Database Maven Plugin is configured here to shutdown the in-memory Apache Derby database server during the post-integration-test phase of the build life cycle.

                </executions>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.8.v20121106</version>
                <configuration>
                    <stopKey>jetty</stopKey>
                    <stopPort>19080</stopPort>
                    <daemon>true</daemon>
                    <webApp>
                        <contextPath>/</contextPath>
                    </webApp>
                    <jettyXml>src/test/jetty/jetty.xml</jettyXml>
                    <connectors>
                        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                            <port>9080</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                    </connectors>
                </configuration>
                <executions>
                    <execution>
                        <id>start-jetty</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>

The Jetty Maven Plugin is configured here to launch the Jetty servlet container as a daemon process during the pre-integration-test phase of the build life cycle.

                    <execution>
                        <id>stop-jetty</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>

The Jetty Maven Plugin is configured here to shutdown the Jetty servlet container during the post-integration-test phase of the build life
cycle.

                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.derby</groupId>
                        <artifactId>derbyclient</artifactId>
                        <version>10.9.1.0</version>
                    </dependency>
                    <dependency>
                        <groupId>commons-dbcp</groupId>
                        <artifactId>commons-dbcp</artifactId>
                        <version>1.3</version>
                    </dependency>
                </dependencies>
            </plugin>

The org.apache.derby:derbyclient and commons-dbcp:commons-dbcp dependencies are required in order to define the data source that connects to the in-memory Apache Derby database.

            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.12.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

The Maven Failsafe Plugin is used to execute the integration tests during the integration-test phase of the build life cycle.

        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.btmatthews.selenium.junit4</groupId>
            <artifactId>selenium-junit4-runner</artifactId>
            <version>1.0.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

The com.btmatthews.selenium.junit4:selenium-junit-runner and junit:junit dependencies are required by the integration tests.

jetty.xml

Add the following fragment of XML to the jetty.xml to define a data source for the test database.

<New class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg>
        <Ref id="Server" />
    </Arg>
    <Arg>jdbc/test</Arg>
    <Arg>
        <New class="org.apache.commons.dbcp.BasicDataSource">
            <Set name="username">sa</Set>
            <Set name="password"></Set>
            <Set name="url">jdbc:derby://localhost/memory:test</Set>
            <Set name="driverClassName">org.apache.derby.jdbc.ClientDriver</Set>
        </New>
    </Arg>
</New>

web.xml

Add the following <resource-ref> to the web.xml to make the data source defined in
jetty.xml available inside the web application:

<resource-ref>
    <res-ref-name>jdbc/test</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

Maven Central Coordinates

The In-Memory Database Maven Plugin has been published in Maven Central at the following coordinates:

<plugin>
    <groupId>com.btmatthews.maven.plugins.inmemdb</groupId>
    <artifactId>inmemdb-maven-plugin</artifactId>
    <version>1.3.0</version>
</plugin>

Credits

This project contains contributions from:

License & Source Code

The In-Memory Database Maven Plugin is made available under the Apache License and the source code is hosted on GitHub at https://github.com/bmatthews68/inmemdb-maven-plugin.