【问题标题】:Gradle: read latest xy-release GIT tag for versioningGradle:读取最新的 xy-release GIT 标签以进行版本控制
【发布时间】:2012-09-26 12:43:46
【问题描述】:
我目前正在将一个项目从 ant 迁移到 gradle。一切都很好,只存在一个问题:目前我们有一个基于 ant-contrib 的自定义任务,用于在 repo 上执行 git 命令以读取最新的 xy-release 标签以获取版本号(以及生成的 jar 文件名,如 project- xy.jar)。
如我所见,我需要一些插件。可悲的是,通过可用的插件,没有什么能真正帮助我(存在一些与 git 相关的插件,但他们的目标是别的东西,比如 制作 一个版本,所以标记 repo,而不是阅读已经-制作标签)。
所以我需要帮助来实现我的目标。我必须在 Gradle 中发明整个东西吗?我知道我可以导入现有的 ant 构建文件,但我真的不想这样做。
【问题讨论】:
标签:
git
ant
versioning
gradle
【解决方案1】:
好的,我尝试在 Gradle 中实现相同的旧东西。 (因为我正在逃离蚂蚁/常春藤,所以我不想保留旧东西)。这是输出(可能很蹩脚,因为我是 Gradle 和 Groovy 的新手)。它有效。
jar {
dependsOn configurations.runtime
// get the version
doFirst {
new ByteArrayOutputStream().withStream { execOS ->
def result = exec {
executable = 'git'
args = [ 'describe', '--tags', '--match', '[0-9]*-release', '--dirty=-dirty' ]
standardOutput = execOS
}
// calculate version information
def buildVersion = execOS.toString().trim().replaceAll("-release", "")
def buildVersionMajor = buildVersion.replaceAll("^(\\d+).*\$", "\$1")
def buildVersionMinor = buildVersion.replaceAll("^\\d+\\.(\\d+).*\$", "\$1")
def buildVersionRev = buildVersion.replaceAll("^\\d+\\.\\d+\\.(\\d+).*\$", "\$1")
def buildTag = buildVersion.replaceAll("^[^-]*-(.*)\$", "\$1").replaceAll("^(.*)-dirty\$", "\$1")
def dirty = buildVersion.endsWith("dirty")
println("Version: " + buildVersion)
println("Major: " + buildVersionMajor)
println("Minor: " + buildVersionMinor)
println("Revision: " + buildVersionRev)
println("Tag: " + buildTag)
println("Dirty: " + dirty)
// name the jar file
version buildVersion
}
}
// include dependencies into jar
def classpath = configurations.runtime.collect { it.directory ? it : zipTree(it) }
from (classpath) {
exclude 'META-INF/**'
}
// manifest definition
manifest {
attributes(
'Main-Class': 'your.fancy.MainClass'
)
}
}
【解决方案2】:
或者,只需重用现有的 Ant 任务。无需导入 Ant 构建。
【解决方案3】:
既然 git 有这个命令,git describe,它应该是很简单的。只需调用 git 并读取输出。要获取最后一个 *-release 标签,确切的命令是
git describe --abbrev=0 --match=*-release
(我不确定是正则表达式还是全局;如果是正则表达式,则需要.*-release)。如果您的发布标签没有注释,请添加--tags。