3

So I have the following code snippet:

def getVersion = { ->
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'describe', '--tags'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

and whenever I call getVersion(), I get the following error:

* What went wrong:
A problem occurred evaluating root project 'ForgeWorkspace'.
> Process 'command 'git'' finished with non-zero exit value 128

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 6.442 secs

On my MacBook Pro I've never encountered this issue, but do on Windows. Any help is greatly appreciated!

T145
  • 1,415
  • 1
  • 13
  • 33
  • You might consider looking into the Gradle git plugin rather than coding it yourself: https://github.com/ajoberstar/gradle-git (maybe it provides what you are looking for) – cjstehno Dec 18 '15 at 18:39
  • I just don't want to add another plugin if I don't have to. If you have an answer that implements my function using that plugin then plop it below. – T145 Dec 18 '15 at 19:43

2 Answers2

2

@RaGe is almost correct. Since you indeed need to use the Windows Command Prompt (cmd) to have the OS search the git executable in the system path, the entire git command should be passed as one argument that follows the /c switch (which means 'execute command').

So the following should work:

commandLine 'cmd', '/c', 'git describe --tags'
stav
  • 1,497
  • 2
  • 15
  • 40
0

On windows, the first two arguments of commandLine should be cmd and /c

//on windows:
commandLine 'cmd', '/c', 'git'...

See here.

RaGe
  • 22,696
  • 11
  • 72
  • 104
  • I added `commandLine 'cmd', '/c', 'git', 'describe', '--tags'` and still got practically the same error, except with 'cmd' instead of 'git'. – T145 Dec 18 '15 at 19:38
  • is `getversion` supposed to be a task? – RaGe Dec 18 '15 at 19:52
  • No, it's supposed to return a unique version based on the latest Git release and commit. The function works perfectly on my MacBook, but for reason will not on my Windows machine. The only problem is with the `commandLine` line. – T145 Dec 18 '15 at 19:54
  • run using `--stacktrace` and post your stacktrace please. – RaGe Dec 21 '15 at 15:11
  • I would also try using the full path for git instead of just `'git'` – RaGe Dec 21 '15 at 15:11