Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make System command calls in Java/Groovy?

What I want to do is invoke maven from a groovy script. The groovy script in question is used as a maven wrapper to build J2EE projects by downloading a tag and invoking maven on what was downloaded. How should I accomplish invoking maven to build/package the EAR (the groovy script is already capable of downloading the tag from SCM).

like image 227
Zombies Avatar asked Apr 23 '10 19:04

Zombies


People also ask

How do I run a command in Groovy?

execute() returns a Process object which is why "ls". execute(). text works. You should be able to just read the error stream to determine if there were any errors.

How do I run a batch file in Groovy script?

Select the trigger Genius Commit created (from the DevOps category). Choose the command Run code, and click Save. Click New action. Select the action Run Groovy script.

How do I use Groovy in Java?

parse the source files. depending on the implementation, create stubs that are compatible with the Java compiler. invoke the Java compiler to compile the stubs along with Java sources – this way Java classes can find Groovy dependencies. compile the Groovy sources – now our Groovy sources can find their Java ...


1 Answers

The simplest way to invoke an external process in Groovy is to use the execute() command on a string. For example, to execute maven from a groovy script run this:

"cmd /c mvn".execute() 

If you want to capture the output of the command and maybe print it out, you can do this:

print "cmd /c mvn".execute().text 

The 'cmd /c' at the start invokes the Windows command shell. Since mvn.bat is a batch script you need this. For Unix you can invoke the system shell.

like image 149
Chris Dail Avatar answered Sep 17 '22 13:09

Chris Dail