从 Groovy 运行脚本
为了让我的设置更接近“一键部署”,我想使用 groovy 脚本来启动/停止由 bat 脚本控制的其他进程,在文件系统的不同部分甚至不同的机器上运行。
如何执行这些脚本以及如何从各自的工作目录执行这些脚本?
我知道 Java 的
java.lang.Runtime's exec()
但是这有很多问题,我想知道 Groovy 是否也有某种速记法?
谢谢!
In order to get my setup a bit closer to "one click deployment", I would like to use groovy scripts to start/stop other processes controlled by bat scripts, running in different parts of the filesystem and even on different machines.
How to execute these scripts and how to do it from their respective working directory?
I know Java's
java.lang.Runtime's exec()
However there are lots of issues with this and I wondered if Groovy had some kind of shorthand for this as well?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Groovy 向普通的旧 String 添加了一个execute() 方法,所以试试这个:
Groovy added an execute() method to plain old String, so try this:
如果您使用“cmd /c”命令作为前缀,然后使用&符号(假设是Windows)将命令链接在一起,则可以使用execute()方法更改目录。
例如,假设您想要转到子目录 subdir 并从那里运行几个批处理文件:
不确定是否没有更好的方法,但这确实有效。
The execute() method can be used to change directories if you prefix it with the "cmd /c" command, and then use ampersand (assuming Windows) to chain commands together.
Example, assuming you want to go to subdirectory subdir and run a couple of batch files from there:
Not sure if there isn't a better way, but this does work.
您还可以使用 ProcessBuilder,它是 java 5 中引入的一个非常方便的 Java 类。
ProcessBuilder 可以让您
请参阅 http://download.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html< /a> 获取简短示例和更多文档。
You can also use ProcessBuilder which is a surprisingly convienent Java class introduced in java 5.
ProcessBuilder lets you
See http://download.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html for a brief example and more documentation.
如果您不害怕创建一些可重用的代码,您可以创建一个包装 .execute() 进程的对象。我创建了类似的东西并经常使用它。
使用以下命令创建一个新进程:
def proc="cmd".execute()
之后,您可以使用“consumeProcessOutput()”来管理“proc”的输入和输出。您发送给它的任何内容都将被执行,就像您将其输入到 shell 中一样,并且该 shell 的所有输出都将可供您使用。
我将所有这些都封装在一个闭包中,以便您可以执行以下操作:
获得仅显示 autoexec.bat 行的显示。请注意,在从闭包返回 true 之前,该进程的标准输入都是可用的,因此您可以发送更多文本行并无限期地与之交互。
我经常使用它,因为像“cd”和“Dir”这样的命令在带有 .execute() 的 Windows 中不起作用,所以一个简单的:
将让我轻松获得快速目录列表。
If you aren't afraid to create some reusable code you could create an object that wraps an .execute() process. I created something like this and use it regularly.
Create a new process with:
def proc="cmd".execute()
After that you can use "consumeProcessOutput()" to manage the input and output of "proc". Anything you send to it will be acted on as though you typed it into a shell, and all the output of that shell will be available to you.
I wrapped all this up in a closure so that you could do this:
To get a display that shows only the autoexec.bat line. Note that until you return true from the closure, the stdin of that process is available so you can send more lines of text and interact with it indefinitely.
I use it quite a bit because commands like "cd" and "Dir" don't work in windows with .execute(), so a simple:
will get me a quick directory listing with ease.