我使用 fest 做了一些单元测试,现在我需要在 headless 系统上使用 maven 运行 mvn 我的测试。我想使用 Xvfb 运行测试,但我需要帮助配置 Maven 以在测试前启动 Xvfb 并在所有完成后停止它。
最佳答案
使用exec-maven-plugin
:
您必须定义两个执行,一个用于启动您的服务器,一个用于停止它。您必须将这些执行配置与适当的 Maven 阶段联系起来——在测试阶段之前启动 Xvfb,并在测试阶段之后停止 Xvfb。
<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>
这是manage-xvfb.sh
脚本的内容:
#!/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
请注意,您需要在 sudoers 文件中设置 NOPASSWD
。
关于java - 我如何在 Xvfb 上启动 mvn 测试阶段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14690603/