groovy - 如何使用 "execute"使 groovy 脚本可移植?

标签 groovy

我有一个用于示例的 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/

相关文章:

linux - 通过Jenkins执行Groovy命令: "find: missing argument to ` -exec'"

ant - 如何在 groovy 中使用 execute() 来运行任何命令

shell - 将 shell 变量与 Jenkinsfile 中的常规变量混合并匹配

gradle - 只能在NetBeans中加载一些gradle项目

java - 将Spring Boot jar名称传递给Gradle Groovy脚本

grails - Groovy markupBuilder更新父节点

jenkins - 如何使用 Groovy 动态构建 Jenkins 管道输入参数?

Groovy 字符串解析成组

gradle - Gradle中的Groovy版本

java - 混合使用 Groovy 和 Java?