【问题标题】:How to execute a shell command through groovy如何通过groovy执行shell命令
【发布时间】:2016-01-15 05:51:42
【问题描述】:

我想在 groovy 中编写一个 shell 命令并通过 gradle 执行。例如,我有这个命令:

git log tag2 tag1

这列出了在两个标签之间完成的提交。 我只是想用 groovy 写这个。 目前,我的写作方式是:

task show <<                                       
{                                          
    def fist = "git log new-tag4 new-tag5" 
    println("[fist]")                      
    Process process = fist.execute()       
    println(process.text)                  
}  

此构建成功,但没有给我结果。我有什么遗漏或做错了吗?

【问题讨论】:

标签: git shell groovy gradle


【解决方案1】:

首先,确保您在正确的目录中:

Process process = fist.execute(null, new File("your git repo"))

或者:

"git log new-tag4 new-tag5".execute(null, new File("C:\Rep9"))  

其次,确保看到所有内容(stdout、stderr),以防命令出现问题:

def process=new ProcessBuilder("git log new-tag4 new-tag5").redirectErrorStream(true).directory(new File("your git repo")).start()
process.inputStream.eachLine {println it}

在“Executing shell commands in Groovy”查看更多信息。

【讨论】:

  • 在命令中,我们必须指定目录名还是文件名,因为当我执行上述语句时,它说:无法运行程序“git log new-Tag4 new-Tag3”(在目录 "C:\Rep9"): CreateProcess error=2, 系统找不到指定的文件
  • @Sakshi 你在C:\Rep9 中有一个.git 文件夹吗?
  • 不,我不知道。我已经使用 sourceTree 从 bitbucket 克隆了这个 repo。没有 .git 文件夹
  • @Sakshi 请作为文件参数传递 repo 的路径(其中包含 .git 的文件夹)
  • 好的,但是我该如何处理这个 repo。如,我想在这个 repo 上执行我的命令,我没有 .git 文件夹
猜你喜欢
  • 2010-09-14
  • 1970-01-01
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
  • 2014-02-20
相关资源
最近更新 更多