Git命令如何在Windows到Gradle上执行?

How can the Git command be executed on Windows through Gradle?

所以我有以下代码片段:

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

并且每当我调用 getVersion() 时,我都会收到以下错误:

* 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

在我的 MacBook Pro 上我从未遇到过这个问题,但在 Windows 上遇到过。非常感谢任何帮助!

在windows上,commandLine的前两个参数应该是cmd和/c

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

参见here

@RaGe 几乎是正确的。由于您确实需要使用 Windows 命令提示符 (cmd) 让 OS 在系统路径中搜索 git 可执行文件,因此整个 git 命令应传递为/c 开关后面的一个参数(表示 'execute command')。

所以以下应该有效:

commandLine 'cmd', '/c', 'git describe --tags'