简体   繁体   English

在Netbeans / Maven中远程调试单元测试

[英]Remotely debug unit tests in Netbeans / Maven

I would like to automatically launch and debug unit tests on a remote machine in a Maven project using Netbeans. 我想使用Netbeans在Maven项目中的远程机器上自动启动和调试单元测试。 All the IDE features should work, such as debugging, the output window, etc. The process has to be fully automated with a single click of "Debug Focused Test Method." 所有的IDE功能都应该起作用,例如调试,输出窗口等。必须通过单击“调试重点测试方法”完全自动化该过程。

The solution is to replace the mvn command with a custom script which will perform an rsync and an ssh . 解决方案是将mvn命令替换为将执行rsyncssh的自定义脚本。 Through the magic of both tools, it works really well. 通过这两种工具的魔力,它确实运行良好。 Also, it should be portable to other Maven IDEs. 而且,它应该可移植到其他Maven IDE。 The current guide is for bash , but the same idea could be implemented in Windows with powershell , groovy , or even cmd . 当前指南是针对bash ,但是相同的想法可以在Windows中使用powershellgroovy甚至cmd (I tried to use Nashorn, but the way to execute other commands while properly preserving all the arguments seemed broken.) (我尝试使用Nashorn,但是在正确保留所有参数的同时执行其他命令的方法似乎已失效。)

  • In Netbeans directory /usr/local/netbeans-8.1/java/maven/bin/ , rename the mvn script: 在Netbeans目录/usr/local/netbeans-8.1/java/maven/bin/ ,重命名mvn脚本:
    • cd /usr/local/netbeans-8.1/java/maven/bin/
    • sudo mv mvn mvn.orig
  • Create new mvn script. 创建新的mvn脚本。

     #!/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 
  • Configure the remote machine: 配置远程机器:
    • Either install mvn , or install Netbeans and put its mvn on the path. 安装mvn或安装Netbeans并将其mvn放在路径上。 I chose the later, but either should be fine. 我选择了后者,但两者都可以。
    • Configure ssh. 配置ssh。 I won't describe how to do it here. 在这里我不会描述如何做。 Make sure that AllowTcpForwarding is set to yes or remote in /etc/ssh/sshd_config . 确保在/etc/ssh/sshd_config中将AllowTcpForwarding设置为yesremote
  • Create new Netbeans configuration that will forward all maven actions (including clean & build) to the remote machine: 创建新的Netbeans配置,该配置会将所有Maven操作(包括清理和构建)转发到远程计算机:

    • Go into Project's properties 进入Project的属性
    • Create new Configuration 创建新配置
    • 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. 若要使用,请在工具栏的下拉菜单中选择新配置,然后照常调用任何Maven目标。

    • I'm still looking for a way to set up this configuration globally. 我仍在寻找一种在全球范围内设置此配置的方法。 If you know how to do this, please tell me! 如果您知道该怎么做,请告诉我!
  • Alternatively, create a Custom Action that has the same properties set. 或者,创建具有相同属性集的“自定义操作”。
  • I should mention that the debug experience works pretty well, thanks to the remote debug capabilities built into the JVM and the version/dependency tracking of Maven and the excellent Maven integration in Netbeans. 我应该提到,由于JVM内置了远程调试功能以及Maven的版本/相关性跟踪以及Netbeans中出色的Maven集成,因此调试体验效果很好。 I really like that you're able to easily step into the code of other projects (and it can resolve specific versions). 我真的很喜欢您能够轻松进入其他项目的代码(并且它可以解析特定版本)。

Surefire / Failsafe插件支持开箱即用的远程调试: http : //maven.apache.org/surefire/maven-surefire-plugin/examples/debugging.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM