如何在Java / Groovy中进行系统命令调用? [英] How to make System command calls in Java/Groovy?
问题描述
我想做的是从groovy脚本中调用Maven。有问题的groovy脚本用作maven包装器,可通过下载标签并在下载的内容上调用maven来构建J2EE项目。我应该如何完成调用Maven来构建/打包EAR(groovy脚本已经能够从SCM下载标签)。
What I want to do is invoke maven from a groovy script. The groovy script in question is used as a maven wrapper to build J2EE projects by downloading a tag and invoking maven on what was downloaded. How should I accomplish invoking maven to build/package the EAR (the groovy script is already capable of downloading the tag from SCM).
推荐答案
<在Groovy中调用外部进程的最简单方法是对字符串使用execute()命令。例如,要从常规脚本执行Maven,请运行以下命令:
The simplest way to invoke an external process in Groovy is to use the execute() command on a string. For example, to execute maven from a groovy script run this:
"cmd /c mvn".execute()
如果要捕获命令的输出并打印出来,可以执行以下操作:
If you want to capture the output of the command and maybe print it out, you can do this:
print "cmd /c mvn".execute().text
开始的'cmd / c'调用Windows命令shell。由于mvn.bat是批处理脚本,因此您需要此脚本。对于Unix,您可以调用系统外壳程序。
The 'cmd /c' at the start invokes the Windows command shell. Since mvn.bat is a batch script you need this. For Unix you can invoke the system shell.
这篇关于如何在Java / Groovy中进行系统命令调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!