groovy(java):exec()不会脱离进程(Intellij IDEA与Maven + TestNG) [英] groovy (java): exec() do not detach from process (Intellij IDEA vs Maven+TestNG)
问题描述
我有启用了TestNG和Surefire插件的Groovy Maven2测试项目.
I have Groovy Maven2 test project with TestNG and Surefire plugin enabled.
我想在最后一种测试方法中启动外部进程(* .cmd文件,该文件以* .exe文件开头),完成我的测试,并使进程在测试后运行.
I want to launch external process (*.cmd file which start some *.exe file) in last test method, finish my tests and left process running after tests.
我尝试了以下代码来做到这一点:
I tried the following codes to do it:
1次尝试
def builder = new ProcessBuilder('cmd','/c <name>.cmd')
builder.directory( ( new File( <path_to_working_directory> ) ) )
builder.start()
2次尝试(带有和不带有start cmd选项)
2 attempt (with and without start cmd option)
Runtime.getRuntime().exec( "cmd /c start <name>.cmd", null , ( new File( <path_to_working_directory> ) ) )
3次尝试
( new AntBuilder() ).exec(
dir: "<path_to_working_directory>",
executable: "<name>.cmd"
)
.cmd在哪里:
set path=<path_to_execFile>;%path%
start <execFileName>.exe
当我通过运行"功能(Alt + Shift + F10)从Intellij IDEA启动这些代码中的每一个时,代码均成功执行,进程启动并在测试完成后运行.
When I launch each of these codes from Intellij IDEA via 'Run' functionality (Alt+Shift+F10) codes execute successfully, process started and run after test finishes.
当我从Intellij IDEA Maven任务启动所有这些代码时,干净的Maven安装(甚至包括来自Jenkins的Maven任务)过程已成功启动,但测试仍在运行.我需要手动将其杀死.当我手动终止测试流程(Maven流程)时,我启动的外部流程将继续按预期工作.
When I launch each of these codes both from Intellij IDEA Maven task, clean Maven installation (and even Maven task from Jenkins) process started successfully but test remains running. I need to kill it manually. When I kill test process (Maven process) manually my launched external process continue to work as I expect.
这个挂起的测试过程暂时让我头疼.
This hung test process is my headache for the moment.
我浏览了许多材料,但没有发现任何根本原因,解决此问题的方法甚至解决方法.我看到我所有的尝试(也许,除了AntBuilder()之外)都会创建分离的进程.我想这可以与JVM设置关联.但是我找不到那个.
I looked through a lot of materials but didn't find any root cause, fix and even workaround for this issue. I see that all my attempts (perhaps, except of AntBuilder()) create deattached processes. I suppose that this can be connected with JVM settings. But I coudnl't find to which one.
我也尝试过
"full command to run my cmd".execute()
但这对我也没有帮助.
您能帮我解决问题吗?
预先感谢!
推荐答案
因此,我在这里看不到任何问题的答案.但是我有一些更新.
So, I do not see any answers for my issue here. But I have some updates.
我发现我可以使用PsExec工具代替直接的cmd调用:
I found that I can use PsExec tool instead of direct cmd calling:
def builder = new ProcessBuilder( 'psexec', 'cmd', '/c', '<name>.cmd' )
builder.directory( ( new File( <path_to_working_directory> ) ) )
builder.start()
当我仅从干净的Maven(不是从Jenkins)启动该代码时,此代码运行良好:进程已启动,Maven任务成功完成,该进程继续运行.
And this code works fine when I launch it from clean Maven only (not from Jenkins): process started, Maven task completes successfully, the process continues to run.
但是在执行此代码作为某些Maven2 Jenkins任务的一部分时,我不得不再次发出该命令:psexec已启动,但Jenkins任务正在运行,并且在手动终止Jenkins任务之前,我的进程未启动.
But during execute this code as part of some Maven2 Jenkins task I faced to issue again: psexec started but Jenkins task is running and my process does not started before I terminate Jenkins task manually.
为避免此问题,我创建了一个简单的附加Groovy服务脚本,该脚本以监听模式启动(并且在初始计算机准备过程中,手动在目标计算机上编写TCP服务器).该脚本始终在计算机上运行.
To avoid this issue I created simple additional Groovy service script I launch in listen mode (and Writing a TCP Server) on target machine manually during initial machine preparation. This script is running on machine always.
我将这个命令文件的侦听器名称发送给我,以从我从Jenkins启动的测试中执行,并且该命令成功执行了所有cmd:进程启动,Jenkins任务成功完成,进程继续运行.我在此侦听器中使用processbuilder.
I send to this listener name of command file to execute from my test I launch from Jenkins and it executes all cmds successfully: processes start, Jenkins task completes successfully, processes continue to run. I use processbuilder inside this listener.
对于发送名称,我使用简单套接字(编写TCP客户端)
For name sending I use simple socket (Writing a TCP Client)
我还找到了如何在Win32上将孩子与进程树分离?.但是对于我来说,我的方式看起来更像Groovy.
Also, I found how to detach child from process tree on win32?. But for me system my way looks more Groovy I think.
这篇关于groovy(java):exec()不会脱离进程(Intellij IDEA与Maven + TestNG)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!