如何在 Java/Groovy 中进行系统命令调用? [英] How to make System command calls in Java/Groovy?

查看:24
本文介绍了如何在 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() 命令.例如,要从 groovy 脚本执行 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 命令外壳.由于 mvn.bat 是一个批处理脚本,您需要它.对于 Unix,您可以调用系统 shell.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆