January 22, 2016 | Philip Aston
Here at Push Technology, Java is at the heart of everything we do so we’re really interested in tools and technologies that improve our Java productivity. If you care about Java, you probably have some interest in Maven and how it can be used to facilitate better build and test of your code. (Yes, all the cool kids are using Gradle, but conversely all the cool kids are not using Java at all these days). Love it or hate it, Maven has its uses and can be used to enforce build conformity that leads to less build violations of the Principle of Least Surprise.
We use Maven for building our code, running tests, running code coverage, running FindBugs and more, and on the whole it works pretty well. One of Maven’s shortcomings (or strengths, depending on your perspective) is that anything you do requires a plugin; Maven has no general scripting capability, any scripting you require means falling back on Ant. Falling back on Ant is OK, but it means you have stepped outside of Maven’s fascist build architecture and that means that you are unlikely to get the benefit of cross-plugin integration. Tests are a case in point – Maven has various infrastructure plugins for running normal unit (via the SureFire plugin) and integration tests (via the Failsafe Plugin), but it has no generic capability for running integration tests when doing so relies on starting back-end infrastructure, and in particular middleware.
Various middleware projects have addressed this by building their own Maven plugins for starting and stopping servers – see, for instance, the jetty-maven-plugin or the jboss-maven-plugin – and actually this turns out the best way to run integration tests against middleware servers… like Diffusion. Enter the diffusion-maven-plugin.
The diffusion-maven-plugin fulfils a simple task – it allows you to start and stop a Diffusion server as part of your Maven build. To use it, first of all you need to set the value of the environment variable
DIFFUSION_HOME to the location of your Diffusion installation. Next you need to set up the Maven Failsafe Plugin to run your integration tests, so add the following to your POM:
<plugin> <artifactId>maven-failsafe-plugin</artifactId> <executions> <execution> <goals> <goal>integration-test</goal> <goal>verify</goal> </goals> </execution> </executions> </plugin>
OK, so this will ensure that integration tests residing in the
src/test/java directory and ending in
IT.java will get run. We now want to bind starting and stopping to the Maven phases created exactly for this purpose –
post-integration-test. First let’s start the server in
pre-integration-test. Before we do this we need to tell Maven where to find the plugin, like so:
<repositories> <repository> <id>push-repository</id> <url>http://download.diffusiondata.com/maven/</url> </repository> </repositories>
and now we can tell Maven to start the plugin in the correct phase:
<plugin> <artifactId>diffusion-maven-plugin</artifactId> <groupId>com.pushtechnology.diffusion.maven.plugin</groupId> <version>1.0</version> <executions> <execution> <id>start-diffusion</id> <phase>pre-integration-test</phase> <goals> <goal>start</goal> </goals> </execution> </executions> </plugin>
By default, the plugin will use the installation in
DIFFUSION_HOME to work its magic, but if you want to point somewhere else you can define your Diffusion installation as a dependency. There is an example of this in the plugin itself. Then we want to stop the test at the end, so add the
stop goal as another execution in the plugin:
<execution> <id>stop-diffusion</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> </goals> </execution>
…and we are done! Run the test via
mvn clean install and you should see the server start up at the appropriate point and be stopped at the end.
For more information, check out the diffusion-maven-plugin on GitHub.