Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I start mvn test phase on Xvfb?

I made some unit-tests using fest, now I need to run mvn my test using maven on a headless system. I would like to run test using Xvfb but I need help to configure maven to start Xvfb before testing and stop it when all is done.

like image 547
Alepac Avatar asked Oct 06 '22 03:10

Alepac


1 Answers

With the exec-maven-plugin:

You'll have to define two executions, one for starting your server and one for stopping it. You'll have to tie those execution configurations with the appropriate maven phase -- start the Xvfb before your test phase, and stop Xvfb after your test phase.

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>

            <executions>
                <execution>
                    <id>exec-at-test-compile</id>
                    <phase>test-compile</phase> <!-- runs right before 'test' -->
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>/home/anew/bin/manage-xvfb.sh</executable>
                        <arguments>
                            <argument>start</argument>
                        </arguments>
                    </configuration>
                </execution>
                <execution>
                    <id>exec-at-prepare-package</id>
                    <phase>prepare-package</phase> <!-- runs right after 'test' -->
                    <goals>
                        <goal>exec</goal>
                    </goals>
                    <configuration>
                        <executable>/home/anew/bin/manage-xvfb.sh</executable>
                        <arguments>
                            <argument>stop</argument>
                        </arguments>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Here is the content of the manage-xvfb.sh script:

#!/bin/bash

XVFB_CMD="sudo /usr/bin/Xvfb :15 -ac -screen 0 1024x768x8"

function stop_xvfb {
  XVFB_PID=`ps ax | pgrep "Xvfb"`
  if [ "${XVFB_PID}" != "" ]; then
    sudo kill ${XVFB_PID}
  fi
}

if [ "${1}" == "start" ]; then
  stop_xvfb
  ${XVFB_CMD} &
elif [ "${1}" == "stop" ]; then
  stop_xvfb
fi 

Note that you'll need to have NOPASSWD set in your sudoers file.

like image 77
Anew Avatar answered Oct 07 '22 17:10

Anew