在Android build.gradle中使用`git describe --match`

时间:2017-12-28 17:41:34

标签: android git gradle

无法获取与Gradle中传递给git describe的glob匹配的最新git标记。它在终端时工作正常。

我尝试了以下内容:

project.ext.releaseVersionName = "git describe --match \'[0-9]*.[0-9]*.[0-9]*\' --abbrev=0 --tags".execute().text.trim()

def getReleaseVersion = { ->
    try {
        def stdout = new ByteArrayOutputStream()
        exec {
            commandLine 'bash', '-c', 'git', 'describe', '--match "[0-9]*.[0-9]*.[0-9]*"', '--abbrev=0', 'HEAD'
            standardOutput = stdout
        }
        return stdout.toString().trim()
    }
    catch (ignored) {
        return null
    }
}

但是两者都返回空字符串。如果我没有匹配,那么一切正常。我认为这是导致问题的全球化。

1 个答案:

答案 0 :(得分:1)

通过将整个'--match "[0-9]*.[0-9]*.[0-9]*"'放在单引号中,您基本上会传递一个包含整个字符串的选项。你真正想要的可能是通过参数--match传递选项[0-9]*.[0-9]*.[0-9]*。因此,您应该拆分该参数,以便commandLine成为:

commandLine 'git', 'describe', '--match', '[0-9]*.[0-9]*.[0-9]*', '--abbrev=0', 'HEAD'

或者,您可以将--match参数切换为--arg=value语法,即像--match=[0-9]*.[0-9]*.[0-9]*一样使用--abbrev=0

我根据评论删除了'bash', '-c'部分。如果要使用'bash', '-c',则其余部分应为单个字符串,因为它将作为-c的{​​{1}}参数的值。