如何通过Groovy执行一个Shell命令

3

我可以帮你将一个shell命令用groovy语言编写并通过gradle执行。例如,我有以下这个命令:

git log tag2 tag1

这列出了两个标签之间所做的提交。我想用Groovy编写它。目前,我的写法是:


git log tag1..tag2

请注意,这里的“tag1”和“tag2”分别是两个标签的名称。

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

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

https://gist.github.com/katta/5465317 - Ultimo_m
1个回答

2

首先,请确保你在正确的目录下:

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

或者:

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

其次,务必查看所有内容(标准输出、错误输出),以防命令出现问题:

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命令" 了解更多信息。


@Sakshi 你在 C:\Rep9 文件夹里有一个 .git 文件夹吗? - VonC
@Sakshi 请将存储库的路径(包含 .git 文件夹的文件夹)作为文件参数传递。 - VonC
@Sakshi 如果你是通过stash克隆了repo,你就有一个.git文件夹。尝试在该文件夹中运行git status以检查它是否是一个git repo。 - VonC
在主分支上 您的分支比 'origin/master' 提前了 2 次提交。 (使用 "git push" 将本地提交发布) 没有要提交的内容,工作目录干净。这是在 git cli 中运行 git status 命令时得到的信息。 - sver
我编写了任务: task showTwo <<{ def fist = "git log new-Tag4 new-Tag3"
Process process = fist.execute(null, new File("C:\Rep9")) 并且我在命令提示符上使用gradlew showTwo来执行。
- sver
显示剩余12条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接