Actually I use this plugin configuration:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>start-xvfb</id>
<phase>process-test-classes</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo message="Starting xvfb ..." />
<exec executable="Xvfb" spawn="true">
<arg value=":1" />
</exec>
</tasks>
</configuration>
</execution>
<execution>
<id>shutdown-xvfb</id>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo message="Ending xvfb ..." />
<exec executable="killall">
<arg value="Xvfb" />
</exec>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
The good is that you get a background process (with spawn = "true"
) and you can kill the Xvfb process (with killall
) without having to write any script. Moreover in my Ubuntu distro I don't hav to set any special setting in my sudoers file to let this works.
The shutdown-xvfb execution is executed at the end of the test phase but (here is the problem) is not executed if the test fail. This is not a problem if you want to restart another test (the old Xvfb is still running and the new one can't run, but this is not a problem) but the problem is that the resources are still busy by Xvfb. A workaround for this could be to add testFailureIgnore = "true"
to the configuration of maven-surefire-plugin but this way I can't easily see if some test fail.