I would like to automatically launch and debug unit tests on a remote machine in a Maven project using Netbeans. All the IDE features should work, such as debugging, the output
Surefire/Failsafe plugin supports remote debugging out of the box: http://maven.apache.org/surefire/maven-surefire-plugin/examples/debugging.html
The solution is to replace the mvn command with a custom script which will perform an rsync and an ssh. Through the magic of both tools, it works really well. Also, it should be portable to other Maven IDEs. The current guide is for bash, but the same idea could be implemented in Windows with powershell, groovy, or even cmd. (I tried to use Nashorn, but the way to execute other commands while properly preserving all the arguments seemed broken.)
/usr/local/netbeans-8.1/java/maven/bin/, rename the mvn script:
cd /usr/local/netbeans-8.1/java/maven/bin/sudo mv mvn mvn.origCreate new mvn script.
#!/bin/bash
if [ -z "$REMOTE" ] ; then
SCRIPTDIR=$(dirname "$0")
"$SCRIPTDIR/mvn.orig" "$@"
else
if [ -z "$REMOTE_BASE_DIR" ] ; then
echo "ERROR: Please set environment variable REMOTE_BASE_DIR to the folder which contains the project directory"
exit
fi
PROJECT_DIR=$(basename "$(pwd)")
REMOTE_PROJECT_DIR=$REMOTE_BASE_DIR/$PROJECT_DIR/
ARGS=
for var in "$@"
do
ARGS="$ARGS \\\"$var\\\""
done
echo "Syncing project directory..."
(set -x; rsync -aczhW --progress --delete --exclude '.git' --exclude 'target' $RSYNC_OPTS ./ "$REMOTE:\"$REMOTE_PROJECT_DIR\"")
echo "Executing maven..."
if [ "$REMOTE_PORT" = '${jpda.address}' ] ; then
(set -x; ssh ${REMOTE} "cd \"$REMOTE_PROJECT_DIR\"; mvn $ARGS")
else
(set -x; ssh -R $REMOTE_PORT:localhost:$REMOTE_PORT ${REMOTE} "cd \"$REMOTE_PROJECT_DIR\"; mvn $ARGS")
fi
fi
mvn, or install Netbeans and put its mvn on the path. I chose the later, but either should be fine.AllowTcpForwarding is set to yes or remote in /etc/ssh/sshd_config.Create new Netbeans configuration that will forward all maven actions (including clean & build) to the remote machine:
In "Set properties" add:
Env.REMOTE_BASE_DIR=<directory on the server that will contain your project directory>
Env.REMOTE=<address of remote machine>
Env.REMOTE_PORT=${jpda.address}
Env.RSYNC_OPTS=<optional, but could be another --exclude>
To use, select the new configuration in the drop-down menu in the toolbar, and invoke any maven goal as usual.