Fork me on GitHub

Description

This zookeeper-maven-plugin enables integration testing for applications utilizing ZooKeeper servers. It will enable you to start a ZooKeeper server prior to running integration tests, and then enable you to shut it down gracefully. This plugin was inspired by the jetty-maven-plugin and behaves similarly, using a shutdownString and shutdownPort to shut down a specific instance during different phases of a Maven build lifecycle.

Configuration options are available for many of ZooKeeper’s own configuration options. It uses the ${project.build.directory}/zmp to store its running state, including the ZooKeeper dataDir. Multiple concurrent executions are possible, but must each must specify a unique clientPort and shutdownPort option.

Getting Started

To add this plugin to your project, configure the plugin similarly to:

  <build>
    <plugins>
      <plugin>
        <groupId>net.revelc.code</groupId>
        <artifactId>zookeeper-maven-plugin</artifactId>
        <version>1.2.0</version>
        <configuration>
          <!-- This is a unique string to kill the service when finished -->
          <shutdownString>UniqueShutDownString</shutdownString>
          <!-- This is the port the plugin listens on for the shutdownString -->
          <shutdownPort>21122</shutdownPort>
          <!-- Configure the ZooKeeper port; defaults to listening on 127.0.0.1 -->
          <clientPort>21123</clientPort>
        </configuration>
        <executions>
          <execution>
            <!-- In this example, only one ZooKeeper service is started and the
            two goals share the same configuration, so the stop goal sends the
            same shutdown message that the start goal is listening for -->
            <id>run-zookeeper</id>
            <goals>
              <goal>start</goal><!-- runs at pre-integration-test phase -->
              <goal>stop</goal><!-- runs at post-integration-test phase -->
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.17</version>
        <executions>
          <execution>
            <!-- configure failsafe to execute some ITs which use ZooKeeper -->
            <id>run-integration-tests</id>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

and build your project, similarly to (it runs at the verify phase by default):

mvn verify

Retaining ZooKeeper’s Data/State

By default, zookeeper-maven-plugin will create a zmp directory under your project’s ${project.build.directory}. This directory is typically cleared and its children recreated with each new build. You can change the zmp directory providing the following configuration:

<zmpDir>/path/to/different/zmp</zmpDir>

Now, future builds will create use that location, creating it if it doesn’t exist.

You can take it a step further and add the following to keep data between builds.

<keepPreviousState>true</keepPreviousState>

Both configurations must be present if you wish to keep data between builds. The boolean ensures the plugin itself doesn’t delete the directory, and using a different directory than the default ensures it is safe from mvn clean (assuming the directory you’ve chosen is outside your ${project.build.directory} and you don’t have any special clean tasks configured).