Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a shell command through groovy

I want to write a shell command in groovy and execute via gradle. For example, I have this command :

git log tag2 tag1

This lists the commits done in between two tags. I simply want to write this in groovy. Currently, I am writing like :

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

This build successfully but doesn't give me the result. Anything I am missing or doing wrong?

like image 842
sver Avatar asked Feb 17 '26 01:02

sver


1 Answers

First, make sure you are in the right directory:

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

Or:

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

Second, make sure you see everything (stdout, stderr) just in case the command has an issue:

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

See more at "Executing shell commands in Groovy".

like image 145
VonC Avatar answered Feb 18 '26 16:02

VonC