繁体   English   中英

如何通过groovy执行shell命令

[英]How to execute a shell command through groovy

我想在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)                  
}  

这个构建成功,但没有给我结果。 我遗失或做错了什么?

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

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}

有关详细信息,请参阅“ 在Groovy中执行shell命令 ”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM