我有一个用于示例的 groovyscript
"mvn install".execute()
虽然这在 Linux 上运行得很好,但在 Windows 上却失败了。在 Windows 上,必须这样写:
"cmd /C mvn install".execute()
这有点烦人。我只是想通过使用 if OS == windows then
来避免代码困惑。我很确定有人已经解决了这个问题并为此提供了一些库。我只是找不到任何东西......
最佳答案
由于 String.execute()
委托(delegate)给操作系统,因此不应期望它是可移植的,但您可以创建自己的可移植等效项。
String.metaClass.pexecute = {
if(/* Windows OS check goes here*/) {
"cmd /C $delegate".execute()
} else {
delegate.execute()
}
}
将此代码放在脚本的早期位置,您可以像这样调用 String.pexecute()
:"mvn install".pexecute()
关于groovy - 如何使用 "execute"使 groovy 脚本可移植?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34220615/