• 0.8.2

Usage of MySQL Maven Plugin for Integration Testing

First, add these two supplementary plugins to your pom.xml:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.8</version>
        <executions>
          <execution>
            <goals>
              <goal>reserve-network-port</goal>
            </goals>
            <configuration>
              <portNames>
                <portName>mysql.port</portName>
              </portNames>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.8</version>
        <executions>
          <execution>
            <goals>
              <goal>unpack</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>com.jcabi</groupId>
                  <artifactId>mysql-dist</artifactId>
                  <version>5.6.14</version>
                  <classifier>${mysql.classifier}</classifier>
                  <type>zip</type>
                  <overWrite>false</overWrite>
                  <outputDirectory>${project.build.directory}/mysql-dist</outputDirectory>
                </artifactItem>
              </artifactItems>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

Then, configure jcabi-mysql-maven-plugin:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>com.jcabi</groupId>
        <artifactId>jcabi-mysql-maven-plugin</artifactId>
        <version>0.8.2</version>
        <executions>
          <execution>
            <id>mysql-test</id>
            <goals>
              <goal>classify</goal>
              <goal>start</goal>
              <goal>stop</goal>
            </goals>
            <configuration>
              <port>${mysql.port}</port>
              <data>${project.build.directory}/mysql-data</data>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

Make sure that you have perl installed on your build machine. It is required by MySQL scripts, which pre-configure an empty database before it can be used.

Now you need to know this temporary port number in your integration tests:

<project>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <configuration>
              <systemPropertyVariables>
                <mysql.port>${mysql.port}</mysql.port>
              </systemPropertyVariables>
            </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
  [...]
</project>

In your integration tests connect to this on-demand instance of MySQL server:

public class FooITCase {
  private static final String PORT = System.getProperty("mysql.port");
  @Test
  public void worksWithMysqlServer() {
    // make a connection to
    // jdbc:mysql:localhost:XXX/root?user=root&password=root
    // where XXX is the port number
  }
}

User name, database name and password are always "root".

See the list of MySQL distributions supported out-of-the-box.

Since version 0.4 you can start MySQL and keep it running in foreground, until Ctrl-C is hit:

$ mvn jcabi-mysql:run

Cutting Edge Version

If you want to use current version of the product, you can do it with this configuration in your pom.xml:

<pluginRepositories>
  <pluginRepository>
    <id>oss.sonatype.org</id>
    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
  </pluginRepository>
</pluginRepositories>
<build>
  <plugins>
    <plugin>
      <groupId>com.jcabi</groupId>
      <artifactId>jcabi-mysql-maven-plugin</artifactId>
      <version>1.0-SNAPSHOT</version>
      [...]
    </plugin>
  </plugins>
</build>