40

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).

Zombies
  • 25,039
  • 43
  • 140
  • 225

4 Answers4

56

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.

Chris Dail
  • 25,715
  • 9
  • 65
  • 74
  • Cool! Didn't know you can do that. – armandino Apr 27 '10 at 05:46
  • This worked first time for me. Went down the Process "consumeProcessOutput()" route for a while but had no dice with that. This'll help greatly with determining reasons for Docker build fails. – eversMcc Mar 01 '17 at 11:58
35

It is as simple as doing

"yourCommand".execute();

If you want to get print outputs on the executed command on standard output you can do

def proc = "yourCommand".execute();
proc.waitForProcessOutput(System.out, System.err);

If you want to store and process the output you can do

def proc = "yourCommand".execute();
def outputStream = new StringBuffer();
proc.waitForProcessOutput(outputStream, System.err);
println(outputStream.toString());

UPDATE:

Also you can set working dir by

File workingDir = file("<path to dir>")
def proc = "yourCommand".execute([], workingDir.absolutePath);
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • 3
    when I do this def homeDir = "c:/test/server"; def workDir = "${homeDir}/workDir"; def tempDir = "${workDir}/tmp"; println(homeDir); println(workDir); runShell("cd c:"); //runShell("mkdir -p ${tempDir}"); void runShell(String s) { def execute = s.execute(); execute.waitForProcessOutput(System.out,System.err); def value = execute.exitValue(); if (val > 0) { throw new Exception("Exit value: ${value}"); } } I get an error as below java.io.IOException: Cannot run program "cd": CreateProcess error=2, The system cannot find the file specified – user641887 Nov 01 '16 at 23:27
6

For Java 7+ stdio redirection:

new ProcessBuilder('cmd', …args…).redirectOutput(ProcessBuilder.Redirect.INHERIT).start().waitFor();
Jesse Glick
  • 24,539
  • 10
  • 90
  • 112
  • If you pass in a `List` of args, make sure the elements are all `String`s and not [GString](http://docs.groovy-lang.org/docs/latest/html/api/groovy/lang/GString.html)s with interpolated variables. Also don't forget to `redirectError` as well. – seanf Nov 23 '17 at 01:35
4

You may use Runtime class to launch a shell command. take a look here: http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html#exec(java.lang.String) You may later capture the results of the Process execution (to find out if it failed or not).

Alex Khvatov
  • 1,415
  • 3
  • 14
  • 23